tags:

views:

59

answers:

3

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?

+2  A: 

If you are adding to a frame that has already been made visible, you need to revalidate the frame:

frame.validate();

From the JavaDocs for frame.add(...):

Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component.

If you want to make the color chooser show in a separate dialog (the usual approach), do this:

final Color color = JColorChooser.showDialog(null, "Choose a color", Color.BLUE); 

For JFileChooser here is a typical approach:

JFileChooser ch = new JFileChooser();
ch.showOpenDialog(null);

The JavaDocs for these two dialogs have good working examples.

Steve McLeod
I want to show the dialog into the frame container... but also with invalidate nothing work!
xdevel2000
Dialogs are by definition top level containers. That is, analogous to JFrames. You can't show a dialog into a frame container.
Steve McLeod
@Steve That's not right. I can add these dialogs directly into a JFrame content pane. In fact, this statement do it: add(color_choser, BorderLayout.CENTER). May be the problem is with panes with no layout manager...
xdevel2000
That's not adding a dialog. That's adding a panel. In your question you mentioned dialogs, so I assumed you wanted a dialog.
Steve McLeod
Oops...instead of invalidate() you must call validate()... replace the repaint() lines in your sample code with validate() and the code sample works.
Steve McLeod
Ok, thanks but the code works only if I call validate on a getContentPane() and not directly on a JFrame.
xdevel2000
+1  A: 

Generally I add components to a specific panel and not the frame directly so I would use:

panel.add( someComponent );
panel.revalidate();
panel.repaint(); // sometimes required

But in this case you can just use the validate() method:

color_chooser.setSize( color_chooser.getPreferredSize() );
add(color_chooser); 
validate();
repaint(); 


file_chooser.setSize( file_chooser.getPreferredSize() );
add(file_chooser);  
validate();
repaint();  

Edit: of course you should also use the preferred size of the component so that the entire component is visible. Now all you need to do is add all the code to respond when a user makes a selection. That is a lot of work, which is why it is better to use the dialog version of each class.

camickr
+1  A: 

(See the comments in @Steve McLeod's answer for context.)

JColorChooser and JFileChooser are not regular JComponents. You can add them to a container but you don't see anything because JColorChooser's UI doesn't paint, and JFileChooser doesn't even have a UI.

As in Steve's answer, you can use JColorChooser#showDialog and JFileCHooser#showOpenDialog to get modal dialog, which is the right way to use them.

If you really want, you can call JColorChooser#createDialog then grab its content pane (you could do this for any top-level container):

public void createJColorChooser()
{
    ...
    add(JColorChooser.createDialog(this, "", false, color_chooser, null, null).getContentPane());
    ...
}

And you could override JFileChooser to publicize its createDialog, but please, please don't do that. File chooser should always be modal dialogs.

Geoffrey Zheng