Dialog separator : Dialog « Swing JFC « Java






Dialog separator

Dialog separator
   
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class DialogSeparator extends JLabel {
  public static final int OFFSET = 15;

  public DialogSeparator() {
  }

  public DialogSeparator(String text) {
    super(text);
  }

  public Dimension getPreferredSize() {
    return new Dimension(getParent().getWidth(), 20);
  }

  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  public Dimension getMaximumSize() {
    return getPreferredSize();
  }

  public void paint(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());

    Dimension d = getSize();
    int y = (d.height - 3) / 2;
    g.setColor(Color.white);
    g.drawLine(1, y, d.width - 1, y);
    y++;
    g.drawLine(0, y, 1, y);
    g.setColor(Color.gray);
    g.drawLine(d.width - 1, y, d.width, y);
    y++;
    g.drawLine(1, y, d.width - 1, y);

    String text = getText();
    if (text.length() == 0)
      return;

    g.setFont(getFont());
    FontMetrics fm = g.getFontMetrics();
    y = (d.height + fm.getAscent()) / 2;
    int fontWidth = fm.stringWidth(text);

    g.setColor(getBackground());
    g.fillRect(OFFSET - 5, 0, OFFSET + fontWidth, d.height);

    g.setColor(getForeground());
    g.drawString(text, OFFSET, y);
  }
  public static void main(String argv[]) {
    new FlightReservation();
  }
}

class FlightReservation extends JFrame {
  public FlightReservation() {
    super("Dialog ");

    Container c = getContentPane();
        c.setLayout(new FlowLayout());
    c.add(new DialogSeparator("Options"));

    ButtonGroup group = new ButtonGroup();
    JRadioButton r1 = new JRadioButton("First class");
    group.add(r1);
    c.add(r1);

    JRadioButton r2 = new JRadioButton("Business");
    group.add(r2);
    c.add(r2);

    JRadioButton r3 = new JRadioButton("Coach");
    group.add(r3);
    c.add(r3);

    c.add(new DialogSeparator());

    JButton b3 = new JButton("Exit");
    c.add(b3);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);

    setSize(300,200);
    setVisible(true);
  }
}
           
         
    
    
  








Related examples in the same category

1.Creating and using Dialog BoxesCreating and using Dialog Boxes
2.Dialog boxes and creating your own componentsDialog boxes and creating your own components
3.A frame that can easily support internal frame dialogsA frame that can easily support internal frame dialogs
4.An example of using the JOptionPane with a custom list of options in anAn example of using the JOptionPane with a custom list of options in an
5.See the differences between various types of option panesSee the differences between various types of option panes
6.Vote DialogVote Dialog
7.Create simple about dialogCreate simple about dialog
8.Message dialogMessage dialog
9.Error message dialogError message dialog
10.Information dialog with customized logoInformation dialog with customized logo
11.Input dialog with user-defined logoInput dialog with user-defined logo
12.Confirmation dialogConfirmation dialog
13.Default button for dialog: press Enter to activateDefault button for dialog: press Enter to activate
14.Simple dialog for asking a yes no questionSimple dialog for asking a yes no question
15.Class to Prompt the User for an ID and Password
16.Simple Save Dialog demoSimple Save Dialog demo
17.Demonstrate JOptionPaneDemonstrate JOptionPane
18.Create Color Sample PopupCreate Color Sample Popup
19.Simple Input DialogSimple Input Dialog
20.No button dialogNo button dialog
21.Message Dialog demo Message Dialog demo
22.Escape Key close Dialog
23.Dialog can be closed by pressing the escape key
24.Dialog which displays indeterminate progress
25.Dialog with Escape KeyDialog with Escape Key
26.Modal Message Dialog
27.A frame with a menu whose File->Connect action shows a password dialogA frame with a menu whose File->Connect action shows a password dialog
28.A sample modal dialog that displays a message and waits for the user to click the Ok button