MouseClicked Dilemma [closed] - java

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!");
}
});

Related

How to change a color of string/text in UI application build in java swing? [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 9 months ago.
Improve this question
I have a string/text that I want to show on UI made with java swing. I need to color that text. I was using HTML to color my string and it was working fine till now. But now we have to stop using HTML for coloring string for some reason. I am looking for other alternatives.
My existing approach was something like this:
public class dummy{
public static void main(String []args){
System.out.println("<html><font color=red>"+"This is my simple text" +"</font></html>");
}
}
I have checked for JTREE Render, but that's for tree node coloring and that's not suiting my scenario. Any other suggestion is most welcomed.
This may help:
JButton b = new JButton("<html><center><b><u>D</u>isable</b><br>"
+ "<font color=#ffffdd>middle button</font>",
leftButtonIcon);
b.setForeground(Color.RED);
b.setBackground(Color.WHITE);
Set color using setForeground and setBackground methods
How to Use HTML in Swing Components

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 (ยง).

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 load all control variable names and then edit their properties [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 want to load all the control variables for and example
Button1
Button2
Button3
And then Edit their properties like
Button1.settext() ;
Please tell me is their a way to do this .
Assuming you have all those labels in the same frame, you can try the following:
Arrays.asList(myframe.getComponents()).forEach((c) -> {
if( c instanceof JLabel) {
JLabel l = (JLabel)c;
//do whatever you'd like to do with your label here...
}
});

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);
}
});

Categories