Solved

JButton help getting them to work

Posted on 2004-10-31
444 Views
Last Modified: 2008-03-10
I'm trying to figure out how to make the buttons on this work...  The purpose of the program is just to compare two strings.  But I can't figure out how to make it compare the strings once "Compare Strings" is pushed, and also I'm not sure how to do the Clear button.  Any help?  Thanks.

package Program4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class Program4 {
  public static void main(String[] args) {

    JLabel JLString1, JLString2, JLResults;
    JTextField JTFString1, JTFString2, JTFResults;
    JButton JBCompare, JBClear;
    JPanel Panel;
    String output = "";

    JLString1 = new JLabel("String 1: ");
    JLString2 = new JLabel("String 2: ");
    JLResults = new JLabel("Results: ");
    JBCompare = new JButton("Compare Strings");
    JBClear = new JButton("Clear");
    JTFString1 = new JTextField(15);
    JTFString2 = new JTextField(15);
    JTFResults = new JTextField(20);
    Panel = new JPanel();

    JTFString1.requestFocus();

    Panel.add(JLString1);
    Panel.add(JTFString1);
    Panel.add(JLString2);
    Panel.add(JTFString2);
    Panel.add(JBCompare);
    Panel.add(JBClear);
    Panel.add(JLResults);
    Panel.add(JTFResults);

    String String1 = JTFString1.getText();
    String String2 = JTFString2.getText();

    int compared = String1.compareTo(String2);
    if (compared < 0) {
    JTFResults.setText("String1 > String2");
    }

    else if (compared == 0) {
    JTFResults.setText("String1 = String2");
    }

    else if (compared > 0) {
    JTFResults.setText("String1 < String2");
    }

    JFrame Frame = new JFrame();
    Frame.getContentPane().add(Panel);
    Frame.setSize(250,175);
    Frame.setVisible(true);
  }
}
0
Question by:Job4Suprmn
    12 Comments
     
    LVL 6

    Expert Comment

    by:expertmb
    write action listener
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    JBCompare.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    int compared = String1.compareTo(String2);
        if (compared < 0) {
        JTFResults.setText("String1 > String2");
        }

        else if (compared == 0) {
        JTFResults.setText("String1 = String2");
        }

        else if (compared > 0) {
        JTFResults.setText("String1 < String2");
        }



                }
            });


    JBClear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    JTFResults.setText("");

                }
            });
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    The best thing to do is to add                   implements ActionListener        to your class,
    then say:                     myButton.addActionListener(this);
    for every button.
    Now you can handle events in the method:

         public void actionPerformed()

    that you must add to your class.
    In there check the 'source' of the action to be what button and you can handle everything inside this class.
    ;JOOP!
    0
     

    Author Comment

    by:Job4Suprmn
    expertmb, I tried what you said, but I"m getting:

    local variable String1 is accessed from within inner class; needs to be declared final at line 96 (98:4)
    local variable String2 is accessed from within inner class; needs to be declared final at line 96 (98:4)
    local variable JTFResults is accessed from within inner class; needs to be declared final at line 98 (98:4)
    0
     
    LVL 3

    Expert Comment

    by:vikraman_b
    Hi ,
    I modified ur program..u have to write action listeners for the buttons...
    just compile na drun the program..i already tested.

    package Program4;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;


    public class Program4 {
          private JLabel JLString1, JLString2, JLResults;
          private JTextField JTFString1, JTFString2, JTFResults;
          private JButton JBCompare, JBClear;
          private JPanel Panel;
          private String output = "";
          Program4(){
                try {
                      init();
                } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                }
          }
          private void init() throws Exception{
                JLString1 = new JLabel("String 1: ");
                JLString2 = new JLabel("String 2: ");
                JLResults = new JLabel("Results: ");
                JBCompare = new JButton("Compare Strings");
                JBCompare.addMouseListener(new java.awt.event.MouseAdapter()
                {
                      public void mouseReleased(MouseEvent e)
                      {
                            JBCompare_mouseReleased(e);
                      }
                });
                JBClear = new JButton("Clear");
                JBClear.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            JTFString1.setText("");
                            JTFString2.setText("");
                            JTFResults.setText("");
                        }
                      });

                JTFString1 = new JTextField(15);
                JTFString2 = new JTextField(15);
                JTFResults = new JTextField(20);
                Panel = new JPanel();

                JTFString1.requestFocus();

                Panel.add(JLString1);
                Panel.add(JTFString1);
                Panel.add(JLString2);
                Panel.add(JTFString2);
                Panel.add(JBCompare);
                Panel.add(JBClear);
                Panel.add(JLResults);
                Panel.add(JTFResults);



                JFrame Frame = new JFrame();
                Frame.getContentPane().add(Panel);
                Frame.setSize(250,175);
                Frame.setVisible(true);
          }
          
          
          private void JBCompare_mouseReleased(MouseEvent aMouseEvent)
          {
                String String1 = JTFString1.getText();
                String String2 = JTFString2.getText();

                int compared = String1.compareTo(String2);
                if (compared < 0) {
                JTFResults.setText("String1 > String2");
                }

                else if (compared == 0) {
                JTFResults.setText("String1 = String2");
                }

                else if (compared > 0) {
                JTFResults.setText("String1 < String2");
                }
          }
      public static void main(String[] args) {
            new Program4();


      }
    }
    0
     
    LVL 6

    Accepted Solution

    by:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;

    public class Program5 {
      public static void main(String[] args) {

        JLabel JLString1, JLString2, JLResults;
        final JTextField JTFString1, JTFString2, JTFResults;
        JButton JBCompare, JBClear;
        JPanel Panel;
        String output = "";

        JLString1 = new JLabel("String 1: ");
        JLString2 = new JLabel("String 2: ");
        JLResults = new JLabel("Results: ");
        JBCompare = new JButton("Compare Strings");
        JBClear = new JButton("Clear");
        JTFString1 = new JTextField(15);
        JTFString2 = new JTextField(15);
        JTFResults = new JTextField(20);
        Panel = new JPanel();

        JTFString1.requestFocus();

        Panel.add(JLString1);
        Panel.add(JTFString1);
        Panel.add(JLString2);
        Panel.add(JTFString2);
        Panel.add(JBCompare);
        Panel.add(JBClear);
        Panel.add(JLResults);
        Panel.add(JTFResults);



    JBCompare.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                            String String1 = JTFString1.getText();
                            String String2 = JTFString2.getText();
                    int compared = String1.compareTo(String2);
                    System.out.println(compared);
        if (compared < 0) {
        JTFResults.setText("String1 > String2");
        }

        else if (compared == 0) {
        JTFResults.setText("String1 = String2");
        }

        else if (compared > 0) {
        JTFResults.setText("String1 < String2");
        }



                }
            });


    JBClear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    JTFResults.setText("");

                }
            });

        JFrame Frame = new JFrame();
        Frame.getContentPane().add(Panel);
        Frame.setSize(250,175);
        Frame.setVisible(true);
      }
    }
    0
     

    Author Comment

    by:Job4Suprmn
    Aha!  Thanks again expertmb!  Here are your points!
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    thanks
    :)
    0
     
    LVL 23

    Expert Comment

    by:rama_krishna580
    Hi job4suprmn,

    can you justify Whats Wrong  in "vikraman_b" answer which he already provided before "expertmb".

    I think you should have a clear look at the answers first before you accept.
    And i hope you can look at the answer provided by "vikraman_b" also....

    Best of luck..

    R.K
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    rama_krishna580,

    it is another version of my code which i posted at 10/31/2004 11:17PM PST
    instead of action listener he put it as mouse listener.
    0
     
    LVL 23

    Expert Comment

    by:rama_krishna580
    Hi ,

    But, in your question you never mentioned that the solution should contain this particular listener right..?
    Only thing you ask was >>>> I'm not sure how to do the Clear button and Compare button...right..?
    Then ..what do you say about it..? As a member of this Experts Exchange group I strongly disagree what you did....above..! if you need a particular solution to be provided by the Experts you must specify the correct direction what solution and what kind of objects experts need to add in it...i hope you understand...

    R.K
    0
     

    Author Comment

    by:Job4Suprmn
    1. expertmb's version was right before vikra posted his version.  It was just out of tiredness or something I didn't realize that all it needed was a final next to the variable name.

    2. expertmb's was on my track moreso than vikra's.  Yes, vikra's may have been right, but I knew what expertmb was doing..vikra had some stuff in there I had no idea what it was...so I chose the answer I understood more.  I didn't even test vikra's because I couldn't understand the code...if I can't understand it there's no reason for me to use it because I don't know what's going on in there.

    I didn't specify requirements because I didn't know what to specify.  expertmb replied very quickly and knowledgebly, so I just went with what he said and worked with him throughout the entire process.  Vikra responded when I had used the code expertmb posted but all I had to do was add final.  I already had the answer.

    Sorry to vikra and thanks for helping out.  I understand why people may be upset, I'm just telling you my reasons for selecting the answer I did.  I don't want there to be an argument over this.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Need some clarity on Rest API 5 37
    sum67 challenge 35 68
    countHi challenge 25 38
    countPairs challenge 7 13
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This video teaches viewers about errors in exception handling.

    910 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now