This question already has an answer here:
Add key bindings to JButtons that get their actions from action commands?
(1 answer)
Closed 2 years ago.
I would like to have 2 buttons:
1) BindBTN - When clicked a keyListener/action will listen for key press and find the KeyCode of that key.
2) RunBTN - When clicked an action will wait until the user presses the same key and then preforms runProgram()
RunBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
#SuppressWarnings("serial")
AbstractAction run = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
runProgram();
}
};
RunBTN.getInputMap().put(KeyStroke.getKeyStroke("**bound key**"),
"run");
RunBTN.getActionMap().put("run",
run);
}
});
What should i do for BindBTN?
Okey so i figured it out:
for BindBTN:
BindingBTN.addKeyListener(new KeyAdapter()
{
#Override
public void keyPressed(KeyEvent evtBind)
{
BindCmd = evtBind.getKeyCode();
BindCmdString = KeyEvent.getKeyText(BindCmd);
}
});
and for RunBTN:
RunBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
#SuppressWarnings("serial")
AbstractAction run = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
runProgram();
}
};
RunBTN.getInputMap().put(KeyStroke.getKeyStroke(BindCmdString),
"run");
RunBTN.getActionMap().put("run",
run);
}
});
Related
b.button1 = new JButton("Deal");
b.button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
b.button2 = new JButton("Hit");
panel.add(b.button2);
panel.validate();
b.button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
});
b.button3 = new JButton("Stay");
panel.add(b.button3);
panel.validate();
b.button3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
}
});
So I want The Buttons Hit and Stay to be added as soon as the Deal button has been pressed. I searched for a solution and found the panel.validate() method. I used it but now if I press the Deal button it only adds the Hit button.
You could add the buttons before and make them "hidden". If you press the button, you can "show" them to include them.
I am new to coding GUIs in Java and I am trying to just print a message on the terminal when a button is pressed and another one when it is released.
This is what I have for a regular button pressing.
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Pressed");
}
});
I did this with the help of IntelliJ IDEA. I want to make the button send a message when pressed and a different thing when released.
You can just add a simple MouseAdapter, like this:
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
}
};
leftButton.addMouseListener(ma);
frame.add(button);
This will detect when it is the mouse is pressed on the button or released on the button.
If you want, you can also add a mouseClicked() method, mouseExited(), mouseEntered(), mouseMoved(), and (many) more methods in your MouseAdapter. Check out this JavaDoc
Use custom class and use it
leftButton.getModel().addChangeListener(new BtnCusttomListener());
private class BtnCusttomListener implements ChangeListener {
private boolean pressed = false; // holds the last pressed state of the button
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel Buttonmodel = (ButtonModel) e.getSource();
// if the current state differs from the previous state
if (model.isPressed() != pressed) {
String text = "Button pressed: " + model.isPressed() + "\n";
textArea.append(text);
pressed = model.isPressed();
}
}
}
You can use a MouseListener instead:
leftButton.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
// The mouse button was pressed and released
}
#Override
public void mousePressed(MouseEvent e) {
// The mouse button was pressed
}
#Override
public void mouseReleased(MouseEvent e) {
// The mouse button was released
}
#Override
public void mouseEntered(MouseEvent e) {
// The cursor entered the bounds of the button (i.e. hovering)
}
#Override
public void mouseExited(MouseEvent e) {
// The cursor exited the bounds of the button
}
});
I have two events that support the button and table by click mouse
if i click te button should remove the record from the list and table. It
happens but program display this exception ArrayIndexOutOfBoundsException:
-1
r.getjTable3().addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e)
{
System.out.println(e.getClickCount());
r.getjButton2().addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e)
{
String nazwa = r.getjTable3().getValueAt(r.getjTable3().getSelectedRow(), 3).toString();
System.out.println(r.getjTable3().getSelectedRow());
((DefaultTableModel)r.getjTable3().getModel()).removeRow(r.getjTable3().getSelectedRow());
for (Wydarzenie lista1 : lista)
{
if(nazwa==lista1.name)
{
lista.remove(lista1);
}
}
}
});
}
});
i have one button group with 2 radio buttons:
private javax.swing.JRadioButton jRadioButtonESPRINCIPAL;
private javax.swing.JRadioButton jRadioButtonESSECUNDARIO;
buttonGroup1 = new javax.swing.ButtonGroup();
I know that i can clear the group by buttonGroup1.clearSelection(), but i want to do this only if i click on a clicked radio button.
I have tried
private void jRadioButtonESPRINCIPALMouseClicked(java.awt.event.MouseEvent evt) {
if (jRadioButtonESPRINCIPAL.isSelected()) {
buttonGroup1.clearSelection();
}
else{
jRadioButtonESPRINCIPAL.setSelected(true);
}
}
private void jRadioButtonESSECUNDARIOMouseClicked(java.awt.event.MouseEvent evt) {
if (jRadioButtonESSECUNDARIO.isSelected()) {
buttonGroup1.clearSelection();
}
else{
jRadioButtonESSECUNDARIO.setSelected(true);
}
}
But didnt works
Any help will be appreciate
My previous answer was wrong sorry. You have to use ActionListeners like this:
jRadioButtonESPRINCIPAL.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//your code goes here
}
});
and then use a variable to store the last RadioButton that was selected. In the ActionListener you then have to check if the isSelected() equals the last selection. If yes, then use buttonGroup1.clearSelection();.
So the final code should look like this:
jRadioButtonESPRINCIPAL.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (jRadioButtonESPRINCIPAL.equals(lastSelectedRadioButton)) {
buttonGroup1.clearSelection();
lastSelectedRadioButton = null;
}
else {
lastSelectedRadioButton = jRadioButtonESPRINCIPAL;
}
}
});
jRadioButtonESSECUNDARIO.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (jRadioButtonESSECUNDARIO.equals(lastSelectedRadioButton)) {
buttonGroup1.clearSelection();
lastSelectedRadioButton = null;
}
else {
lastSelectedRadioButton = jRadioButtonESSECUNDARIO;
}
}
});
I have following code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TesttxtF {
/**
* #param args
*/
public static void main(String[] args) {
TextField txt1 = new TextField();
TextField txt2 = new TextField();
DefaultFocusManager manager = new DefaultFocusManager() {
#Override
public void processKeyEvent(Component focusedComponent, KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER:
if (e.getID() == KeyEvent.KEY_PRESSED)
super.focusNextComponent(focusedComponent);
else
super.processKeyEvent(focusedComponent, e);
break;
default:
super.processKeyEvent(focusedComponent, e);
break;
}
}
};
FocusManager.setCurrentManager(manager);
JPanel panel = new JPanel();
panel.add(txt1);
panel.add(txt2);
txt1.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
System.out.println("Key keyTyped");
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println("Key keyReleased");
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed");
}
});
txt1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel, null);
frame.setSize(100, 100);
frame.setVisible(true);
}
}
When run with
jdk1.6.0_18 -> output as following when pressing enter key
Key Pressed
Key keyTyped
Action performed
jdk1.7.0_12 -> output as following when pressing enter key
Key Pressed
What is wrong with java 7 then ?
when i typed any number and clear the console. Then press the Enter key focus changed to next component but actionperformed never fired in java 7. How i can fix it ? i had checked with java 7 update 25 also. i got same result. Can anybody help ?