Click to See Complete Forum and Search --> : Cannot access text from JTextFields


Paddy Notemaker
06-24-2005, 08:19 AM
Dear All,

I have been having some difficulty with a program that I applied a SpringLayout to and restructured. To apply the SpringLayout, I shifted my code from the public <classname> to the createAndShowGUI() method and then back again, as I thought that the problem might be something to do with accessing variables from static/non static contexts. For some reason, I cannot access the data from the text fields when I press the button. Also, if anyone knows of free programs that help with visual layout it would be great, as I now appreciate that this can take a lot longer with code. Here is the code that I have been having difficulty with. I will indicate the area where I cannot access the data from the text fields and the place where I set the text fields up.

import javax.swing.SpringLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class SpringGraphCalcBackup extends JPanel implements ActionListener
{
int field1=0;
int field2=0;
int field3=0;
int field4=0;
int field5=0;

public SpringGraphCalcBackup()
{
SpringGraphCalcBackup contentPane = new SpringGraphCalcBackup();
contentPane.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);
JLabel label1 = new JLabel("a");
JLabel label2 = new JLabel("b");
label2.setHorizontalTextPosition(JTextField.CENTER);
JLabel label3 = new JLabel("c");
label3.setHorizontalTextPosition(JTextField.TRAILING);
JLabel label4 = new JLabel("xparam 1");
JLabel label5 = new JLabel("xparam 2");
JTextField text4 = new JTextField(6); //These
JTextField text5 = new JTextField(6); //are the
JTextField text1 = new JTextField(6); //text fields
JTextField text2 = new JTextField(6); //that I am having
JTextField text3 = new JTextField(6); //difficulty with.JButton calculatebutton = new JButton("Update Graphics");
calculatebutton.setVerticalTextPosition(AbstractButton.CENTER);
calculatebutton.setHorizontalTextPosition(AbstractButton.LEADING);
calculatebutton.setMnemonic(KeyEvent.VK_U);
calculatebutton.setActionCommand("update");
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(text1);
contentPane.add(text2);
contentPane.add(text3);
contentPane.add(text4);
contentPane.add(text5);
contentPane.add(label4);
contentPane.add(label5);
contentPane.add(calculatebutton);
layout.putConstraint(SpringLayout.WEST,label1,20,SpringLayout.WEST,contentPane);
layout.putConstraint(SpringLayout.NORTH,label1,20,SpringLayout.NORTH,contentPane);
layout.putConstraint(SpringLayout.NORTH,text1,20,SpringLayout.NORTH,contentPane);
layout.putConstraint(SpringLayout.WEST,text1,20,SpringLayout.EAST,label1);
layout.putConstraint(SpringLayout.NORTH,label2,0,SpringLayout.NORTH,text2);
layout.putConstraint(SpringLayout.WEST,label2,0,SpringLayout.WEST,label1);
layout.putConstraint(SpringLayout.NORTH,text2,10,SpringLayout.SOUTH,text1);
layout.putConstraint(SpringLayout.WEST,text2,0,SpringLayout.WEST,text1);
layout.putConstraint(SpringLayout.WEST,label3,0,SpringLayout.WEST,label1);
layout.putConstraint(SpringLayout.NORTH,label3,10,SpringLayout.SOUTH,text2);
layout.putConstraint(SpringLayout.WEST,text3,0,SpringLayout.WEST,text1);
layout.putConstraint(SpringLayout.NORTH,text3,0,SpringLayout.NORTH,label3);
layout.putConstraint(SpringLayout.WEST,calculatebutton,8,SpringLayout.EAST,text1);
layout.putConstraint(SpringLayout.NORTH,calculatebutton,0,SpringLayout.NORTH,text1);
layout.putConstraint(SpringLayout.WEST,label4,5,SpringLayout.EAST,text2);
layout.putConstraint(SpringLayout.NORTH,label4,0,SpringLayout.NORTH,label2);
layout.putConstraint(SpringLayout.WEST,text4,10,SpringLayout.EAST,label4);
layout.putConstraint(SpringLayout.NORTH,text4,0,SpringLayout.NORTH,label4);
layout.putConstraint(SpringLayout.WEST,label5,0,SpringLayout.WEST,label4);
layout.putConstraint(SpringLayout.NORTH,label5,0,SpringLayout.NORTH,label3);
layout.putConstraint(SpringLayout.NORTH,text5,0,SpringLayout.NORTH,text3);
layout.putConstraint(SpringLayout.WEST,text5,0,SpringLayout.WEST,text4);
}

public void actionPerformed(ActionEvent e)
{
if("update".equals(e.getActionCommand()))
{
field1 = Integer.parseInt(text1.getText()); //For some
field2 = Integer.parseInt(text2.getText()); //reason, I
field3 = Integer.parseInt(text3.getText()); //cannot access
field4 = Integer.parseInt(text4.getText()); //the data from
field5 = Integer.parseInt(text5.getText()); //the text fields. }
else
{
//do nothing
}
}
public static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GraphCalc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.pack();
frame.setSize(new Dimension(300, 400));
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}


Thanks

buntine
06-24-2005, 09:08 AM
Is the actionPerformed method being executed at all? Where have you set the event listener for your JButton?

Regards.

Paddy Notemaker
06-25-2005, 04:54 AM
Do you see the actionPerformed method title just above the bit highlighted in red? I want the program to retrieve the values from the text fields when the button is pressed. Thanks for your help buntine.

PS-Nice site!

buntine
06-25-2005, 05:14 AM
It's been a while since I last played around with Swing, but don't you actually have to physically set the EventListener?

text1.addActionListener(this);

Thanks for the nice comment. ;)

Regards.