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
Solved

Refresh JPanel from a JButton in  another class

Posted on 2004-04-20
16
6,873 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I build my own IDE using ASP.NET MVC? 2 53
tomcat administrtor 12 61
Export all vba modules from outlook vbaproject.otm 7 32
java example issue 5 24
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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 implement Singleton Design Pattern in Java.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

829 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