开发者

JColorChooser and JFileChooser open problem

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(ActionE开发者_StackOverflowvent 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?


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.


(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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