having trouble with event handling and JTextField
I've got an assignment to write a java app that will convert celsius to fahrenheit (or reverse), and I'm having trouble getting my JTextField objects (FText and CText) being recognized by ActionHandler, and putting their input into integer variables so I can use them in the calculations (my first program with event handling and GUI's, I'm still absorbing a lot of information all at once)
The app is a box with a fahrenheit icon with "Fahrenheit" below it, then a JTextField to the left that receives the temperature, and the same setup for celsius. When a value is put in one JTextField and user presses enter, the converted value appears in the other box.
I wrote the professor two days ago about this, knowing that I may not have it figured out by the due date (midnight tonight). She wrote me back 3 hours ago and basically re wrote my whole setup. I'm new to this, but I want it to come out my way, if I wanted to I could have copied code from examples in the book and been done, I actually want to know how to do this and manipulate these objects like I want.
So I really appreciate any help, here's my code, yahoo answers will likely annhilate the formatting but it isn't too long, I hope it's clear enough what some of my nonsense lines of code mean.
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class TempConverter extends JFrame
{
public TempConverter()
{
super( "Convert Celsius to American!" );
setLayout( new FlowLayout() );
//Fahrenheit box
JTextField FText = new JTextField( 5 );
add( FText );
//Fahrenheit label
Icon FIcon = new ImageIcon( getClass().getResource( "FIcon.jpg" ) );
JLabel FLabel = new JLabel( "Fahrenheit", FIcon, SwingConstants.LEFT );
FLabel.setToolTipText( "Do something already!" );
FLabel.setVerticalTextPosition( SwingConstants.BOTTOM );
add( FLabel );
//Celcius box
JTextField CText = new JTextField( 5 );
add ( CText );
//Celsius label
Icon CIcon = new ImageIcon( getClass().getResource( "CIcon.jpg" ) );
JLabel CLabel = new JLabel( "Celsius", CIcon, SwingConstants.LEFT );
CLabel.setToolTipText( "Do something already!" );
CLabel.setVerticalTextPosition( SwingConstants.BOTTOM );
add( CLabel );
//handlers
TextFieldHandler handler = new TextFieldHandler();
FText.addActionListener( handler );
CText.addActionListener( handler );
}
}
class TextFieldHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
//if fahrenheit is known
if ( event.getSource() == FText )
String FString = event.getActionCommand();//retrieve input from FText and place in FString
int F = Integer.parseInt( FString );
int C = (5/9)*(F-32)
CText.setText( "%d", C );
//if celsius is known
if (event.getSource() == CText )
String CString = event.getActionCommand(); //retrieve input from CText and place in CString
int C = Integer.parseInt( CString );
int F = (C + 32)/(5/9);
event.getActionCommand();
FText.setText( "%d", F );
}
}
import javax.swing.JFrame;
public class TextFieldTest
{
public static void main( String args[] )
{
TempConverter tempFrame = new TempConverter();
tempFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
tempFrame.setSize( 350, 350 );
tempFrame.setVisible( true );
}
}
creating graph using the value of textfield in the same frame
hi all,
plz help me out from a task.i have a asignment to plot graph using the inputted value of user in the group of textfield in the same frame.it means when a user input data to the 1st textfield ,at the same time the graph starts its plotting in the next panel used for plotting using jfreechart.when the user go on inputting data the graph go on ploting at the same time and if i delete the data the graph also go on deleting simultaneously.
Thanks a lot
sarada