J Components Not Visible

Hi,

I have several J Components (like JButton, JComboBox, JTextField) that I would like to add to my application. However, when I add them to my JPanel, none of these objects are visible. I have tried setVisible(true) and it still doesn't work. Any ideas as to what may be going on here?

Thanks,

JOD8FY

[328 byte] By [JOD8FYa] at [2007-11-27 5:35:23]
# 1
If you add the components to the panel after the panel is visible then you need to revalidate() the panel.Otherwise read the [url http://java.sun.com/docs/books/tutorial/uiswing/TOC.html]Swing tutorial[/url] for working examples.
camickra at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for your reply. Unfortunately, it still doesn't seem to be working. Where exactly am I supposed to call revalidate()? I've tried it in init() and in the paint method, but still have had no success. Any help is greatly appreciated.JOD8FY
JOD8FYa at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I've tried it in init() and in the paint method

There is no need to override the paint() method. If you are doing this that is probably what your problem is. Why do you override a method when you don't know what is does.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 4

I think this is more complicated than it should be. I've never used any J components before (for example I've always used Button instead of JButton, TextField intsead of JTextField), so I'm unfamiliar with how they work. What I wanted to do originally is be able to utilize the doClick() feature of a JButton, as this feature is not available with a regular Button. Is there any way to perform a similar doClick() action on a regular Button?

JOD8FY

JOD8FYa at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

> I think this is more complicated than it should be.

No its not. Why do you think I gave you a link to the Swing tutorials.

If you take a look at any of the tutorials you can download a "working" example program. Since your question is about buttons, you may want to start with that tutorial.

There is no trick. Your first simple Swing GUI can be written in about 5 lines of code.

a) create the jframe

b) create the jbutton

c) add the button to the frame

d) pack the frame

e) make the frame visible

If you are not willing to read the tutorial and post a SSCCE, then I'm not willing to help.

camickra at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 6
> s there any way to perform a similar doClick() action on a regular Button?move the code from the button's actionPerformed() into a separate method.now you can call that method from elsewhere in your program
Michael_Dunna at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 7

First thing you can do, is add them to the JPanel, before making it visible. Something like:

JPanelpanel= new JPanel();

JTextField txtField = new JTextField(10);

JButtonbutton= new JButton("Button");

// first add them to JPanel

panel.add( txtField );

panel.add( button );

// now do anything you want with the panel ...

JFrame frame = new JFrame();

frame.add( panel );

frame.setVisible( true );

However, if the panel is already created and visible, call revalidate() on the panel, after you add new components:

JTextField txtField = new JTextField(10);

JButtonbutton= new JButton("Button");

panel.add( txtField );

panel.add( button );

panel.revalidate();

Hope it helps.

Babantika at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks so much, I think that's exactly what I'm looking for.JOD8FY
JOD8FYa at 2007-7-12 15:04:22 > top of Java-index,Desktop,Core GUI APIs...