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);
}
});
Related
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);
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()
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");
}
}
I would catch by a listener the button pressed but not by the text inside it because I've button only with background image.
This code catch it by label inserted in the jbutton constructor but I don't want show this label.
So or I find a way to hide label on button or I don't insert a label and catch button by some other handle.
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
JOptionPane.showMessageDialog(null,"È stato premuto"+b.getActionCommand());
}
}
Use setActionCommand() to avoid a default action command of the button text.
JButton myButton = new JButton();
myButton.setActionCommand("myButtonCommand");
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if (actionCommand.equals("myButtonCommand")) {
// do something...
}
}
ImageIcon ic=new ImageIcon("C:/image.png");
JButton btn=new JButton(ic);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JButton b = (JButton)ae.getSource();
JOptionPane.showMessageDialog(null,"È stato premuto"+b.getActionCommand());
}
});
I would like the user to enter a value into the JTextField and use a listener to listen to the textfield and print the value to the console straightaway without pressing a key.
textfield1.addChangeListener(new ChangeListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(textfield1);
}
});
error:
<anonymous textfield$2> is not abstract and does not override abstract method stateChanged(ChangeEvent) in ChangeListener
Put this private class into your public class. Just like a method.
private class textChangedListener implements KeyListener
{
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e)
{
System.out.print(textField1.getText());
}
}
And then call it to your JTextField in your main method like so:
private JTextField textField1; // just showing the name of the JTextField
textField1.addKeyListener(new textChangedListener());
Yes, just use the KeyListener class, see the example below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label = new JLabel("Write something: ");
JTextField input = new JTextField();
input.setPreferredSize(new Dimension(100, 20));
final JTextField output = new JTextField();
output.setPreferredSize(new Dimension(100, 20));
add(label);
add(input);
add(output);
input.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
JTextField textField = (JTextField) e.getSource();
String text = textField.getText();
output.setText(text);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
});
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
You can set a addlistener to textproperty of the text field.
textField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println("textfield changed to "+ newValue);
}
});