?
Solved

Refresh JPanel from a JButton in  another class

Posted on 2004-04-20
16
Medium Priority
?
6,927 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
Industry Leaders: 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 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

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!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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