Results 1 to 6 of 6
  1. #1
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default 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 );
    }
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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.

    edit:
    also make CText and FText a class field.

    edit2:
    you also need to use a different layout manager.

    more info here: The Java™ Tutorials
    Last edited by angryboy; 03-13-2009 at 02:13 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    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).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by v1nsai View Post
    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?).
    Wrong. To be an "inner" class, it needs to be inside of the outer class. Make sense?

    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?
    You can find out possible other layouts yourself by going to the Sun Layout Manger Tutorial.

    Best of luck.

  5. #5
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    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.

  6. #6
    saradap80 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Smile 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

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2009, 03:11 AM
  2. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 01:21 PM
  3. Combo Box Event handling
    By smajidali26 in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 06:57 PM
  4. problem with event handling!!!
    By ahdus in forum Java Applets
    Replies: 1
    Last Post: 11-17-2007, 07:24 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •