Solved

Refresh JPanel from a JButton in  another class

Posted on 2004-04-20
16
6,849 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now