JButton when clickedl leaves some border around text - java

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.

Related

Change button color after updating text field

There are 20 JTextField and one calculate button in panel.
after entering data to text field and I click button, program calculate as well.
What I want to do, after first click for button, is there any updates in any text fields, button colors should be change as green (it means you have to know something already change and you have to calculate again).
How can I proceed with that, should I put action listener for each text field or any other short code exist?
Your question is not clear. As far my concern to yr question you can track button click event and put code there for check jtextfield value and on the bases of value you can take decision to change the color of the button

Button remains focused after clicking

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.

How do I set a JButton component to be NOT clickable without setEnabled(false)?

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)

Auto scroll JTextArea on middle mouse button click

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

How do you remove the selection box in the GUI?

In any program, or at least most, when you select a button or anything, there is a selection box made of lines which are made of dots.
How do you get rid of that box?
The reason why I want to do this is because I have a button with an image, no contentFill, no borders, and it looks very awkward when selected.
Make the button or component non-focusable by calling its setFocusable(...) method:
myButton.setFocusable(false);
Edit
... or maybe even better:
myButton.setFocusPainted(false);
This way the component can still get the focus if need be, but just doesn't show the focus border.

Categories

Resources