-1

Once a JPanel has been instantiated and added to a visible JFrame, how do I add a new JComponent to it and update the display to show said new JComponent?

Original Question:

How to add JComponent to JPanel after JPanel has been added to a JFrame. I think I may have to extend JPanel and possibly override paintComponent().

ThunderWolf
  • 130
  • 1
  • 9
  • care to elaborate with some code? – David Kroukamp Aug 04 '12 at 18:28
  • Thunder: yours is a very incomplete question, one that you're lucky someone guessed at what you were actually asking and then gave you a valid answer. Please next time try to put more information into your question. A good rule of thumb is to put as much effort in asking your question as you'd like a volunteer to exert when answering it. – Hovercraft Full Of Eels Aug 04 '12 at 18:56
  • I disagree I think my post is logical and to the point. I don't think Max guessed at the question, but he would have to be the one to tell me if he did. – ThunderWolf Aug 04 '12 at 19:07
  • This post has to do with java, if you are not a java programmer chances are you will not understand it @Hovercraft Full Of Eels – ThunderWolf Aug 04 '12 at 19:20
  • @ThunderWolf: I think that I understand Java fairly well, but I did not understand exactly where you were stuck. Were you having problems with not seeing a component added to an already visualized container? Were you having problems due to layout issues? My point is that all who answered your question asked for more information and code, and this time you were lucky that someone guessed just what your issue is, but next time you might not be so lucky. You will have an easier time of it if you show some code showing just where your stuck and describing your problem more. – Hovercraft Full Of Eels Aug 04 '12 at 19:25
  • Also Max already told you he wanted more information in his request for an [sscce](http://sscce.org). "It is easier if you can post SSCCE with your question." I would listen to him. – Hovercraft Full Of Eels Aug 04 '12 at 19:29
  • Yes Max did edit that in and Max's reply was very well appreciated as where all replies, but in the first sentence: "after JPanel has been added to a JFrame" and second sentence: "possibly override paintComponent()" used together as clues where descriptive that the JPanel was already being displayed. As a matter of fact both of the replies got that. Did you even read my post I mean really read it @Hovercraft Full Of Eels, or did you just read the replies asking for a code example. There I think that pretty well describes it! – ThunderWolf Aug 04 '12 at 23:12

2 Answers2

4

JPanel.add() should work fine. If the frame is already visible, then call:

validate();
repaint();

Also, depending on the size and layout you may need to repack the frame with pack().

It is easier if you can post SSCCE with your question.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
1

I don't know if I understand your question, there should be no problem adding any JComponent to a JPanel, befor or after it has been added to a JFrame:

JPanel panel = new JPanel();
frame.setContentPane(panel);
// sometime later
panel.add(new JLabel("JLabel is a JComponent")); 

Maybe if you post your code, the question will be somewhat more clear.

pafau k.
  • 1,667
  • 12
  • 20