• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding a jComponent dynamically  RSS feed

 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

Im trying to add a Jpanel (which has some stuff on it) to a another Jpanel (that has already been placed on the Jframe) when a button is clicked. Is this actually possible? Currently when i run it, it does add the Jpanel, but it is very small.

When the button is clicked i do the following---



Thanks for any help.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the layout of the JPanel you are adding the new JPanel to? That is the main thing that will affect the sizing.
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its grid layout, ive done it like this:

this.lineGraphPanel.setLayout(new GridLayout(2, 2));

Also note i tried borderlayout as well, and i got the same problem.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding a component to a container that's visible on the screen, you must call validate() on the container to get it to redo its layout.
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys, but its still having the same problem. I tried to call validate as you said below.

LineGraphGui l = new LineGraphGui("Node "+row, "Time", "Temperature");
lineGraphPanel.add(l);
lineGraphPanel.validate();


Any other ideas?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a GridLayout, all the subcomponents will always be the same size. Is that what you're seeing? You said your subpanel is "small" -- but it's just the same size as all the other contents of the main panel, right?
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

no its not the same size as the other stuff in the main panel. Actually the added panel is shown in a 1 by 1 box which you can just see. Do you have any other ideas that might help?
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, i forgot to mention that the Jpanel that im adding to is initally empty, maybe thats the problem?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at this point it would be better for you to post a working sample program.

just a frame with the gridlayout panel at "center", a button at "south",
the code for LineGraphGui (strip all unnecessary stuff), and the code for the
button to add the LineGraphGui.
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i ive just done what you said and i have found some problems.

When i try to add the LineGraphGui to the main panel(by pressing the button), nothing happens.

And just as a test i tried adding the LineGraphGui before runtime to see if everything is ok, but it wasnt. It is appearing really small on the screen just like before.

Any ideas?


[ September 07, 2006: Message edited by: john latham ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't test your code without the LineGraphGui code.

if LineGraphGui has no components, perhaps you need to give it a preferredSize
setPreferredSize(new Dimension(100,100));//add to constructor of LineGraphGui
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didnt work, but here is the complete source code. Note im using an external library to build the graph. You can download the jar here: http://prdownloads.sourceforge.net/jchart2d/jchart2d-2.0.0.jar?download


[ September 07, 2006: Message edited by: john latham ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in LineGraphGui constructor add this

makeGraph();
setBackground(Color.BLUE);//<------

you will see the panel adds OK

the problem is the chart has no size
add this, making (100,100) whatever you want

 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help, It works well
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!