• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

JButton help getting them to work

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
Job4Suprmn
Asked:
Job4Suprmn
  • 5
  • 3
  • 2
  • +2
1 Solution
 
expertmbCommented:
write action listener
0
 
expertmbCommented:
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
 
sciuriwareCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Job4SuprmnAuthor Commented:
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
 
vikraman_bCommented:
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
 
expertmbCommented:
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
 
Job4SuprmnAuthor Commented:
Aha!  Thanks again expertmb!  Here are your points!
0
 
expertmbCommented:
thanks
:)
0
 
rama_krishna580Commented:
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
 
expertmbCommented:
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
 
rama_krishna580Commented:
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
 
Job4SuprmnAuthor Commented:
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now