How do you remove the selection box in the GUI? - java

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.

Related

How to Make a JList with "x" Buttons in Each Cell

I would like to make a custom component that is much like a JList, except there is a little "x" button on the right side of each cell that removes that cell from the list (and triggers an event). I know that you would have to extend JList, but looking at the code for JList I have no idea where to go from there. For reference, I would like the list to be like on the macOS Messages app (except the "x" button can always be visible, not just when the mouse is over the cell).
I would like to make a custom component
I suggest you do that by extending JPanel and adding real components to your panel. Then you can actually add JButton with the "x" that can respond to the mouse event.
A JList does not display real components, only rendered images of the component and therefore is does not respond to events if you try to click on the "x".
The other option is to use a JTable. A JTable does allow you to display values in a column format. In this case it does support the concept of editors, which would allow you to add a button to a column. For example check out Table Button Column.

How can I prevent a CodenameOne GUI Element from scrolling (tickering?) when it changes to Bold?

I am building my first CodenameOne app using the GUI Builder. I've defined a style for my buttons where the "unselected" state is regular text and the "selected" or focus state has Bold text.
The problem I'm seeing is that once a button gains focus, the bold text is slightly wider than the original text and it starts scrolling (I think the effect is referred to as "tickering"):
Unselected Button Image
Selected Button Image
I see this in the simulator and on my Samsung SG6 testing. Is this a bug or is there something I can do to pre-size it for the bold text first so it doesn't start scrolling when it gets focus? I don't want to make the button larger than it needs to be (ie: fit it to a container)
You need to do 2 things,
first reduce the left and right padding of the selected button UIID and then call this in your code
mybutton.setEndsWith3Points(false);
mybutton.setTickerEnabled(false);
As an extra, place the button in a container with the right layout.
Note that since the text is now Bold, it's normal for the button to gain extra width. So I will suggest you make a room for that.
Edit:
I noticed that the button was tickering when you took the snapshot so ignore the first point about padding.

Java not focusing any component

I use a c1.setFocusable(false) so that my Checkbox c1 is not focused as shown in the pic and after that, focus goes to the next Checkbox c2, I use again c2.setFocusable(false) and it continues to all Checkboxes. Even if it doesn't find one, it continues to the next JTextField. Is there a way to not let the focus go to something instead of setting focusable to all my numerous components?
how can I get rid of that grey border thing?
Why would you want to do that? That "grey border thing" is the focus indicator. It is used to provide information to the user so the user knows which component has focus.
If you really want to confuse the user and make them guess which component has focus then you can use:
button.setFocusPainted( false );

JButton when clickedl leaves some border around text

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.

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src

Categories

Resources