import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
public
class
Tool
extends
JFrame
implements
ActionListener, ItemListener {
static
JToolBar tb;
static
JButton b1, b2;
static
JFrame f;
static
JComboBox x;
static
JLabel l, l1;
public
static
void
main()
{
Tool to =
new
Tool();
l =
new
JLabel(
"nothing selected"
);
l1 =
new
JLabel(
"nothing selected"
);
f =
new
JFrame(
"Toolbar demo"
);
f.setLayout(
new
BorderLayout());
tb =
new
JToolBar(
"toolbar"
);
tb.setOrientation(SwingConstants.VERTICAL);
JPanel p =
new
JPanel();
p.setLayout(
new
BoxLayout(p, BoxLayout.Y_AXIS));
x =
new
JComboBox(
new
String[] {
"item 1"
,
"item 2"
,
"item 3"
});
x.addItemListener(to);
b1 =
new
JButton(
"button 1"
);
b2 =
new
JButton(
"button 2"
);
b1.addActionListener(to);
b2.addActionListener(to);
p.add(b1);
p.add(b2);
p.add(x);
tb.add(p);
JPanel p1 =
new
JPanel();
p1.add(l);
p1.add(l1);
f.add(tb, BorderLayout.WEST);
f.add(p1, BorderLayout.CENTER);
f.setSize(
500
,
500
);
f.setVisible(
true
);
}
public
void
actionPerformed(ActionEvent e)
{
l.setText(e.getActionCommand() +
" selected."
);
}
public
void
itemStateChanged(ItemEvent e)
{
l1.setText(x.getSelectedItem() +
" selected."
);
}
}