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