How to auto scroll JTextArea on middle mouse button click? To be clear, when we click the mouse wheel (middle button) in Firefox (for example) and push it down/up we will be able to scroll the page automatically when we release the middle button, we can alo see a round icon at the point of middle button click.
Here is a screenshot of it.
I think there is no such functionality for JTextArea in Java. Is there any hack to implement it.
Any answer is appreciated.
To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you:
isLeftMouseButton
isMiddleMouseButton
isRightMouseButton
Based on the mouse button clicked, you can take appropriate action to scroll the JTextArea programmatically. Probably, you can use something like this:
textArea.setCaretPosition(textArea.getDocument().getLength()); -> to move to the end of the JTextArea
You can look at this link to get a good idea of positioning the cursor position in JTextArea
Related
I'm getting some trouble with a toolbar I made. In this I have 4 JButtons and below a JTable, which is set on focus when my windows shows in order to see the JButtons unfilled.
This is how I set each JButton:
btn = new JButton("New");
btn.setFocusPainted(false);
btn.setBackground(SystemColor.window);
btn.setVerticalTextPosition(SwingConstants.BOTTOM);
btn.setHorizontalTextPosition(SwingConstants.CENTER);
btn.addActionListener(new ActionListener() {
...
});
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
toolBar_General.add(btn);
btn.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/file11.png")));
Example of my toolbar at start: http://www.tiikoni.com/tis/view/?id=0ef950b
But after clicking on a JButton, it remains filled after itsactionListener does his work.
Example of my toolbar after clicking a JButton: http://www.tiikoni.com/tis/view/?id=9e88ea4
My question is: how can I mantain my buttons contentAreaFilled property true (so the user can tab between components in my window and see which control he has selected) and unfocus my button after the user click?
The result I would like to have is that of the first photo.
I think the call you are looking for is:
btn.setRequestFocusEnabled(false);
this will stop your buttons from taking over the focus when the mouse clicks them, achieving the goal of not having the keyboard focus painted for mouse users. However it still lets keyboard-centric users "tab over" to them.
If one of your buttons gets the initial focus, however, then the keyboard focus will still render there. In that case, you probably want to select some other sensible component to place the initial keyboard focus onto by calling:
otherComponent.requestFocusInWindow();
Are you talking about the input Focus? (the dotted rectangle a component shows when it has the focus)
You can explicitly disable a component getting the focus by calling setFocusable(false) on the component (e.g. every button). It does not prevent you from clicking on the buttons, but it does prevent you from cycling the focus with the tab-key to it.
I have been searching on the web for this but have not found a solution.
I have a 2D array of JButtons. When the user clicks the button, my application currently adds an ImageIcon to that button and does setEnabled(false) to the button.
The problem with this is that it greys out the image. I want the image to stay in full colour but I want to make the JButton not clickable. It would seem that setEnabled() is not the method that I want.
I have considered removing the ActionListener from it yet this would still allow the action of a click (even though nothing would happen).
How do I make a JButton unclickable without disabling it or removing the ActionListener?
JButton derives disabled icon by graying out original icon. if you want the button to be disabled but still the icon in full color, set its disabled icon explictly to full color image using following method
button.setDisabledIcon(button.getIcon());
checkout the javadoc for AbstractButton#setDisabledIcon(Icon)
I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon().
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.
I am having one simple form i created using JFrame.
In which i used one small JButton in which i set text only "X", now the problem is that when i press that button some border appears around the text which should not appear.
So, how can i resolve it?
How can i remove this border.I mean to say when the user clicks then also it should not show this border.
Here Try to look at the button at the corner with red background.
Looks like a keyboard focus indicator. If you want that never appear call setFocusable(false) for that button.
I am writing a game in java and it has a grid view where you can left click on a single box (the box becomes red). How can make it so that after left clicking on a box if I keep pressing the left click and move the cursor onto another box it will keep clicking. Any suggestions ? I hope I was clear. Thanks.
You need to add a MouseMotionListener to your component.
The mouseDragged method will be called when the user moves the mouse whilst holding a mouse button down. You can then check if the cursor is over another box and fill it red.