Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Refresh JPanel from a JButton in  another class

Posted on 2004-04-20
16
Medium Priority
?
6,977 Views
Last Modified: 2013-11-23
We have a JPanel that's populated from a database that consists numerous JLabels and JButtons.  Is there a way to refresh or clear the JPanel to except new results from click on a JButton from another class?

Basically is there a way to refresh the JPanel??  We've tried using
JPanel.remove();
and we couldn't get it to work.   Can anyone help us?
0
Comment
Question by:calvinklein1
  • 9
  • 3
  • 2
  • +2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10873175
You need to do

validate();
repaint();

too
0
 
LVL 92

Expert Comment

by:objects
ID: 10873176
panel.removeAll();
0
 
LVL 92

Expert Comment

by:objects
ID: 10873177
better also call:

panel.revalidate();
panel.repaint();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 10873188
And ensure you make the call from the EDT:

EventQueue.invokeAndWait(new Runnable()
{
   public void run()
   {
      panel.removeAll();
      panel.revalidate();
      panel.repaint();
   }
});

0
 
LVL 92

Expert Comment

by:objects
ID: 10873201
you only need to use invokeAndWait/invokeLater if you are doing the removing from a thread other than the event dispatch thread.
If it is being done as a result of a button press then it is not necessary and you can call it directly.
0
 

Author Comment

by:calvinklein1
ID: 10873847
It seems easy enough.  I'm obviously trying something more complex than the following example but the thought process remains the same.  In the example below, instead of updating the JLabel by an event that says jLabel.setText("changed") - would I not be able to change the string field that the jLabel's text is set to - then, removeAll, revalidate and repaint?  


// CODE START

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RefreshTest extends JFrame implements ActionListener {

    JPanel jPanel = new JPanel();
    JButton jButton = new JButton();
    JLabel jLabel = new JLabel();
    String labelText = "start";

    private RefreshTest(){

        super("Test");

        jPanel.setLayout(new FlowLayout());

        // Set jLabel text to equal labelText
        jLabel.setText(labelText);

        jButton.setText("Push Me");
        jButton.addActionListener(this);

        jPanel.add(jButton);
        jPanel.add(jLabel);

        getContentPane().add(jPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pack();
        show();



    }
  public void actionPerformed(ActionEvent e) {
       
        // Change labelText = "changed" and refresh panel to repaint changes.

        labelText = "changed";
        jPanel.removeAll();
        jPanel.revalidate();
        jPanel.repaint();
    }


    public static void main(String[] args){

        RefreshTest rt = new RefreshTest();

    }

}


// CODE ENDS
0
 
LVL 92

Expert Comment

by:objects
ID: 10873866
you need to do:
jLabel.setText(labelText);

but as you are removing the label why would u want to?

0
 
LVL 92

Expert Comment

by:objects
ID: 10873874
> would I not be able to change the string field that the jLabel's text is set to - then, removeAll, revalidate and repaint?

Depends on the end result you are execting :)
doing:
labelText = "changed";
would change the value of that String variable, but would have no effect on the label.

does that answer you r q?
0
 

Author Comment

by:calvinklein1
ID: 10873898
Basically, I want to change the String variable and then refresh the panel, which would then use the String variable to populate the jLabel - thus, updating the jLabel's text without doing jLabel.setText("changed").  I'm really not sure if that is really possible.
0
 
LVL 92

Expert Comment

by:objects
ID: 10873913
>  I'm really not sure if that is really possible.

No you need to call setText() to update the labels text.
Why don't you want to call setText()?
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10873929
0
 

Author Comment

by:calvinklein1
ID: 10873946
We're using a pretty lengthy Vector that we are returning from a ResultSet.  We are iterating through the vector and populate several JLabels and JButtons in the same JPanel.  Rather than set up another iterator to go through and change the text of each JLabel and JButton when a new ResultSet is returned.  It seems much easier to simply change the value of the Vectors and then refresh the JPanel, which would repopulate the many JLabels and JButtons that we are using.

But, if that can't be done, we'll either create an eventhandler to change the JLabels or end up using a JTable.

thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10874034
Yes you can't avoid calling setText() for your labels if you want to change the text it displays.
0
 
LVL 1

Accepted Solution

by:
ACrasborn earned 2000 total points
ID: 10876368
Unfortunately, you cannot only change the String variable because String objects are constants! That's why JLabel and JButton objects will still keep a reference to the first given String even if these String have been updated. The only way to update JLabel and JButton objects is to use the method setText(...).
0
 
LVL 92

Expert Comment

by:objects
ID: 10884435
Isn't that what I already said?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10884472
It's not an answer to the question anyway. It just fits into the category of 'additional comments'
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question