How to setText from tab1's JTextField to tab2's JTextField?

Hi, How to setText from tab1's JTextField to tab2's JTextField, anyone can help me? When I press a button, it cannot pass the tab1's txt1.getText() to tab2's txt2.setText(). I have a 3 simple class that show below:

** Tabmain.java **

===================================================

import java.awt.*;

import java.awt.event.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.*;

public class Tabmain extends JFrame{

JTabbedPane tab;

JPanel panel1, panel2;

JTextField txt1,txt2;

JButton jb;

public Tabmain(){

super();

addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e)

{System.exit(0);}});

getContentPane().setLayout(new BorderLayout());

tab = new JTabbedPane();

tab.add("Tab1",new Tab1());

tab.add("Tab2",new Tab2());

getContentPane().add(tab,BorderLayout.CENTER);

this.setSize(500,400);

this.setVisible(true);

}

public static void main(String args[]){

Tabmain tabmain = new Tabmain();

}

}

** Tab1.java **

=======================================================

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Tab1 extends JPanel{

private JTextField txt1;

private JButton jb;

private JPanel panel1;

public Tab1(){

super();

this.setLayout(new BorderLayout());

this.add(getPanel1());

}

public JPanel getPanel1(){

panel1 = new JPanel();

panel1.setLayout(new BorderLayout());

txt1 = new JTextField();

txt1.setBounds(new Rectangle(10, 10, 100, 12));

panel1.add(txt1,BorderLayout.NORTH);

jb = new JButton("OK");

jb.addActionListener(new Action());

panel1.add(jb,BorderLayout.SOUTH);

return panel1;

}

class Action implements ActionListener{

public void actionPerformed(ActionEvent event){

String jbtxt = ((JButton)event.getSource()).getText();

if(jbtxt=="OK")

{

String str = txt1.getText();

Tab2 tab2 = new Tab2();

tab2.txt2.setText(str);

}

}

}

}

** Tab2.java **

======================================================

import java.awt.*;

import javax.swing.*;

public class Tab2 extends JPanel{

public JTextField txt2;

private JPanel panel2;

public Tab2(){

super();

this.setLayout(new BorderLayout());

this.add(getPanel2());

}

public JPanel getPanel2(){

panel2 = new JPanel();

panel2.setLayout(new BorderLayout());

txt2 = new JTextField();

txt2.setBounds(new Rectangle(10, 10, 100, 12));

panel2.add(txt2,BorderLayout.NORTH);

return panel2;

}

public void setTXT(String str){

txt2.setText(str);

}

}

[3000 byte] By [jiong_ronga] at [2007-11-27 9:10:17]
# 1

if you want the text to be the same, set the documents the same

tab = new JTabbedPane();

Tab1 t1 = new Tab1();

Tab2 t2 = new Tab2();

t2.txt2.setDocument(t1.txt1.getDocument());

tab.add("Tab1",t1);

tab.add("Tab2",t2);

and change txt1's accessor to public

Michael_Dunna at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanx, micheal_dunn, it works!Then, when i click the Tab1's Jbutton, how to switch from tab1 to tab2?Thank You.
jiong_ronga at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

In the ActionPerformed event of the button call

SwingUtilities.invokeLater(new Runnable() {

public void run() {

jTabbedPane.setSelectedIndex(int i);

}

});

c0demonk3ya at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 4
c0demonk3y , it can't work.....
jiong_ronga at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 5
If I click the tab1's JButton, how can make the button's action performed events to switch change to tab2?
jiong_ronga at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 6

in actionPerformed

((JTabbedPane)Tab1.this.getParent()).setSelectedIndex(1);

but this will break if you add Tab1 to another panel, then add otherPanel to the tabbedPane

doing it by passing a reference of the tabbedPane to Tab1 might be better

//Tab1 t1 = new Tab1();

Tab1 t1 = new Tab1(tab);

public Tab1(){

super();

this.setLayout(new BorderLayout());

this.add(getPanel1());

}

change to

public Tab1(final JTabbedPane tp){

super();

this.setLayout(new BorderLayout());

this.add(getPanel1());

jb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

tp.setSelectedIndex(1);

}

});

}

Michael_Dunna at 2007-7-12 21:51:27 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thank You very much to Micheal_Dunn, thank for ur helping, it works.
jiong_ronga at 2007-7-12 21:51:28 > top of Java-index,Desktop,Core GUI APIs...
# 8
hey jiong_rong....i was trying your code as reference example....but am getting error--> Exception in thread main NoSuchMethodError for both Tab1 and Tab2 ....did you get similar error?
ElishaWa at 2007-7-12 21:51:28 > top of Java-index,Desktop,Core GUI APIs...