Timurich
Timurich

Reputation: 13

java swing jpanel will not refresh

In my swing application, I have two panels in a frame. First panel has a button. When I click this button, some data changes, and I want the contents of the second panel to change, too, showing the new data (via jlabel). repaint() does not work. I tried all kinds of updates (see comments in the code).

I have a reference to the second panel. I can change the second panel color by using that reference, but I cannot repaint it. I can also make a reference to the jlabel in the second panel and just change its text, but the main point of this question is to make panel2 refresh.

SSCCE is below. Please, take a look.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class Repaint_Test
{
    public static void main(String args[]){     
        Frame frame = new Frame();      
    }
}

class Frame extends JFrame
{
    public Frame()  {   
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,250);
        this.setLocationRelativeTo(null);
        this.setLayout(new GridLayout(1,2));        
        Panel1 p1 = new Panel1();
        Panel2 p2 = new Panel2();
        Data.panel2 = p2; // getting the reference of panel2    
        this.add(p1);
        this.add(p2);       
        this.setVisible(true);
    }
}

class Panel1 extends JPanel
{
    public Panel1(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel1:"));
        JButton button = new JButton("Push");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){         
                Data.text = "text"; // changing commonly accessed data              
                Data.panel2.setBackground(Color.PINK); // this works
                //Data.panel2.invalidate();
                //Data.panel2.validate();
                //Data.panel2.updateUI();
                //Data.panel2.revalidate();
                Data.panel2.repaint();                // this does not work             
            }
        });
        this.add(button);       
    }   
}

class Panel2 extends JPanel
{
    public Panel2(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel2:"));     
        add(new JLabel(Data.text));
    }   
}

class Data
{   
    public static JPanel panel2 = null; 
    public static String text = "ooooo";
}

SOLUTION:

Thanks to Austin Patel's suggestion, I was able to achieve my goal and even refresh (repaint) panel2. For that purpose an update method was created inside Panel2 class. All panel2 alterations were made there and then revalidate() and repaint() methods were called from update to refresh panel2.

Upvotes: 1

Views: 3008

Answers (1)

Austin Patel
Austin Patel

Reputation: 24

The problem is a refresh/repaint issue, but rather you never actually change the JLabel's text. Changing Data.text doesn't actually change the JLabel's text. You have to call the setText method of the JLabel. If you try printing out the text of the JLabel right after you change Data.text you will notice that the JLabel's text didn't change. Here is my solution:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class Repaint_Test
{
    public static void main(String args[]){
        Frame frame = new Frame();
    }
}

class Frame extends JFrame
{
    private static final long serialVersionUID = 1L;
    public Frame()  {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,250);
        this.setLocationRelativeTo(null);
        this.setLayout(new GridLayout(1,2));
        Panel1 p1 = new Panel1();
        Panel2 p2 = new Panel2();
        Data.panel2 = p2; // getting the reference of panel2    
        this.add(p1);
        this.add(p2);
        this.setVisible(true);
    }
}

class Panel1 extends JPanel
{
    private static final long serialVersionUID = 1L;
    public Panel1(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel1:"));
        JButton button = new JButton("Push");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Data.text = "text"; // changing commonly accessed data              
                Data.panel2.setBackground(Color.PINK); // this works
                //Data.panel2.invalidate();
                //Data.panel2.validate();
                //Data.panel2.updateUI();
                //Data.panel2.revalidate();
//                Data.panel2.repaint();                // this does not work
                Data.panel2.updateText(Data.text);
            }
        });
        this.add(button);
    }
}

class Panel2 extends JPanel
{
    private static final long serialVersionUID = 1L;
    private JLabel label;

    public Panel2(){
        this.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Panel2:"));

        label = new JLabel("Initial Text");
        add(label);
    }

    public void updateText(String text) {
        label.setText(text);
    }
}

class Data
{
    public static Panel2 panel2 = null;
    public static String text = "ooooo";
}

Upvotes: 1

Related Questions