Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JButton help getting them to work

Posted on 2004-10-31
12
Medium Priority
?
448 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
Comment
Question by:Job4Suprmn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 12460997
write action listener
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12461013
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
ID: 12461019
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Job4Suprmn
ID: 12461057
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
ID: 12461062
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:
expertmb earned 500 total points
ID: 12461070
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
ID: 12461079
Aha!  Thanks again expertmb!  Here are your points!
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12461082
thanks
:)
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 12461153
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
ID: 12461173
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
ID: 12461192
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
ID: 12580096
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

596 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