Reputation: 111
I've got a problem with JTextField. I don't know how to pass a variable from a JTextField (situated in JFrame A) to another JTextField (situated in JFrame B). I tried to do that, but it doesn't do anything, i.e. it doesn't get any runtime/compilation error nor it receives the text.
I tried to do this in ClassB:
ClassA a = new ClassA();
String text = a.jtextfield1.getText();
but it doesn't work!!
Could you help me, may with a simple example? What did I do wrong?
p.s.: i'm using netbeans
[edit --] That's the code of ClassA:
public class ClassA extends javax.swing.JFrame {
public ClassA() {
initComponents();
}
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("Some text blah blah");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(86, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassA().setVisible(true);
}
});
}
public javax.swing.JTextField jTextField1;
}
Here's the code of ClassB:
public class ClassB extends javax.swing.JFrame {
public ClassB() {
initComponents();
}
public ClassA a = new ClassA();
public void initComponents() {
jTextField1 = new javax.swing.JTextField();
getText = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getText.setText("GetText");
getText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
getTextActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(getText)))
.addContainerGap(143, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(getText)
.addContainerGap(49, Short.MAX_VALUE))
);
pack();
}
public void getTextActionPerformed(java.awt.event.ActionEvent evt) {
a.jTextField1.getText(); //this doesn't work. How can I it makes work?
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassB().setVisible(true);
}
});
}
public javax.swing.JButton getText;
public javax.swing.JTextField jTextField1;
}
Thanks in advance.
Upvotes: 0
Views: 4396
Reputation: 36621
You ask for a simple example, you will get one. I leave it up to you how to match this example on your code, which shouldn't be too difficult.
public class PanelWithTextField extends JPanel{
private JTextField textField = new JTextField();
public JTextField getTextField(){ return textField; };
}
public static void main( String[] args ){
EventQueue.invokeLater(){ new Runnable(){
public void run(){
//create a first panel
PanelWithTextField panel = new PanelWithTextField();
panel.getTextField().setText( "Some text" );
//create a second panel
PanelWithTextField anotherPanel = new PanelWithTextField();
//copy the text from the first panel's textfield to the second panel's textfield
anotherPanel.getTextField().setText( panel.getTextField().getText() );
}
}
}
I hope I did not make too much typo's in it, as I did not try to run it nor did I used my IDE to write this code.
Basically you will need a reference to your first panel in your second panel (or in any part of the code where you which to access that textfield).
And as already suggested by others. You should start by making sure you understand the basic OO concepts and basic Java syntax before you start messing around with Swing and UI's
Upvotes: 1
Reputation: 692121
If you instantiate a new ClassA instance (and, BTW, the syntax is new ClassA()
), then you will have... a new instance, with another jtextfield1 than the one in the existing ClassA instance. You need to pass a reference to the existing ClassA instance to the ClassB instance:
When ClassB is constructed:
ClassB theClassB = new ClassB(theClassA);
You don't seem to master the basic OO concepts of classes and objects, and not even the syntax of Java. I would advise not using Swing now, and learning the basics first. Swing is hard, much too hard for a developer which doesn't understand these concepts.
Read the basics and OO lessons of the Java tutorial
Upvotes: 1