How to detect when a JButton is pressed (swing)? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Java's swing package, I was wondering how to detect when a JButton is pressed. Is there a function that is called when the button is pressed? Thanks

Yes, when you deal with button pressing, you want to add what is known as an action listener. First, you must
import java.awt.event.ActionListener;
then you can do the following
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// this makes sure the button you are pressing is the button variable
if(e.getSource() == button) {
// do action necessary code
}
}
});

Related

Java Enable/Disable Button [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm creating a game that uses buttons. I want to disable a button once it's been selected, so that it cannot again until the game has restarted, but I'm having trouble achieving this. Could some let me know how to go about doing this?
public void actionPerformed(ActionEvent e) {
if (q==2) {
label2.setText("Correct!"); }
else {
label2.setText("Wrong!!");
}}
You can get the source of the event from the ActionEvent with getSource. Then cast it to the correct type, and disable it with setEnabled
Here's an example, assuming you're using JButton
public void actionPerformed(ActionEvent e) {
if (q==2) {
label2.setText("Correct!");
} else {
label2.setText("Wrong!!");
}
if(e.getSource() instanceof JButton) {
((JButton)e.getSource()).setEnabled(false);
}
}
Call button.setEnabled(false) to disable it, and button.setEnabled(true) to reinstate it.
What's the trouble? I trust you've taken a look at the JButton documentation (assuming you're using JButtons). Using this function will allow you to enable/disable buttons.
JButton.setEnabled(boolean)

How to change a picture after pressing on it using MouseListener? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My Images are in JLabels and they follow a MouseListener but I don't know what to add in the mousePressed(Event e) method to change the JLabel with another image ??
JLabel has setIcon() method. You can use it to change the picture your label shows.
For example:
JLabel myLabel = new JLabel(imageOne);
myLabel.addMouseListener(new MouseAdaper() {
public void mousePressed(MouseEvent e) {
ImageIcon image2 = new ImageIcon("image2.png");
myLabel.setIcon(image2);
}
});

How do I create a Java Swing component that contains only an image and a JLabel? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to make a simple component that does nothing other than draw an arbitrary image, and then directly beneath it place a JLabel for displaying arbitrary text.
How can I achieve this? I'm brand new to Swing and I'm trying to learn as I go, but I don't currently understand how I would go about doing this. I know it's a basic question, and I appreciate any help I can get.
JLabel is your friend:
JLabel label = new JLabel("Your text here");
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setIcon(new ImageIcon(this.getClass().getResource("/path/to/image/image.jpg")));

Making a multi-tiered program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on an assignment in which I need to combine two programs that I have created into one functioning program. The end result I am hoping for is a program that once launched, opens a log in window, then once logged in, the user gets to play a tic tac toe game. Basically I just was wonder how to have a window within which when you click a button, a new window opens that can run extensive code.
If you're using Swing framework, Create a second JFrame and set its visibility to false, and when the button is clicked, set it visibility to true.
public class MyFrame extends JFrame {
private JButton jbt = new JButton("Open Window");
private AnotherFrame jfrm = new AnotherFrame();
public MyFrame(){
add(jbt);
jfrm.setVisibility(false);
add(jfrm);
jbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jfrm.setVisibility(true);
}
});
}
private AnotherFrame extends JFrame {
public AnotherFrame(){
}
}
}

Opening a JFrame within a JFrame [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a main JFrame with a button that generates another JFrame. However, when I close the 2nd JFrame the main JFrame is also closed which is not what I want. What am I doing wrong?
I assume you're setting the default closing action as:
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
The JFrame.EXIT_ON_CLOSE constant terminates the whole program when you click the x button. You probably want to use DISPOSE_ON_CLOSE or HIDE_ON_CLOSE instead. I recommend to have a look at the JavaDoc of JFrame.

Categories