Results 1 to 20 of 20
  1. #1
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default GUI's and inputting doubles or ints

    hey, im working on a basic gui program and i need to input some numbers from the interface so i can calculate their average.

    So far my program is working, except for that part

    Java Code:
    import javax.swing.*; // Required library
    import java.awt.*; // Required library
    import java.awt.event.*; // Required library
    public class MyGUI extends JFrame {
      private JTextField myExam = new JTextField(""); // Create a text field
      private JLabel labelWithExam = new JLabel("Exam avg");
      private JTextField myHW = new JTextField(""); // Create a text field
      private JLabel labelWithHW = new JLabel("HW avg");
      private JTextField myLab = new JTextField(""); // Create a text field
      private JLabel labelWithLab = new JLabel("Lab Avg");
      private JTextField myQuiz = new JTextField(""); // Create a text field
      private JLabel labelWithQuiz = new JLabel("Quiz Avg");
      private JButton myButton = new JButton("Calculate"); // Create a new button
    // Constructor
    public MyGUI() {
      setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid
      add(labelWithExam);
      add(myExam);
      add(labelWithHW);
      add(myHW);
      add(labelWithLab);
       add(myLab);
       add(labelWithQuiz);
      add(myQuiz);
    add(myButton); // Make button appear in window
    
    myButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String ex = myExam.getText(); // Get the text in the TextField
    String h = myHW.getText(); // Get the text in the TextField
    String l = myLab.getText(); // Get the text in the TextField
    String q = myQuiz.getText(); // Get the text in the TextField

  2. #2
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    im entering numbers for exam, hw, lab and quiz in the interface, but I cant use them because their strings...and im not sure how to input them rather as double types

    Thanks

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    Double.parseDouble(...);
    My Hobby Project: LegacyClone

  4. #4
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    heyy, thanks

    Java Code:
    String ex = myExam.getText(); // Get the text in the TextField
    Double.parseDouble(ex);
    String h = myHW.getText(); // Get the text in the TextField
    Double.parseDouble(h);
    String l = myLab.getText(); // Get the text in the TextField
    Double.parseDouble(l);
    String q = myQuiz.getText(); // Get the text in the TextField
    Double.parseDouble(q);
    double avg = 0;
    avg = ex + h + l + q;
    System.out.println(avg);
    but its still not working. im getting incompatible types as an error.
    Anything else that could fix it?

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

    Default

    You need to read the Double API as that's not how you use this method. It cannot change the String that is given into it magically into a double (nothing can). Rather it returns a double value that you must place into a double variable. In other words ex, h, l, and q are still Strings. So instead do this:

    Java Code:
    String ex = myExam.getText();
    double examGrade = Double.parseDouble(ex);
    or perhaps even better:
    Java Code:
    double examGrade = Double.parseDouble(myExam.getText());

  6. #6
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    thanks so much for your help...i have one more question though.
    after i input my numbers and calculate the average, i want to press the calculate button and have the average appear on the screen as text next to the button. I'm trying:

    Java Code:
    private JLabel labelWithButton = new JLabel(avg);
    
    ...
    ...
    
    
    add(labelWithButton);

  7. #7
    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 lopder1 View Post
    thanks so much for your help...i have one more question though.
    after i input my numbers and calculate the average, i want to press the calculate button and have the average appear on the screen as text next to the button. I'm trying:

    Java Code:
    private JLabel labelWithButton = new JLabel(avg);
    ...
    ...
    
    add(labelWithButton);
    OK. Well, good luck with this.

  8. #8
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    well its not working...got any tips?

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

    Default

    How exactly is it not working? Sorry, but we really can't do anything to help you without more information.

    One suggestion is to have the JLabel present in your app from the start, but with no text. Then when you get a solution, don't add a new JLabel, just set the text of the already existing label. Note that it must have class scope -- it must be visible throughout the class.

  10. #10
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    well, the program is designed to calculate avg. when the gui pops up, i enter the four grades for the four components. then after i press the calculate button, the avg should appear next to it. however thats not working.

    Java Code:
    import javax.swing.*; // Required library
    import java.awt.*; // Required library
    import java.awt.event.*; // Required library
    public class MyGUI extends JFrame {
      private JTextField myExam = new JTextField(""); // Create a text field
      private JLabel labelWithExam = new JLabel("Exam avg");
      private JTextField myHW = new JTextField(""); // Create a text field
      private JLabel labelWithHW = new JLabel("HW avg");
      private JTextField myLab = new JTextField(""); // Create a text field
      private JLabel labelWithLab = new JLabel("Lab Avg");
      private JTextField myQuiz = new JTextField(""); // Create a text field
      private JLabel labelWithQuiz = new JLabel("Quiz Avg");
      private JButton myButton = new JButton("Calculate"); // Create a new button
      private JLabel labelWithButton = new JLabel(avg);
      
    // Constructor
    public MyGUI() {
      setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid
      add(labelWithExam);
      add(myExam);
      add(labelWithHW);
      add(myHW);
      add(labelWithLab);
       add(myLab);
       add(labelWithQuiz);
      add(myQuiz);
    add(myButton); // Make button appear in window
    add(labelWithButton);
    
    myButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String ex = myExam.getText(); // Get the text in the TextField
    double examGrade = Double.parseDouble(ex);
    String h = myHW.getText(); // Get the text in the TextField
    double hwGrade = Double.parseDouble(h);
    String l = myLab.getText(); // Get the text in the TextField
    double labGrade = Double.parseDouble(l);
    String q = myQuiz.getText(); // Get the text in the TextField
    double quizGrade = Double.parseDouble(q);
    double avg = 0;
    avg = examGrade*.56 + hwGrade*.22 + labGrade*.17 + quizGrade*.05;
    
    }
    });
    
    }
    i also have the main method but its not important. everything is working, just not the part where i click the button and the average prints out next to it on the gui

  11. #11
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    theres a problem with this line:

    private JLabel labelWithButton = new JLabel(avg);

  12. #12
    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 lopder1 View Post
    theres a problem with this line:

    private JLabel labelWithButton = new JLabel(avg);
    Sorry to sound like a broken record, but what problem? Are you getting an error at compile time or an exception at run time? If so, please post the error. Have you tried my suggestion: creating the JLabel at start up but not giving it text until the button has been pressed?

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

    Default

    Oh I see what you are doing. You're trying to put a non-existent value into the JLabel. don't do that but instead just leave it blank:

    Java Code:
       // private JLabel labelWithButton = new JLabel(avg);
       private JLabel labelWithButton = new JLabel();

  14. #14
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    but if i do that then the average will not be printed after i press the calculate button

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

    Default

    You have to tell it to do that in your actionPerformed method by setting the label's text as I mentioned in post 9: you must add the text when you get a solution. You only get the solution within the actionPerformed method.

  16. #16
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    so basically move:

    private JLabel labelWithButton = new JLabel(avg);

    to the actionPerformed method

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

    Default

    No. Again, declare the jlabel on the class level as you've done. It needs to be visible from the whole class. Place it in your app where it sits empty. Again, in your actionperformed set the text of the label only once you've calculated your average. Note that the label will require a String parameter not double when setting the text.

    Something like

    myLabel.setText("average: " + avg);

    will work.

  18. #18
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    ok so now i created this:

    Java Code:
      private JTextField myExam = new JTextField(""); // Create a text field
      private JLabel labelWithExam = new JLabel("Exam avg");
      private JTextField myHW = new JTextField(""); // Create a text field
      private JLabel labelWithHW = new JLabel("HW avg");
      private JTextField myLab = new JTextField(""); // Create a text field
      private JLabel labelWithLab = new JLabel("Lab Avg");
      private JTextField myQuiz = new JTextField(""); // Create a text field
      private JLabel labelWithQuiz = new JLabel("Quiz Avg");
      private JButton myButton = new JButton("Calculate"); // Create a new button
      private JLabel labelWithButton = new JLabel();
      
    // Constructor
    public MyGUI() {
      setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid
      add(labelWithExam);
      add(myExam);
      add(labelWithHW);
      add(myHW);
      add(labelWithLab);
       add(myLab);
       add(labelWithQuiz);
      add(myQuiz);
    add(myButton); // Make button appear in window
    add(labelWithButton);
    
    myButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String ex = myExam.getText(); // Get the text in the TextField
    double examGrade = Double.parseDouble(ex);
    String h = myHW.getText(); // Get the text in the TextField
    double hwGrade = Double.parseDouble(h);
    String l = myLab.getText(); // Get the text in the TextField
    double labGrade = Double.parseDouble(l);
    String q = myQuiz.getText(); // Get the text in the TextField
    double quizGrade = Double.parseDouble(q);
    double avg = 0;
    avg = examGrade*.56 + hwGrade*.22 + labGrade*.17 + quizGrade*.05;
    myButton.setText(avg);
    
    }
    });
    which i think you were saying...but now there is a "setText(java.lang.String) in javax.swing.AbstractButton cannot be applied to (double)" error

    i tried doing String avgGrade = String.parseString(avg);

    but it didnt work

  19. #19
    lopder1 is offline Member
    Join Date
    Nov 2009
    Posts
    15
    Rep Power
    0

    Default

    I got it, the average is being printed on the button itself so if you know how to fix it can you tell me? but other than that, thanks a ton, youve been great

  20. #20
    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 lopder1 View Post
    I got it, the average is being printed on the button
    Because you're telling it to do this. Look at what you're calling setText(avg) on -- is it the label or the button? what should it be?
    itself so if you know how to fix it can you tell me?
    don't call setText on the button.

    Much luck!

Similar Threads

  1. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 10:26 PM
  2. GUI's
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 07-26-2009, 04:38 AM
  3. reading in unsigned ints into a 2D array
    By newToIt in forum New To Java
    Replies: 9
    Last Post: 03-06-2009, 01:36 PM
  4. GUI's
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-22-2008, 10:19 PM
  5. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-14-2008, 12:26 AM

Posting Permissions

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