Java Enable/Disable Button [closed] - java

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)

Related

Minecraft Plugin setMotd() [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 was looking for some time how to use setMotd() function, but I can't find anything useful. I've found ServerListPingEvent http://www.javaminecraft.com/bukkitapi/org/bukkit/event/server/ServerListPingEvent.html but I've got no idea how to use it... Maybe You could help me?
To listen to this event and, as a response, change the motd, you need a Listener. Define a class implementing Listener (let's say FooListener) with the following method declaration:
#EventHandler
public void onServerListPingEvent(ServerListPingEvent event) {
event.setMotd("Some MOTD");
}
You then need to register this listener in one of your plugin class's onLoad() or onEnable() methods. Do this like so:
#Override
public void onLoad() { // or onEnable()
// other stuff
getServer().getPluginManager().registerEvents(new FooListener(), this);
}
If you want to colour your MOTD, look it up. You have to use the section sign (ยง).

How to save input taken through text field using a save button using net beans? [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 8 years ago.
Improve this question
I am a beginner in programming and using net beans java for my work. can someone please tell how can I save input data that I have taken from user using text field in the gui? Here is the code for input:
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{
char b_no = evt.getKeyChar();
if (!(Character.isDigit(b_no)) || (b_no==KeyEvent.VK_BACKSPACE) ||b_no==KeyEvent.VK_DELETE))
{
getToolkit().beep();
evt.consume();
}// TODO add your handling code here:
}
I need help on how I should save this input
If you're trying to add a KeyListener to a JTextField, then don't. This should never be done as it will mess up the functionality of the JTextField. Instead if you're trying to limit input to text, then consider using :
a JFormattedTextField with a decent MaskFormatter. For example, formatted text field tutorial, or
add a DocumentFilter to your JTextField's Document to limit the input to numeric or
Use an InputVerfier to verify that the input is OK
Or allow any input and verify it simply on JButton press or enter press using an ActionListener. This is the route I'd go as it's the most definitely the easiest to implement. Here in the ActionListener's actionPerformed you'd get the text from the field via myField.getText(), check if it is valid by whatever test you wish, and if not valid, clear the text via myField.setText("") and send an error message to the user via JOptionPane.sendMessage(...) dialog.

I want to provide a functionality to our users to printscreen and then use Ctrl+V to add image as attachment without saving image anywhere [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a GWT app and i want to provide a functionality to users simply press printscreen and then on pressing Ctrl+V it should automatically get attached as an image like gmail,skype etc.
There is an event for pasting:
com.google.gwt.user.client.Event.ONPASTE
I use this but only for pasting text (user must press Ctrl+V or right-click>Paste). I guess there may be a way for you to use this.
To capture the event, I sink it to my Widget first:
sinkEvents(Event.ONPASTE | Event.ONKEYPRESS | Event.ONKEYDOWN | Event.ONFOCUS);
Then, I implement onBrowserEvent(Event):
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (event.getTypeInt()) {
case Event.ONPASTE: paste(event);
}
}
Hope you can find a way to adapt this for images.

How to detect when a JButton is pressed (swing)? [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
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
}
}
});

MouseClicked Dilemma [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a if statement questioning if a user has clicked an image using the java method mouseClicked? If so how?
This is what I was thinking would be correct
public void mouseClicked(parameters of image){
//Sample Code
}
To be able to view an image, it must be loaded and placed on a Component (JPanel, JLabel, etc.) right? I'm assuming here since you made no mention of custom painting and provided no code. So just add a MouseListener to whichever Component has the image:
JLabel label = new JLabel();
// add the image to the label, then:
label.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
System.out.println("Image was clicked!");
}
});

Categories