PDA

View Full Version : Getting info in JTextFields?


cassio
03-02-2005, 04:06 PM
very new to Java and I have to process a sales transaction using JLabels, JTextFields, and JButtons....

I have this code so far, and I know the top half will not work as is Im just trying to pass the different parameters through the Textfields and convert the text to an integer so I can input them from the array.

import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;


public class USFStore
{
int [] Pcode = {100, 200, 300, 400, 500, 600};
String [] Iname = ("Basket Ball", "Base Ball", "Golf Ball", "Tennis Ball", "Foot Ball", "Soccer Ball"};
float [] Price = {"24.99", "5.99", "2.99", "8.98", "9.99", "19.99"};
float [] Tax = {".05", ".035", ".072", ".04", ".042", ".05"};



public class TextFieldFrame extends JFrame
{
private JTextField textField1; // text field
private JTextField textField2; // text field
private JButton button;
private JButton button2;
private JButton button3;
private String input;

// TextFieldFrame constructor adds JTextFields to JFrame
public TextFieldFrame()
{
super( "USF Campus Store" );
setLayout( new FlowLayout() ); // set frame layout


// construct textfield with 10 columns

textField1 = new JTextField( 5 );
add( textField1 ); // add textField1 to JFrame





// construct textfield with default text
textField2 = new JTextField( 5 );
add( textField2 ); // add textField2 to JFrame

button = new JButton("Next Item");
add(button);

button2 = new JButton("Receipt");
add(button2);

button3 = new JButton("Summary");
add(button3);


// register event handlers
TextFieldHandler handler = new TextFieldHandler();

button.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener (handler);

} // end TextFieldFrame constructor

// private inner class for event handling
private class TextFieldHandler implements ActionListener
{
// process textfield events
public void actionPerformed( ActionEvent event )
{
String string = ""; // declare string to display

// user pressed Enter in JTextField textField1
if ( event.getSource() == button)
{
input = textField1.getText() + "\n" ;
textField1 = Integer.parseInt(input);

string = string + textField2.getText() + "\n";


}

if ( event.getSource() == button2)
{
string = string + textField1.getText() + "\n" ;
string = string + textField2.getText() + "\n";
string = string + String.format( "test1");
}

if ( event.getSource() == button3)
{
string = string + textField1.getText() + "\n" ;
string = string + textField2.getText() + "\n";

}



JOptionPane.showMessageDialog( null, string );
} // end method actionPerformed
} // end private inner class TextFieldHandler
}


import javax.swing.JFrame;

public class TextFieldTest
{
public static void main( String args[] )
{
TextFieldFrame textFieldFrame = new TextFieldFrame();
textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
textFieldFrame.setSize( 325, 200 ); // set frame size
textFieldFrame.setVisible( true ); // display frame
} // end main
} // end class TextFieldTest



//thanks for any help, Ian

JPM
03-02-2005, 07:49 PM
The code has several errors, which the compiler should show if you try to compile it.

What exactly are you trying to do here?:
input = textField1.getText() + "\n" ;
textField1 = Integer.parseInt(input);

cassio
03-02-2005, 09:13 PM
i know there are several errors. I was talking to a friend, and he said to try to convert the text into an integer, i wasnt sure what he was saying, basically when I input the data into the JTextField i will have to have it stored, then run a few math commands... I put the top part in to show the arrays that I am using. I will spend some more time on it tonight, but any insight will be highly appreciated.

thanks,

Ian

JPM
03-03-2005, 03:54 PM
basically when I input the data into the JTextField i will have to have it stored, then run a few math commands

input = textField1.getText() + "\n" ;
int test = Integer.parseInt(input);

the test variable now holds the value of textField1 as an int value