• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Repaint my swing app

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am quite new to Swing gui programming and I may not use it correctly. I have a problem with refreshing my application gui components.

My application executes a JFrame that has a JMenu for opening a input file and a JPanel for displaying data. So I open a input file, get the needed data, create some components with that data and finally I add them to the content JPanel. When all is done, the content panel still is empty. When I minimize and maximize the application frame, the contents of content panel get visible. I wonder what am I doing wrong.

I add a code of the content panel and I use "addContent()" method for adding the components to the panel.



I have no idea, what I am doing wrong. Help is very welcome.
[ September 19, 2007: Message edited by: Juhan Voolaid ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any time you add/remove components (after the parent is visible) you need to
to revalidate the parent

[add components to panel]
[panel].revalidate();
[panel].repaint();//sometimes needed, sometimes not
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works! Thank you.

But now I am facing another problem. After loading the data and displaying it on content panel I chose to open new input file. So I load the new data and again use the same "addContent" method to display that new info, but the old content remains on the panel and new data does not get uptated on the screen.

The method now looks like that:

[ September 19, 2007: Message edited by: Juhan Voolaid ]
 
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
there are a number of possibilities, so it would be better to post your code.
Something we can compile/run/observe (strip all irrelevant to problem),
and hard-code 'inputFile1' and 'inputFile2'
 
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have your removed your existing components from the panel? If not, the layout manager may not be choosing to show the new info (and that's probably not what you want anyway - you just want the new info).

But what you probably want is to put the new info in the existing components, not create and add completely new components every time.

Chet.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Juhan Voolaid ,
Here is the trick for your problem which I faced many many times.
step1:Have your contentPanel as a mainPanel to which added JScrollPane
JPanel contentPanel = new JPanel();

step2:Have seperate panel which holds your others components.
JPanel componentsPanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameTextField = new JTextField(10);
componentsPanel.add(nameLabel);
componentsPanel.add(nameTextField );
step3:add that seperate panel to JScrollPane using setViewportView(JComponent jc) method of the JScrollPane.
JScrollPane contentScrollPane = new JScrollPane();
contentScrollPane.setViewportView(componentsPanel);
contentPanel.add(contentScrollPane);
JFrame frame = new JFrame();
frame.add(contentPanel);
frame.setSize(300,300);
frame.setVisible(true);


Each time you create new components mantain all that components in JPanel and again call the setViewportView(JComponent) method.
supoose I create new Panel componentsPanel2 during runtime add to contentPanel:
code below:
JPanel componentsPanel2 = new JPanel();
JLabel passwordLabel = new JLabel("Password");
JTextField passwordTextField = new JTextField(10);
componentsPanel2.add(passwordLabel);
componentsPanel2.add(passwordTextField);
contentScrollPane.setViewportView(componentsPanel2);

This make your components visible without any resize of your JFrame.

Try with this if you don't get it I will post to you the entire sample code of small demo.
Please let me know.
regards,
Dhilshuk Reddy.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Juhan Voolaid:
It works! Thank you.

But now I am facing another problem. After loading the data and displaying it on content panel I chose to open new input file. So I load the new data and again use the same "addContent" method to display that new info, but the old content remains on the panel and new data does not get uptated on the screen.



Any particular reason why you want to reuse the same method and end up adding the same component all over again, when what you are really trying to do is update the data in the table. Well if you really want to stick to this approach, the you can try calling container.removeAll() before the add.

Another way of achieving the same result would be to update the table model.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic