web_starter
web_starter

Reputation: 87

Java JTabbedPane, update others tab JLabel value?

I have 2 JTabbedPane. I am unable to refresh the data. PLease help, here is my code:

pane1:

//.. some codes...
// This is the ButtonListener
private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent event)
    {
      userInput = tf.getText(); // tf is JTextField
      //System.out.println("the input is "+ finalInput);
      pane2.updateData(userInput);
    }
} 

pane2:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
}   

Here is my main class:

import java.awt.*;
import javax.swing.*;

public class Main {
public static Pane2 p2 = new Pane2();
    public static void main(String[] args) {

        JFrame f= new JFrame ("My Frame");
        f.setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE);

        JTabbedPane tp = new JTabbedPane();
        p2 = new Pane2();

        tp.addTab("Pane1", new PaneFirst(p2));
        tp.addTab("Pane2", new PaneSecond());

        f.add(tp);
        f.pack();
        f.setVisible(true);
   }
}

The labelUser never updates, but I trace the updateData function, its being called. Why is the text in labelUser not being updated?

EDIT:

"labelUser" come from pane2.java class.

Upvotes: 2

Views: 2108

Answers (2)

jjnguy
jjnguy

Reputation: 138982

Note: Apparently this didn't fix the problem.

One thing to try would be:

public void updateData(String s){
    System.out.println("Update data function is called");
    labelUser.setFont(new Font("Arial", Font.BOLD, 30));
    labelUser.setText("Updated text here " + s);
    repaint(); // add this line to tell your pane to repaint itself
}  

There is a chance that your panel is just not getting repainted.

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114847

Might be a typo but - in actionPerformed() you store the content of the textfield in userInput but use finalInput to update pane2.

Upvotes: 0

Related Questions