Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Adding a jComponent dynamically

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, i forgot to mention that the Jpanel that im adding to is initally empty, maybe thats the problem?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help, It works well
 
Grow your own food... or this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic