Results 1 to 14 of 14
  1. #1
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default input from JTextField

    Hello All!

    I am trying to figure out how to get input from a JTextField. Been trying to figure this out for days, but i do not have anyone to talk to about this. It is getting pretty frustrating, as i consider being able to do this to be rather fundamental.

    I thought i finally had it figured out when I found a problem i cant find an answer to. I searched "Parsing JTextFields" on this site, got 3 threads, but i could not find a similar problem. I searched through Javanotes 6.0, Section 6.6 -- Basic Components and still couldn't answer this issue. Heck i even went to my old text from back in college but nothing. Heck, i even resorted to hours of searching and reading the webz...

    I am trying to read input from a user in a JTextField. From what I understand, when the user presses enter after filling text into the field, an action is generated. I want to use that action as a queue to parse the data entered and then perform a calculation. I have not gone as far as to write the code for the calculation yet as that is the easy part. I am more focused on learning how to make a program interact with a person at this time.

    What i am stuck on... It seems that netbeans cannot find the textField1 in the Parser() method below. I highlighted the areas of possible interest in orange. I highlighted the variable the compiler is taking issue with finding in red. More text below this, along with error message.

    Java Code:
    package javaapplication2;
    
    //importing 
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FFXIVCraftingCalculator {
    
          private static class Rect extends JPanel {
          public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(new Color(58, 51, 147));
          g.fillRoundRect(10, 20, 400, 185, 20, 20);
          setVisible(true);
       }
       }  
    
    [COLOR="orange"]//make listener for textField's and comboBox's and assign action to it
       private class TextEntered implements ActionListener {
           public void actionPerformed(ActionEvent f) {
               Parse();
           }
       }[/COLOR]
       
    [COLOR="orange"]//Action method called in TextEntered listener
          public void Parse() {
          String r1 = [COLOR="red"]textField1[/COLOR].getText();
          int rank1 = Integer.parseInt(r1); 
    }[/COLOR]
    
    
    
    //making listener for Close button, and assigning action to it
       private static class ButtonHandler implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             System.exit(0);
          }
       }
       
     //beginning main  
       public static void main(String[] args) {
     
    //create text fields, positions them
         [COLOR="orange"] JTextField textField1 = new JTextField("0", 3);
          textField1.setBounds(130, 40, 60, 18); 
          TextEntered toParser = new TextEntered();
          textField1.addActionListener(toParser);[/COLOR]
    
          JTextField textField2 = new JTextField("0", 3);
          textField2.setBounds(130, 60, 60, 18); 
          
          JTextField textField3 = new JTextField("0", 3);
          textField3.setBounds(130, 80, 60, 18); 
          
          JTextField textField4 = new JTextField("0", 3);
          textField4.setBounds(130, 100, 60, 18); 
          
          JTextField textField5 = new JTextField("0", 3);
          textField5.setBounds(130, 120, 60, 18); 
          
          JTextField textField6 = new JTextField("0", 3);
          textField6.setBounds(130, 140, 60, 18); 
          
          JTextField textField7 = new JTextField("0", 3);
          textField7.setBounds(130, 160, 60, 18); 
          
          JTextField textField8 = new JTextField("0", 3);
          textField8.setBounds(130, 180, 60, 18); 
          
          
     //create close button object, assign listener, and set position/size
          JButton okButton = new JButton("Close");
          ButtonHandler listener = new ButtonHandler();
          okButton.addActionListener(listener);
          okButton.setBounds(200, 300, 100, 30);
          
     //text labels created, sized, positioned, color
          JLabel item1 = new JLabel("First Item: ");
          item1.setBounds(20, 40, 80, 18);
          item1.setForeground(new Color(236, 232, 133));
          
          JLabel item2 = new JLabel("Second Item: ");
          item2.setBounds(20, 60, 80, 18);
          item2.setForeground(new Color(236, 232, 133));
          
          JLabel item3 = new JLabel("Third Item: ");
          item3.setBounds(20, 80, 80, 18);
          item3.setForeground(new Color(236, 232, 133));
          
          JLabel item4 = new JLabel("Fourth Item: ");
          item4.setBounds(20, 100, 80, 18);
          item4.setForeground(new Color(236, 232, 133));
          
          JLabel item5 = new JLabel("Fifth Item: ");
          item5.setBounds(20, 120, 80, 18);
          item5.setForeground(new Color(236, 232, 133));
          
          JLabel item6 = new JLabel("Sixth Item: ");
          item6.setBounds(20, 140, 80, 18);
          item6.setForeground(new Color(236, 232, 133));
          
          JLabel item7 = new JLabel("Seventh Item: ");
          item7.setBounds(20, 160, 80, 18);
          item7.setForeground(new Color(236, 232, 133));
          
          JLabel item8 = new JLabel("Eighth Item: ");
          item8.setBounds(20, 180, 80, 18);
          item8.setForeground(new Color(236, 232, 133));
          
          JLabel itemRank = new JLabel("Item Rank");
          itemRank.setBounds(130, 20, 80, 18);
          itemRank.setForeground(new Color(236, 232, 133));
          
          JLabel Quality = new JLabel("Item Quality");
          Quality.setBounds(198, 20, 80, 18);
          Quality.setForeground(new Color(236, 232, 133));
          
     //drop boxes created, granted values, positioned, sized
          JComboBox dropBox1 = new JComboBox();
          dropBox1.addItem("NQ");
          dropBox1.addItem("HQ");
          dropBox1.addItem("HQ2");
          dropBox1.addItem("HQ3");
          dropBox1.setBounds(200, 40, 60, 18);
          
          JComboBox dropBox2 = new JComboBox();
          dropBox2.addItem("NQ");
          dropBox2.addItem("HQ");
          dropBox2.addItem("HQ2");
          dropBox2.addItem("HQ3");
          dropBox2.setBounds(200, 60, 60, 18);
          
          JComboBox dropBox3 = new JComboBox();
          dropBox3.addItem("NQ");
          dropBox3.addItem("HQ");
          dropBox3.addItem("HQ2");
          dropBox3.addItem("HQ3");
          dropBox3.setBounds(200, 80, 60, 18);
          
          JComboBox dropBox4 = new JComboBox();
          dropBox4.addItem("NQ");
          dropBox4.addItem("HQ");
          dropBox4.addItem("HQ2");
          dropBox4.addItem("HQ3");
          dropBox4.setBounds(200, 100, 60, 18);
          
          JComboBox dropBox5 = new JComboBox();
          dropBox5.addItem("NQ");
          dropBox5.addItem("HQ");
          dropBox5.addItem("HQ2");
          dropBox5.addItem("HQ3");
          dropBox5.setBounds(200, 120, 60, 18);
          
          JComboBox dropBox6 = new JComboBox();
          dropBox6.addItem("NQ");
          dropBox6.addItem("HQ");
          dropBox6.addItem("HQ2");
          dropBox6.addItem("HQ3");
          dropBox6.setBounds(200, 140, 60, 18);
          
          JComboBox dropBox7 = new JComboBox();
          dropBox7.addItem("NQ");
          dropBox7.addItem("HQ");
          dropBox7.addItem("HQ2");
          dropBox7.addItem("HQ3");
          dropBox7.setBounds(200, 160, 60, 18);
          
          JComboBox dropBox8 = new JComboBox();
          dropBox8.addItem("NQ");
          dropBox8.addItem("HQ");
          dropBox8.addItem("HQ2");
          dropBox8.addItem("HQ3");
          dropBox8.setBounds(200, 180, 60, 18);
         
    /*Created Object of type rect, rect expands JPanel to add 
    the color/size/position of the rounded rectangle*/
          Rect content = new Rect();
          
    //set layout manager of the content object to null     
          content.setLayout(null); 
          
    //add GUI elements to content object
          content.setBackground(new Color(0, 0, 0));
          content.add(okButton);
          content.add(textField1);
          content.add(textField2);
          content.add(textField3);
          content.add(textField4);
          content.add(textField5);
          content.add(textField6);
          content.add(textField7);
          content.add(textField8);
          content.add(dropBox1);
          content.add(dropBox2);
          content.add(dropBox3);
          content.add(dropBox4);
          content.add(dropBox5);
          content.add(dropBox6);
          content.add(dropBox7);
          content.add(dropBox8);
          content.add(item1);
          content.add(item2);
          content.add(item3);
          content.add(item4);
          content.add(item5);
          content.add(item6);
          content.add(item7);
          content.add(item8);
          content.add(itemRank);
          content.add(Quality);
          
    //create jframe with title
          JFrame window = new JFrame("FFXIV Crafting Calculator");
    //set the content pane to contain the content object
          window.setContentPane(content);
          window.setSize(500,400);
          window.setLocation(100,100);
          window.setVisible(true);
          
       }//end Main
       
    }//end Class
    Figuring this out will be a major step in a positive direction. Ill be able to practice if many times in this application as i intend to do this for all the comboBoxes and JTextFields to gather the data needed for the calculation.




    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: textField1.getText
    at javaapplication2.FFXIVCraftingCalculator.Parse(FFX IVCraftingCalculator.java:28)
    at javaapplication2.FFXIVCraftingCalculator$TextEnter ed.actionPerformed(FFXIVCraftingCalculator.java:22 )
    at javax.swing.JTextField.fireActionPerformed(JTextFi eld.java:492)
    at javax.swing.JTextField.postActionEvent(JTextField. java:705)
    at javax.swing.JTextField$NotifyAction.actionPerforme d(JTextField.java:820)
    at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1639)
    at javax.swing.JComponent.processKeyBinding(JComponen t.java:2851)
    at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2886)
    at javax.swing.JComponent.processKeyEvent(JComponent. java:2814)
    at java.awt.Component.processEvent(Component.java:606 5)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1850)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:712)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:990)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:855)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:676)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4523)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    One thing to note about these errors. The only occur when i type a value into textField1 AND press enter after. Then the error message occurs.

    Thank you for taking the time to look this over.
    Last edited by Willriker; 08-12-2011 at 06:00 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    The most common way is to have a button and add an ActionListener to the button. When the user clicks the button you have the actionPerformed method retrieve the data from the textfield.

  3. #3
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    I do not know if i understand you correctly.

    I tried to place the:
    String r1 = textField1.getText();

    line into the TextEntered listener. and it still is giving me those little red squiggelies under the textField1. So, i am still confounded as to why it isnt seeing it.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    When you hover your mouse over the red line what does it say? Copy and paste the message here.

  5. #5
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Cannot find Symbol

    symbol: variable textField1
    location: class javaapplication2.FFXIVCraftingCalculator
    ----
    alt enter to show hints

    after pressing alt enter, the tips were three options:

    Create class "textField1" in package javaapplication2
    Create class "textField1" in javaapplication2.FFXIVCraftingCalculator
    Create Field textField1 in javaapplication2.FFXIVCraftingCalculator

    Which is part of the reason why i am so confused. Because there is a textField1 in javaapplication2.CraftingCalculator already. Unless i am missinterpreting it. It is one of the first things done in the main above.
    Last edited by Willriker; 08-12-2011 at 07:13 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Learn to use
    1) Arrays
    2) Meaningful variable names
    3) Layout Managers
    4) Inner classes

    And finally, to get better help sooner, learn to post an SSCCE that isn't cluttered with code totally irrelevant to the problem.

    db

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    It seems that you have declared all your variables in the main method and not your class.

  8. #8
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    Learn to use
    1) Arrays
    2) Meaningful variable names
    3) Layout Managers
    4) Inner classes


    And finally, to get better help sooner, learn to post an SSCCE that isn't cluttered with code totally irrelevant to the problem.

    db

    Although i have done considerable reading on Java... reading and doing are two entirely different things. How would arrays help with GUI issues? I haven't read up on arrays being used in this topic. That's a new concept to me.


    Quote Originally Posted by Junky View Post
    It seems that you have declared all your variables in the main method and not your class.
    You advise moving all of my components outside of the main? I have seen people use an Init() method for all of the components. Do you think that would resolve this issue?
    Last edited by Willriker; 08-12-2011 at 07:55 AM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Try it and see.

  10. #10
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Thank you all for the advice. I took time out and learned enough of the Netbeans GUI builder to get this working for me. Well, it works when i run it out of Netbeans. So, i know that it is now both correct and i made it robust with try/catch stuff for when the user enters anything other than numerical values into the jtextfields.

    And its all thanks to you guys for pointing me into the correct direction!

    My only issue now is that the Applet is of .java instead of .class. From what I am reading, in order to bring it into a website, it needs to be of type .class. How is that accomplished?
    Last edited by Willriker; 08-13-2011 at 10:30 PM.

  11. #11
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Figured it out, i think i uploaded the wrong document to the website... But still i am getting an error message.

    Java Plug-in 1.6.0_26
    Using JRE version 1.6.0_26-b03 Java HotSpot(TM) Client VM
    User home directory = C:\Users\Jon
    ----------------------------------------------------
    c: clear console window
    f: finalize objects on finalization queue
    g: garbage collect
    h: display this help message
    l: dump classloader list
    m: print memory usage
    o: trigger logging
    q: hide console
    r: reload policy configuration
    s: dump system and deployment properties
    t: dump thread list
    v: dump thread stack
    x: clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------


    load: class FFXIVCraftingCalculatorJApplet.class not found.
    java.lang.ClassNotFoundException: FFXIVCraftingCalculatorJApplet.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.ClassNotFoundException: FFXIVCraftingCalculatorJApplet.class

    This is what i wrote in the HTML editor they provided:
    Java Code:
    <html>
    <body>
    <p align=center>
    <applet code="FFXIVCraftingCalculatorJApplet.class" codebase="http://files.enjin.com/65434/Applets/FFXIVCraftingCalculatorJApplet.class"  height=300 width=610>
    </applet>
    </p>
    </body>
    </html>

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

  13. #13
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    In netbeans, it is in package name FFXIV. The java file is FFXIVCraftingCalculatorJApplet.java. I am using a JPG file as an image. It is in the same package and is named BlankMenu.JPG.

    It works in netbeans. But, when i placed the FFXIVCraftingCalculatorJApplet.class file (and the JPG) into the website directory, and used that HTML code i posted, i get that error message.

    BTW, i abandoned that code in the OP and used the Netbeans GUI Builder instead.
    Last edited by Willriker; 08-14-2011 at 12:37 AM.

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

    Default

    Quote Originally Posted by Willriker View Post
    In netbeans, it is in package name FFXIV. The java file is FFXIVCraftingCalculatorJApplet.java. I am using a JPG file as an image. It is in the same package and is named BlankMenu.JPG.
    If the code's package name is FFXIV then the fully qualified class name should be (I believe): FFXIV.FFXIVCraftingCalculatorJApplet, and you should leave the ".class" extension off. Something like:

    Java Code:
    <applet code="FFXIV.FFXIVCraftingCalculatorJApplet" ... etc ...
    I'm pretty sure your codebase is not correct either and you'd better double check on what should be appropriate.

Similar Threads

  1. Allow only numerical input on JTextField
    By patriotsfan in forum AWT / Swing
    Replies: 3
    Last Post: 05-31-2011, 12:39 PM
  2. Dynamically input text JTextField. Is it possible?
    By africanhacker in forum New To Java
    Replies: 2
    Last Post: 03-21-2011, 04:23 PM
  3. JTextfield renderer/editor input map
    By mine0926 in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 06:26 AM
  4. Convert Input from JTextField to int?
    By jls7168 in forum New To Java
    Replies: 7
    Last Post: 02-20-2009, 02:29 AM
  5. Constraining Input in JTextField
    By kataya in forum AWT / Swing
    Replies: 1
    Last Post: 06-26-2008, 06:25 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
  •