Setting Multiple buttons to disabled - java

I'm trying to make it so when the user clicks a button it will become disabled. I've used the following code to achieve this:
final JButton die1 = new JButton(theModel.letters.get(0));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn1){
die1.setEnabled(false);
}
});
boggleGrid.add(die1);
theModel.letters.get(0) is a string value and works validly. My problem is when I try to use this same code for other buttons within the same boggleGrid JPanel. I think it may have to do with the "final" statement, however it is necessary for the actionListener to function. Here is the rest of my code:
JPanel boggleGrid = new JPanel(new GridLayout(4,4));
theModel.randomLetters();
final JButton die1 = new JButton(theModel.letters.get(0));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn1){
die1.setEnabled(false);
}
});
boggleGrid.add(die1);
final JButton die2 = new JButton(theModel.letters.get(1));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn2){
die2.setEnabled(false);
}
});
boggleGrid.add(die2);
final JButton die3 = new JButton(theModel.letters.get(2));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn3){
die3.setEnabled(false);
}
});
boggleGrid.add(die3);
final JButton die4 = new JButton(theModel.letters.get(3));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn4){
die4.setEnabled(false);
}
});
boggleGrid.add(die4);
final JButton die5 = new JButton(theModel.letters.get(4));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn5){
die5.setEnabled(false);
}
});
boggleGrid.add(die5);
final JButton die6 = new JButton(theModel.letters.get(5));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn6){
die6.setEnabled(false);
}
});
boggleGrid.add(die6);
final JButton die7 = new JButton(theModel.letters.get(6));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn7){
die7.setEnabled(false);
}
});
boggleGrid.add(die7);
final JButton die8 = new JButton(theModel.letters.get(7));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn8){
die8.setEnabled(false);
}
});
boggleGrid.add(die8);
final JButton die9 = new JButton(theModel.letters.get(8));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn9){
die9.setEnabled(false);
}
});
boggleGrid.add(die9);
final JButton die10 = new JButton(theModel.letters.get(9));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn10){
die10.setEnabled(false);
}
});
boggleGrid.add(die10);
final JButton die11 = new JButton(theModel.letters.get(10));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn11){
die11.setEnabled(false);
}
});
boggleGrid.add(die11);
final JButton die12 = new JButton(theModel.letters.get(11));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn12){
die12.setEnabled(false);
}
});
boggleGrid.add(die12);
final JButton die13 = new JButton(theModel.letters.get(12));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn13){
die13.setEnabled(false);
}
});
boggleGrid.add(die13);
final JButton die14 = new JButton(theModel.letters.get(13));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn14){
die14.setEnabled(false);
}
});
boggleGrid.add(die14);
JButton die15 = new JButton(theModel.letters.get(14));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn15){
die15.setEnabled(false);
}
});
boggleGrid.add(die15);
JButton die16 = new JButton(theModel.letters.get(15));
die1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent btn16){
die16.setEnabled(false);
}
});
boggleGrid.add(die16);
frame.add(boggleGrid, BorderLayout.CENTER);
Context: I am making a Boggle game (if that helps). Thanks

You're calling addActionListener on the same JButton multiple times, for example:
final JButton die4 = new JButton(theModel.letters.get(3));
die1.addActionListener(new ActionListener() {
You are creating a button called die4 and then adding an ActionListener to die1. You want the call to addActionListener to happen on die4 in this case.
final JButton die4 = new JButton(theModel.letters.get(3));
die4.addActionListener(new ActionListener() {
^^^^
Here is an implementation of your code that is a bit more concise and probably easier to maintain:
JPanel boggleGrid = new JPanel(new GridLayout(4, 4));
ActionListener disableButtonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event)
{
// event.getSource() will return a reference to the control that
// fired the event. In this case, a JButton. We protect
// ourselves from a ClassCastException just in case though.
if (!(event.getSource() instanceof JButton)) {
return;
}
((JButton) event.getSource()).setEnabled(false);
}
};
theModel.randomLetters();
for (int i = 0; i < 16; i++) {
JButton button = new JButton(theModel.letters.get(i));
button.addActionListener(disableButtonListener);
boggleGrid.add(button);
}
frame.add(boggleGrid, BorderLayout.CENTER);

Related

Java Smileys Replacing String with Smiley

I'm writing a Chat program. I designed a mock-up gui with smileys where when the user clicks on a smiley(jbutton) it prints it onto a textpane. I managed to add an advanced feature where when a user types in ":)" and sends it, it inserts the smiley instead of the string - using the insertIcon() method. The problem I have is that it only prints the smiley once rather than multiple times. So if I type "Hi :) My name is Jack :)" it only inserts the icon ONCE. Any suggestions?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile1.png")));
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile2.png")));
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile3.png")));
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile4.png")));
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile5.png")));
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile6.png")));
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile7.png")));
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile8.png")));
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile9.png")));
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
// tPane.setText(a);
// tPane.getText();
if (a.equals(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.equals(":)")) {
tPane.insertIcon(smile);
} else if (a.equals(":(")) {
tPane.insertIcon(sad);
} else if (a.equalsIgnoreCase(":P")) {
tPane.insertIcon(tongue);
} else if (a.equals(";)")) {
tPane.insertIcon(wink);
}
text.setText(null);
text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
insertIcon() method of JTextPane uses selection (caret position in simplest case). So in your case you always replace the icon just once.
Your sendMessage() doesn't check multiple occurences of ":)" in the message. Use while loop obtaining indexes of the ":)" and for each index make it selected and then use insertIcon()

Java insertIcon multiple times in a text pane

I'm writing a chat program in Java. I made a section for smileys. I managed to print the smileys on a text pane every time its clicked but when I click on the same smiley more than once it only prints it once. Any help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
int number = 0;
boolean check = true;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(happy);
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(smile);
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(tongue);
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(veryHappy);
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(wink);
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(laugh);
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(sad);
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(verySad);
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(cry);
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
//
// if(a){
// tPane.setText(a);
// tPane.getText();
// }
if (a.contains(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.contains(":)")) {
tPane.insertIcon(happy);
} else if (a.contains(":(")) {
tPane.insertIcon(sad);
}
// text.setText(null);
// text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
That is because you cannot add the same ImageIcon to the JTextPane more then once.
You can create the ImageIcons dynamically in the ActionListener, every time a smiley is clicked create a new ImageIcon
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource("smileys/Smile2.png")));
}
});

