I want to show the dialogs JColorChooser
and JFileChooser
programmatically from
a method called when I submit a buttons.
After the button is clicked the method is invoked but the dialogs won't display.
I have a JFrame with a null layout (absolute positioning) and, e.g., the following code:
public class _TEST_ extends JFrame
{
private JColorChooser color_chooser;
private JFileChooser file_chooser;
public _TEST_()
{
super("_TEST_");
setLayout(null);
final JButton b = new JButton("Color chooser");
final JButton b2 = new JButton("File chooser");
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b)
{
createJColorChooser();
}
else if (e.getSource() == b2)
{
createJFileChooser();
}
}
};
b.addActionListener(al);
b2.addActionListener(al);
b.setBounds(1, 1, 160, 20);
b2.setBounds(1, 30, 160, 20);
add(b);
add(b2);
}
public void createJColorChooser()
{
color_chooser = new JColorChooser();
color_chooser.setBounds(1, 70, 225, 50);
add(color_chooser);
repaint();
}
public void createJFileChooser()
{
file_chooser = new JFileChooser();
file_chooser.setBounds(330, 70, 225, 50);
add(file_chooser);
repaint();
}
public static void main(String args[])
{
_TEST_ window = new _TEST_();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}
what's wrong?