Results 1 to 6 of 6
- 03-13-2009, 12:44 AM #1
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.
public class TempConverter extends JFrame
super( "Convert Celsius to American!" );
setLayout( new FlowLayout() );
JTextField FText = new JTextField( 5 );
add( FText );
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 );
JTextField CText = new JTextField( 5 );
add ( CText );
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 );
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);
FText.setText( "%d", F );
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 );
- 03-13-2009, 01:06 AM #2
o'boy, alot of things to cover.
1) use code tags.
2) use braces for your if statements.
3) you can't do this: if (event.getSource() == CText )
because its not the same class.
so you'll have to use it as an inner class or make TempConverter implement ActionListener.
also make CText and FText a class field.
you also need to use a different layout manager.
more info here: The Java™ Tutorials
Last edited by angryboy; 03-13-2009 at 01:13 AM.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-13-2009, 01:57 AM #3
oh, sorry about the code braces.
And yea, I wanted to use TextFieldHandler as an inner class, but it doesn't let me declare it private (which would make it an inner class right?). I get the error 'modifier private not allowed here.
FlowLayout is the only layout manager my book has shown me so far, could you suggest a better one if that one won't work? And I realized I was missing braces after I posted haha. I also don't understand what you mean about making CText and FText a class field.
So I still need don't know how to declare TextFieldHandler private, make CText and FText a class field, and find a new layout manager (I guess).
FlowLayout is the only layout manager my book has shown me so far, could you suggest a better one if that one won't work?
Best of luck.
- 03-13-2009, 03:08 AM #5
Got it running!
I was completely closing off my first class and then declaring my private class, so thats why i was getting errors.
Thanks a lot for all the input, I've been in a Java class writing text based apps for the last semester and a half, she threw GUI's and event handling at us all at once, I'm still in the process of absorbing it all haha.
- 06-01-2009, 02:19 PM #6Member
- Join Date
- Jan 2009
- Rep Power
creating graph using the value of textfield in the same frame
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
- By simo_mon in forum New To JavaReplies: 2Last Post: 02-04-2009, 02:11 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM
- By smajidali26 in forum AWT / SwingReplies: 1Last Post: 11-29-2007, 05:57 PM
- By ahdus in forum Java AppletsReplies: 1Last Post: 11-17-2007, 06:24 PM
- By luisarca in forum Sun Java Wireless ToolkitReplies: 5Last Post: 05-07-2007, 06:05 PM