How to assign one Action Listener to a couple of buttons?

button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
I have 3 buttons here, but they are doing same thing. It takes some space in code. How can I group them all and assigned to one ActionListener?
Something like this. I don't know how it should be.
button1.button2.button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
});
Just assign the ActionListener to a different variable first:
ActionListener listener = new ActionListener() {
...
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
It's just a reference after all - the only "special" thing here is the use of an anonymous inner class to create an instance of ActionListener.
If there are multiple things that you want to do with all your buttons, you may well want to put them into a collection rather than having three separate variables for them, too.
Juste create a variable :-)
ActionListener listener = new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
ActionListener listener = new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
exite.setEnabled(true);
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
Implement ActionListener in your class
public class YourClass implements ActionListener
That should make you have to implement an actionPerformed method in just one place where it can handle all your actions.
Your addActionListener lines should then become:
button1.addActionListener(this);
button2.addActionListener(this);
etc.
Then use:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == button1 || event.getSource() == button2 || event.getSource() == button3) {
exite.setEnabled(true);
}
}
You can create a class called event that implements the ActionListener for JButtons. Then use the parameter getCommand() method to get the text of the JButton and create if statements for all of the buttons based on the text that the JButton has, the code would be:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*
public class MultipleJButtons extends JFrame {
JButton button1, button2, button3;
public static void main(String args[]) {
MultipleJButtons gui = new MultipleJButtons();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setTitle("Multiple Buttons");
gui.setVisible(true);
}
public MultipleJButtons() {
event e = new event();
setLayout(new FlowLayout());
button1 = new JButton("Button 1");
add(button1);
button1.addActionListener(e);
button2 = new JButton("Button 2");
add(button2);
button2.addActionListener(e);
button3 = new JButton("Button 3");
add(button3);
button3.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Button 1")) {
System.out.println("You pressed button 1");
} else if (command.equals("Button 2")) {
System.out.println("You pressed button 2");
} else if (command.equals("Button 3")) {
System.out.println("You pressed button 3");
}
}

Changing a String data with Jbuttons in Java

I have a String called s
I have four JButtons b1 b2 b3 b4
By default String s is Hello World
b1 Makes String s toUpperCase
b2 Makes String s toLowerCase
b3 Allows user to change String
b4 Resets String to Hello World
When I run the program I have s set to Hello World but when I click my buttons the String is not changing to its new attribute.
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
public class Driver {
String s = "Hell World";
String up;
String low;
private JFrame f;
private JPanel p;
JFrame frame = new JFrame("Add Some Text");
JButton b1 = new JButton("Uppercase");
JButton b2 = new JButton("Lowercase");
JButton b3 = new JButton("New");
JButton b4 = new JButton("Reset");
JLabel jl = new JLabel(s);
public Driver () {
gui();
}
public void gui() {
f = new JFrame("Hello World");
p = new JPanel();
f.add(p);
p.setLayout(null);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(jl);
b1.setLocation(27, 80);
b2.setLocation(300, 80);
b3.setLocation(27, 180);
b4.setLocation(300, 180);
jl.setLocation(240, 20);
b1.setSize(230, 80);
b2.setSize(230, 80);
b3.setSize(230, 80);
b4.setSize(230, 80);
jl.setSize(230, 20);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(560,300); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
up = s.toUpperCase();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
low = s.toLowerCase();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = "Hello World";
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver
You have to set the text to the label!
Try this:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toUpperCase());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toLowerCase());
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText("Hello World");
}
});
Actually the String is changing, what happens here that you haven't update the JLabel with the new value for the String:
So your code should look like this:
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
then add the following line in each Action ActionListener.
jl.setText(newValue);// newValue depends on what the ActionListener do.

How to perform action on JButton upon Enter on JTextField?

I have a JTextField and a JButton, when user hits enter on the JTextField then action has to be performed on the corresponding JButton. Here is my code snippet.
Also i want to disable the JButton on enter and enable it later when something is changed in the JTextField
JButton jb=new JButton("Print");
JTextField jt=new JTextField(20);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println(jt.getText());
}
});
Add the same action listener to the button and the text field:
JButton jb = new JButton("Print");
JTextField jt = new JTextField(20);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(jt.getText());
}
};
jb.addActionListener(listener);
jt.addActionListener(listener);
Here is a modification of your code, i registered a DocumentListener to listen for text changes in JTextField and also added ActionListener to JTextField
jb=new JButton("Print");
jt=new JTextField(20);
jt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
jb.doClick();
}
});
jt.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void changedUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void removeUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
});
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println(jt.getText());
jb.setEnabled(false);
}
});
To perform same action on button click and enter press on Jfield.
JButton jb = new JButton("Print");
JTextField jt = new JTextField(20);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(jt.getText());
// Diable Button after action performed.
jb.setEnabled(false);
}
};
jb.addActionListener(listener);
jt.addActionListener(listener);
// Enable Button after any change in JtextField value. JTextField value change Listener refered from Link
jt.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void changedUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
public void removeUpdate(DocumentEvent de)
{
jb.setEnabled(true);
}
});

Categories

Resources