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);
Related
I am trying to scroll down a drop down box. Altough mouseWheelListener is invoked, scrollBar is not moved and dropdown box is just closed after scrool event. Problem is value of scrollBar is not changed after setting in the event handler, it is always 0.
Here is the part of code:
public MyClas extends JPopupMenu implements AdjustmentListener, ContainerListener, ChangeListener{
public MyClass(){
.....
setLayout(new ScrollablePopupMenuLayout(this, defaultWidth));
JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
scrollBar.setFocusable(false);
scrollBar.addAdjustmentListener(this);
addContainerListener(this);
addMouseWheelListener(new MouseWheelListener(){
// Here is invoked after scrolling
public void mouseWheelMoved(MouseWheelEvent e){
{
int x = 1;
scrollBar.setValue(scrollBar.getValue() + x)
}
});
}
It worked after adding following line to the mouseWheelEvent method:
e.consume();
I have a frame where the layout is null. There is a panel attached to it that has a flow layout.
My code has it so that a button press creates a new panel that gets added to the first panel (the one attached to the frame). Then I have a mouse listener that lets you drag the newly created panel around.
panel.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
panel.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent me) {
me.translatePoint(me.getComponent().getLocation().x-x, me.getComponent().getLocation().y-y);
panel.setLocation(me.getX(), me.getY());
}
});
However, when I press the button for it to create a new panel, it creates a new panel while returning the it to the flow layout. I've tried removing the panel but when I drag the new panel over it, it gets erased. While if I revalidate and repaint the panel after removing it, it vanishes.
So how do I prevent it from getting erased or remove it from the layout only?
Try changing the layout to null, I don't think what you're asking is possible.
I want to move JSplitPane's divider to center if I double-click the divider.
So I added MouseListener to JSplitPane but it didn't work.
It works only when I double-clicked other JSplitPane's space without the divider.
Is there any way to work as I want?
Here is the code I failed
splitPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//super.mouseClicked(e);
splitPane.setDividerLocation(0.5);
}
});
(it work same when I use MouseListener)
The reason your code doesn't work is that the JSplitPane itself does not recieve the click event.
Instead, the UI class of JSplitPane does.
This code works:
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, component1, component2);
SplitPaneUI spui = split.getUI();
if (spui instanceof BasicSplitPaneUI) {
((BasicSplitPaneUI) spui).getDivider().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
split.setDividerLocation(0.5);
}
}
});
}
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
}
});
I need to change the color of text of tab of JtabbedPane on MouseOver.
Is is possible using the Mouse Listener or is there any different property to do that?
Thanks
Jyoti
There is not a built-in property or method to do this.
One option is to create a custom JLabel (or other component) add a MouseListener that would change the color on mouse entry/exit.
Example, something like this:
class CustomMouseOverJLabel extends JLabel{
public CustomMouseOverJLabel(String text) {
super(text);
addMouseListener(new MouseAdapter(){
#Override
public void mouseEntered(MouseEvent e) {
setForeground(Color.BLUE);
}
#Override
public void mouseExited(MouseEvent e) {
setForeground(Color.RED);
}
});
}
}
Then when you make a call to addTab(title, item), also set custom title components like so:
yourTabbedPane.setTabComponentAt(index, new CustomMouseOverJLabel("title"));
The tabbed pane tutorial has an example of tabs with custom components that might help.