This question already has answers here:
Allowing the "Enter" key to press the submit button, as opposed to only using MouseClick
(7 answers)
create hot keys for JButton in java using swing
(4 answers)
Closed 7 years ago.
I'm trying to add an accelerator/keyboard shortcut to a JButton in a GUI in Eclipse (Luna).
I'm trying to replicate what the program would do when a "Login" button is pressed, but I also want it to work when the "Enter" key on the keyboard is pressed.
I've tried "Add event handler > key > KeyPressed" for the JButton and even tried the JPasswordField/JTextField, but then the code I try doesn't end up working.
This is the auto-generated code for the GUI elements:
loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
checkPassword();
}
});
loginButton.setBounds(146, 117, 165, 23);
frmIpassAccountManager.getContentPane().add(loginButton);
I've spent a fair amount of time searching Stackoverflow and other websites for a solution but am yet to find a solution. I've added accelerators to JMenuItems before, but I believe it works differently to JButtons, ect.
If some one could please tell me what code to add to which GUI element that would be extremely helpful.
Thank you.
Related
This question already has answers here:
How can I check that JButton is pressed? If the isEnable() is not work?
(6 answers)
Closed 7 years ago.
I am creating a basic calculator GUI and I have created JHoverButtons and a text field that only allows integer inputs. Now what I am trying to do is have it so that if for example, the hover button that says "1" on it is clicked by the user, then the text field would display "1" just as the user has done so by clicking the button.
So I have created if-statements to do as I had detailed above and this is what I had written for that:
if (hoverOne.isSelected()){
integerInput.setText("1");
}
Turns out the isSelected method is for toggle buttons with which my hover buttons are not so when I run the file, it doesn't do what I wanted. I named the text field in the GUI as integerInput and the hover button with "1" on it as hoverOne.
How can I get it so that when a user clicks the button, the text field displays the number the user entered?
You can try to use isPressed() method to check if the JButton is pressed:
if(jButton1.getModel().isPressed())
{
//code
}
I'm trying to add and draw buttons using merely my code. To be more specific, my goal is visualizing several buttons after another button has been pressed. This must be done programmatically because the number of buttons that have to be drawn depends on the user's input.
I've seen this piece of code in another thread and tried to implement it, but it didn't quite work:
// Adds a single button to a panel and paints it programmatically
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
JButton button = new JButton();
jPanel1.add(button);
jPanel1.revalidate();
jPanel1.repaint();
}
Any suggestions?
Thanks in advance.
Note: I'm using NetBeans 8.0.2.
This question already has answers here:
Waiting for multiple SwingWorkers
(2 answers)
Closed 8 years ago.
I need to perform a method after the clicking of a JButton in a Java Project.
I'm making a client-server game and after the click of a button I need that the client/server start to wait untill the opponent perform a click. The problem is that at the end of the action listener code I start a loop end untill the opponent don't perform another click the jbutton stays clicked..
public void actionPerformed(ActionEvent e) {
JButton o = (JButton)e.getSource();
String name = o.getName().substring(3);
Click(Integer.parseInt(name));
if(isServer)
ListenServer();
else
ListeClient();
}
ListenServer() and ListenClient() are two loop function... How can I call this methods AFTER the click??? Thanks and sorry for the bad english
You can use threads and synchronization. See https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html for examples.
This question already has an answer here:
Clearing a group of radio buttons in Java
(1 answer)
Closed 9 years ago.
How do you reset/clear a JRadiobutton? clearing a TextField is as simple as surenameField.setText(""); is there any similar way with the RadioButton?
You mean
radioButton.setSelected(false);
?
If you want to clear the text of a JRadioButton named radio, you would call radio.setText(""). If you want to de-select the button, you call radio.setSelected(false). Note that this does not actually fire events, so if you have a listener registered to respond to de-select events, this won't notify that listener.
For more information, you can read the official documentation for JRadioButton here
JRadioButton API
Could you specify what you mean by "reset"?
The following things are possible:
Remove the button from the program: you can do this by calling the remove method from its container ([container].remove([radioButton], eg. myPanel.remove(myRadioButton).
Remove the text from the button: you can do this by doing what you set: myRadioButton.setText("")
I have written code for an online quiz. I would like to change questions by clicking "next" button, but repaint is not working; only new window is working.
i can't even hide jftMainFrame since it works for 8 windows only.quest is a list containing questions and options ,its accessed from access db.repaint() is not working while i click the button.
i have 4 radiobuttons which displays the label.i want to repaint the label of radiobutton and also question
Please help me.
JFrame jtfMainFrame, jtfMainFrame1;
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Next question..");
j++;
quest = getCurrentQuestion();
createWindow();
validate();
}
});
I would like to change questions by clicking "next" button,
I think that your question is about using CardLayout, rather than create lots of Top-Level Comtainers on runtime
Your question is definitely not clear.
What are you trying to do. If you are simply trying to "repaint/refresh" a panel or a component use paintImmediately();
for example
jMyPanel.paintImmediately(jMyPanel.getVisibleRect());
Hope it helps