I have an application that uses JTable and has this selected:
//The line below is setting ptm (AbstractTableModel instance) as the
//parameter for a new JTable instance.
table = new JTable(ptm);
table.setFillsViewportHeight(true);
//This is setting user selection to a single interval.
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
scroll = new JScrollPane(table);
Single_Interval_Selection. But I don't want to be able to drag the mouse and select multiple rows. How can I or whats the best approach to disabling the mouse drag on rows!
I thought of implementing MouseMotionListener which I thought was the best approach.
http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseMotionListener.html
This is how I solved the situation. I ended up finding out that their is a bug with adding a mouse listener to a jScrollPane. Instead you must add it to the view of the panel. You have to add the listener to the parent UI of the JTable.
scroll.getViewport().getView().addMouseMotionListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse has been dragged");
}
#Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Related
How we can use same method (same paramater and return type) with different implementation
in other word , in java Gui i want to use mouseClick method in many different way in one class , how this could be possible ?
You would implement different MouseListeners and add these to each component you want different mouseClick behavior in.
Edit: I added an example below.
public void example() {
JPanel panel1 = new JPanel();
panel1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
/* your code here, what should happen when the mouse clicked the panel */
}
});
JTable table1 = new JTable();
table1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
/* your code here, what should happen when the mouse clicked the table */
}
});
}
Here I used new MouseAdapter() more here instead of new MouseListener(), which implements MouseListener because this allows you to only implement a single method of the MouseListener interface.
I have a JScrollPane (myListScroll) that is added to a JPanel (which in turn is added to another JPanel before being added to a JFrame). This JScrollPane (myListScroll) consists of a list of strings. I want to be able to handle mouse events when clicking on the different items in this list.
In the code below I want to try if something happens if I click i the JScrollpane but nothing happens. What is wrong? Why is "test" not written?
JScrollPane myListScrol = new JScrollPane(myList);
myListScrol.getViewport().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("test");
}
});
I should have added the listener to myList and not to myListScrol.
I have added an image to a Jlabel and I want to add mouse listener to it.
I don't know to add a mouse listener to the Jlabel that contains the image.
So anyone who knows how to implement this please tell me.
I want to add a mousedragged listener to the JLabel.
When the user drags it, it should work.
MouseHandler mk = new MouseHandler();
JLabel label = new JLabel();
label.addMouseListener(mk);
I have implemented a listener in the class that extends mouse adapter.
You can try :
JLabel nameLabel = new JLabel("Name:");
nameLabel.addMouseMotionListener(new MouseMotionAdapter() {
//override the method
public void mouseDragged(MouseEvent arg0) {
// to do .........................
}
});
thats the way I understand your question.
But I guess this can help you too :
Drag and move a picture inside a JLabel with mouseclick
You are adding your mouse adapter as a mouse listener (which handles click, enter, exit, pressed, released). You want to add it as a mouse motions listener if you want to handle drag and move events.
You can do following:
ImageIcon icon = new ImageIcon("C:/image.jpg"); //Path to the image
JLabel label = new JLabel(icon); //add image to the label
label.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
You can add the actions to the above methods as required.
I can't seem to change the resize cursor of JSplitPane by calling setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); Does anyone know how to get around this? I am using Nimbus UI.
Calling setCursor on a JSplitPane component will set the cursor only for left & right (or top & bottom) components.
To set the cursor for the divider component, you can use:
Component divider = ((BasicSplitPaneUI)splitPane.getUI()).getDivider();
divider.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
We can add code for mouse listener in addPropertyChangeListener() listener of JSplitPane and after loading of GUI we can fire this event to bind mouse listener to divider. Here is my code:
splitPanehor.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, (pce) -> {
Component divider1 = ((JSplitPane) pce.getSource()).getComponent(2);
divider1.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
ExomDataGUI.f.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
}
#Override
public void mouseExited(MouseEvent e) {
ExomDataGUI.f.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
});
And we can fire this event after showing GUI in following way:
splitPanehor.firePropertyChange(JSplitPane.DIVIDER_LOCATION_PROPERTY, 219, 220);
Is it possible, in Java, when you hover over a single button, to make the program think you are hovering over multiple buttons?
I'm using a multi-dimensional array with buttons and want to be able to have 5 buttons be hovered over at a time. (All the buttons near the actual hover).
Any ideas on how to do this?
Note: I'm not using JButtons, just regular buttons. (awt.Button)
EDIT
I obviously wasn't clear enough, and I apologize for that.
Here is a screenshot of what I'm looking for:
So, the cursor is hovering over the first gray space, and all of the space next to it have a different background, however, they are not considered as being hovered over, which if what I need.
Assuming you are using a MouseListener, when the mouseEntered(MouseEvent e) method is called on the master button, explicitly call the same method on all of the listeners of all of the other buttons, passing the event you have been given. Ditto for the mouseExited(MouseEvent e) method.
It's up to you to maintain a reference from the master button to the subordinate buttons.
The subordinate buttons' listeners will receive an event that refers to the master button. If necessary, create your listeners with a reference to the button that they are attached to, so that you can operate on that button when receiving an event.
EDIT:
This is the kind of thing I'm talking about. Does it help?
final List<Button> subordinateButtons = Arrays.asList(new Button(), new Button(), new Button());
Button myButton = new Button();
myButton.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
for (Button subordinateButton : subordinateButtons) {
subordinateButton.setBackground(Color.GRAY);
}
}
public void mouseExited(MouseEvent e) {
for (Button subordinateButton : subordinateButtons) {
subordinateButton.setBackground(Color.LIGHT_GRAY);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
There's no reason why you can't keep a reference from a MouseListener to a List<Button>. If it's the business of the listener to work on those buttons then design your classes so that it happens.