Hi and thanks in advance!
I've been working on a game project and no I'm looking into making a basic GUI. To put it short this is what I'm trying to achieve:
The primary component is a J(Scroll)panel that houses moving in-game objects. I want the players to be able to hover the mouse over an object and get some kind of information popup that is tied to the location of the object hovered over. Secondly I want the user to be able to click on an object with the left mouse button in order to "select" the object and also to be able click with the right mouse button to open a popup menu next to the object.
You should add a mouse listener to your panel. Like this:
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
}
});
Related
I am doing a calculator app with swing. I have changed the background color of JButton but when clicked its color changes to a blueish color. Can I set a color manually on mouse over and on click. like the windows 10 default calculator?
Try using this:
buttonName.setFocusPainted(false);
This will get rid of the default border when you click on a button.
To make the button do stuff ie change it's background you can implement the MouseListener interface for it, but I think the best thing for you would be to read about event handling in swing :)
Here's a link
You need to first detect mouse motion over the button using an Mouse listener .You can use an implemented version of it called the MouseAdapter
button.addMouseMotionListener(new MouseAdapter()//To
detect mouse motion
{
#override
public void mouseEntered(MouseEvent m)//called when mouse
enters first time into the button
{
button.setBackground(Color.blue);
}
#override
public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
{
button.setBackground(//whatever you want);
}
});
Note for mouse clicks you can just change the color inside your action listener
This is how the program looks like (Gifs)
So i dont want to use tons of frames for my programs. I have many tables to show. So i made some buttons. When i click the tubbon ,it close other tables and show only one table. And when i click other button ,it close old one and show the new one. Code looks like this:
btn_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
table1_panel.setVisible(false);
table2_panel.setVisible(false);
table3_panel.setVisible(false);
table4_panel.setVisible(false);
table5_panel.setVisible(true);
}
});
So it close other panels and open only one panel. But i also need when i click the button "Detayları Görüntüle" , i want to open a frame with details of selected row. But when i click ,i need to know what table is visible so i can do different codes for every other tables. So i need something like this :
if(table5_panel.setVisible(true);){
}
But as you know it is not boolean so it does not works. What code i need to check which table is visible ?
Use a CardLayout to contain your panels.
Then you don't need to hide/show panels. The UI does the work for you.
i want to open a frame with details of selected row.
The button to process the table belongs on each panel, not on the menu.
The details displayed are different for each table, so the logic belongs with the panel so it can be customized for each table.
So I have a Java Swing program and I want to be able detect mouse clicks.
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
update(evt); //another method in the program
}
});
The code works if I click on the window's side or places where there isn't an object, but does not work when I click on objects in the JFrame, such as my JTable or my text field.
Please help me how to have the MouseListener work on objects inside the JFrame as well.
When you click on a textfield, the textfield gains focus. That means your frame loses ownership of focus, and since your listener is most likely added to your frame, your listener stops working right when frame isnt in focus. Add your listener to all compoments, or use Key Binding
I use a mouse move listener to handle mouse moves on a canvas in SWT.
But I'd like to have two behaviours, one when the mouse is moving into the canvas area and another one when the mouse is quiting the area.
In fact what I really like to do is to change the cursor icon when the mouse is in the area and restore it when it is out. But I can only capture events when the mouse is in the area.
Is there a specific listener to handle the Mouse Out event on a Canvas?
Thank you.
You are looking for a MouseTrackListener, it has methods for entering, exiting and hovering an element. See http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/events/MouseTrackListener.html
This is what you're looking for:
public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
}
Follow this link:
Oracle MouseListeners
I'm stuck with the following.
I'm programming a game and I need to get the mouse pointer location inside the JFrame, I don't need to get the pointer location on the screen, but just in the JFrame. When you use the MouseClick event you could get the position in the frame while the mouse button is pressed, but I want to get it's location when no button is pressed.
I hope you understood my question.
Sounds like you need a MouseMotionListener.
The MouseEvent contains the current mouse position.
void mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.