views:

80

answers:

2

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.

+1  A: 

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.

jjnguy
`setText()` should force a repaint eventually :-/
Aaron Digulla
@Aaron, yeah. I know. But, it was all I could think of...
jjnguy
Its not working, any others ideas?
web_starter
@web, Unfortunately, no.
jjnguy
For more logging, add "s" to your System.out.println:System.out.println("Update data function is called with text: " + s);And debug.
amorfis
@amorfis: It print out the "s", i mean the variable, but it just dun update the label. I am fuckking stress!!
web_starter
Guys, please help!!
web_starter
@web_starter - could you show us the code where the second pane is created? Or just double-check that you *really* add `labelUser` to the pane and don't have something like `pane.add(new Label("test"));`
Andreas_D
Like Andreas, I think you are updating the wrong label. Probably it is not the one added to pane2.
amorfis
I can 100% assure, I updating the correct label, why? I do a test, i create a button in pane2, when triggered I call the same function updateData(), its update my labelUser. I amvery, very very sure, I am updating the correct label.. =(
web_starter
@web, so the label updates when the button is pressed from the same pane as the label, it updates, but when the button is pressed from a different pane, it doesn't update?
jjnguy
yes sire justin. I have been fixing this for 2 days, search through the web, no solution...= (
web_starter
Could you post your whole code for both panes and main method somewhere and link here? This is getting more and more interesting :)
amorfis
A: 

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

Andreas_D
Its not typo. If typo, I wont go through the compiler, I will edit the question, change the finalInout to userInput
web_starter