Solved

Refresh JPanel from a JButton in  another class

Posted on 2004-04-20
16
6,880 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 53
Crystal Reports Licensing Questions 4 65
batch vs regular insert in spring DAO 2 25
JDBC performance 8 29
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

730 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