Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By doWhile

Thread: Call a method from a package using Swing

  1. #1
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Call a method from a package using Swing

    I can't seem to figure out how to call a method from a package to find out if a number is prime or not. My actionlistener from the button click is supposed to pass a variable into a BigIntisPrime(BigInteger b) method that will do the calculation for me. Here is what i have so far. Any help you can provide would be greatly appreciated. Thanks!


    public class MyForm extends JFrame {

    private JPanel contentPane;
    private final JButton btnCompute = new JButton("Compute");
    private final JLabel lblEnterANumber = new JLabel("Enter a Number");
    private final JTextField textField = new JTextField();
    private final JLabel lblTrueOrFalse = new JLabel("True or False");
    private final JButton btnExit = new JButton("Exit");
    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    MyForm frame = new MyForm();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public MyForm() {
    textField.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
    textField.setText(null);
    }
    });
    textField.setBounds(20, 27, 86, 20);
    textField.setColumns(10);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 250, 200);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setBackground(Color.green);
    setContentPane(contentPane);
    contentPane.setLayout(null);
    btnCompute.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    //PrimeNumbers test = new PrimeNumbers();

    String primeValue = textField.getText();

    int num = Integer.parseInt(primeValue);

    PrimeNumbers.IsPrime(num);

    lblTrueOrFalse.setText(primeValue);

    lblTrueOrFalse.setVisible(true);
    }
    });

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Call a method from a package using Swing

    I can't seem to figure out how to call a method from a package to find out if a number is prime or not. My actionlistener from the button click is supposed to pass a variable into a BigIntisPrime(BigInteger b) method that will do the calculation for me. Here is what i have so far. Any help you can provide would be greatly appreciated. Thanks!
    And what is the problem? Does it compile? Do you get exceptions? Does it misbehave? We can only guess as to the answers of these questions, as well ass what the class PrimerNumbers is and where it resides...post an SSCCE

  3. #3
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    It compiles and outputs the same number I input... This because the lblbTrueOrFalse.setText(primevalue) i have in the code. My main issue is in my actionPerformed I'm not able to pass the textField value that the user enters to another class PrimeNumbers.

    I can't figure out the correct way to pass the variable so it determines if it is prime or not.

    Here is my swing class
    [Java] myForm - Pastebin.com
    Here is my PrimeNumbers class
    [Java] PrimeNumbers - Pastebin.com

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

    Default Re: Call a method from a package using Swing

    You seem to be passing the value to the other class just fine. Your problem is that you seem to be discarding the result returned from the other class:

    Java Code:
    PrimeNumbers.IsPrime(num);
    The IsPrime(...) method (which should be renamed isPrime(...)) returns a boolean, but you don't put the result into a local variable. I suggest that you do this, and then print the contents of this variable (true or false) in your JLabel.

  5. #5
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    So this is where I'm passing the value?

    int num = Integer.parseInt(primeValue);

    PrimeNumbers.IsPrime(num);



    How do I put the result into a local variable? I thought I was doing that with this... lblTrueOrFalse.setText(primeValue); ?

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

    Default Re: Call a method from a package using Swing

    Quote Originally Posted by bkohusjr View Post
    So this is where I'm passing the value?

    int num = Integer.parseInt(primeValue);

    PrimeNumbers.IsPrime(num);



    How do I put the result into a local variable? I thought I was doing that with this... lblTrueOrFalse.setText(primeValue); ?
    No you're not. To set a variable it must be on the left hand side of an assignment operator, =. Look where you set primeValue and what you set it to

    Java Code:
    String primeValue = textField.getText();

    It is being assigned the text held by textField, and no where else do you see primeValue being assigned any other value. I'm assuming you want to display "true" or "false", so you must get the result returned from the PrimeNumbers.IsPrime(num) method. How do you do this? by getting the result from this method and putting it into a variable:

    Java Code:
    boolean foo = PrimeNumbers.IsPrime(num);
    Now you've got the result and can do something with it. Keep reading your textbook and notes as this is key stuff you're learning.

  7. #7
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    That is very helpful information as I am doing my best to learn on my own.

    So I'm setting primeValue to the textField(Which is the text the user enters)

    Java Code:
      String primeValue = textField.getText();
    Then I'm setting num to primeValue

    Java Code:
    int num = Integer.parseInt(primeValue);
    Then I'm calling the isPrime method and passing in num

    Java Code:
    PrimeNumbers.isPrime(num);
    Then you say I need to return the value of num with this

    Java Code:
    boolean foo = PrimeNumbers.IsPrime(num);
    Am I on the correct path?

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

    Default Re: Call a method from a package using Swing

    Quote Originally Posted by bkohusjr View Post
    That is very helpful information as I am doing my best to learn on my own.

    So I'm setting primeValue to the textField(Which is the text the user enters)

    Java Code:
      String primeValue = textField.getText();
    Then I'm setting num to primeValue

    Java Code:
    int num = Integer.parseInt(primeValue);
    Then I'm calling the isPrime method and passing in num

    Java Code:
    PrimeNumbers.isPrime(num);
    Then you say I need to return the value of num with this

    Java Code:
    boolean foo = PrimeNumbers.IsPrime(num);
    Am I on the correct path?
    Yes. Now this will return a boolean. You may need to convert this to a String to feed it into a JLabel or JTextField. Boolean has a method that allows you to do this: Boolean.parseBoolean(...).

  9. #9
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    Yes I'm getting many errors that the types do not match.

    Would
    <CODE>Boolean.parseBoolean(...). </CODE>
    be a new line of code in addition to the ones above?

    Maybe boolean num = Boolean.parseBoolean(???)

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

    Default Re: Call a method from a package using Swing

    Sorry, Boolean.parseBoolean was incorrect. :(

    You want to use Boolean.valueOf(foo) or String.valueOf(foo) to convert foo (a boolean) to a String which the JLabel or JTextField will accept.

  11. #11
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    Can you explain this foo thing to me? How do I implement that or is that a generic term for one of my variables?

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

    Default Re: Call a method from a package using Swing

    Quote Originally Posted by bkohusjr View Post
    Can you explain this foo thing to me? How do I implement that or is that a generic term for one of my variables?
    It's just the name of the boolean variable that's local to this method that I used in one of my answers above. You will use some other variable name of course.

    As an aside, programming tradition often uses foo, bar, or baz for names of dummy or demonstration variables.

  13. #13
    bkohusjr is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default Re: Call a method from a package using Swing

    So here is what i have so far.

    Java Code:
      String primeValue = textField.getText();
                      
                      int num = Integer.parseInt(primeValue);
                                     
                      PrimeNumbers.isPrime(num);
                      
                      boolean localPrime = PrimeNumbers.isPrime(num);
                      
                      Boolean.valueOf(localPrime);
     
                      lblTrueOrFalse.setText(localPrime);
    
                      lblTrueOrFalse.setVisible(true);

    it doesn't like the lblTrueorfalse.setText(localPrime);

    The error I get is: The method setText(String) in the type JLabel is not applicable for the arguments (boolean)


    Really appreciate all of your help. At this point my head is spinning from staring at it so long that I could have it right at one point and not even know it!

  14. #14
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Call a method from a package using Swing

    The Boolean.valueOf method returns the result...just passing the value to the method does nothing to help you convert the localPrime variable to a String - you need to capture the returned value of the method and pass that to the setText method.
    Fubarable likes this.

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

    Default Re: Call a method from a package using Swing

    Quote Originally Posted by bkohusjr View Post
    So here is what i have so far.

    Java Code:
      String primeValue = textField.getText();
                      
                      int num = Integer.parseInt(primeValue);
                                     
                      PrimeNumbers.isPrime(num);
                      
                      boolean localPrime = PrimeNumbers.isPrime(num);
                      
                      Boolean.valueOf(localPrime);
     
                      lblTrueOrFalse.setText(localPrime);
    
                      lblTrueOrFalse.setVisible(true);

    it doesn't like the lblTrueorfalse.setText(localPrime);

    The error I get is: The method setText(String) in the type JLabel is not applicable for the arguments (boolean)


    Really appreciate all of your help. At this point my head is spinning from staring at it so long that I could have it right at one point and not even know it!
    You're making the same mistake that I thought we corrected previously!

    Just calling a method does nothing if you don't capture the result it returns. As doWhile states, Boolean.valueOf returns a String -- so capture it like I showed you how to capture the boolean result returned from your own method.

Similar Threads

  1. Tutorial how to call OSGI service from EAR package
    By rcbandit in forum Advanced Java
    Replies: 0
    Last Post: 01-14-2012, 06:29 PM
  2. Tutorial how to call OSGI service from EAR package
    By rcbandit in forum Advanced Java
    Replies: 0
    Last Post: 01-04-2012, 05:16 PM
  3. Replies: 1
    Last Post: 10-17-2011, 02:00 AM
  4. package javax.swing does not exist
    By timosoft in forum AWT / Swing
    Replies: 5
    Last Post: 02-15-2011, 02:17 AM
  5. How to call/invoke a Oracle package using java
    By sasi.tati in forum New To Java
    Replies: 4
    Last Post: 11-03-2010, 03:26 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
  •