aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Repaint my swing app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Repaint my swing app" Watch "Repaint my swing app" New topic
Author

Repaint my swing app

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
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 ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Nov 18, 2003
Posts: 179
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

Joined: Jun 09, 2003
Posts: 4632
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'
Chet Haase
author
Ranch Hand

Joined: Aug 16, 2007
Posts: 32
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.


Book: <a href="http://filthyrichclients.org" target="_blank" rel="nofollow">http://filthyrichclients.org</a> <br />Java: <a href="http://weblogs.java.net/blog/chet" target="_blank" rel="nofollow">http://weblogs.java.net/blog/chet</a> <br />Humor: <a href="http://chetchat.blogspot.com" target="_blank" rel="nofollow">http://chetchat.blogspot.com</a>
Dhilshuk Reddy Jeeru
Greenhorn

Joined: Sep 19, 2007
Posts: 2
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Repaint my swing app