I am wondering if it's possible to change the default radio button menu item appearance in Java Swing.
By default a circle with a dot inside will indicate the selected state of the button, but i just want the good old fashioned tick next to a selected menu item and nothing to be shown next to an item that is not selected. (All items in question are of type JRadioButtonMenuItem)
I tried to use .setSelectedIcon(...) which can be found here :
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setSelectedIcon(javax.swing.Icon)
But nothing changed, no exceptions thrown and im still stuck with the default appearance.
Any ideas?
Instead of using a JRadioButtonMenuItem, you could use JCheckBoxMenuItem which has a tick mark by default. JCheckBoxMenuItems can also belong to a ButtonGroup also giving you single selection behavior shown by JRadioButtonMenuItem.
Example
Related
I want to extend the JButton class and manually paint the button's icon, though I need to get the correct icon according to the state the button is in, how do i do that?
Method getIcon() returns the default icon only, regardless of what state the button is in...
Instead, implement the Icon interface. ColorIcon, illustrated here, is a simple example used by a JButton subclass. Try instantiating ColorIcon for each of several colors . Use the Icon instances as the the button's pressed or rollover icon to see the effect. See also this related example.
getIcon() will return the default icon, but getDisabledIcon() will return the disabled icon.
Also
getDisabledSelectedIcon() returns the icon used by the button when it's disabled and selected. If no disabled selection icon has been set, this will forward the call to the LookAndFeel to construct an appropriate disabled Icon from the selection icon if it has been set and to getDisabledIcon() otherwise.
Some look and feels might not render the disabled selected Icon, in which case they will ignore this.
Then there is getPressedIcon(), getRollOverIcon() and getRolloverSelecedIcon().
Check the AbstractButton manual page.
As you can see there are many options available to you. On the other hand, you probably are the one who has set the icons, so if you may keep them in an Icon array, you could get them straight from there.
Is there a way to make radio buttons do nothing when they are clicked? I am trying to make a Score Board and need a way to show the periods/halves/quarters ect. The radio buttons will be selected by the program to display periods/halves/quarters ect. Is this a good way to do it or is there a better way?
Check out the clickable attribute for Views
Defines whether this view reacts to click events.
Must be a boolean value, either "true" or "false".
http://developer.android.com/reference/android/view/View.html#attr_android:clickable
If the user doesn't ever need to click the radio buttons (ie if its always controlled by the program), why don't you just use images instead of radio buttons.
The benefits of using images for this are...
Images aren't clickable by default
It'll look nicer (you can make the Images look like a real scoreboard)
It doesn't rely on the default Android appearance (which is different for each phone)
I have checkboxes. I have a buttongroup. When I click on the option "All Checkboxes," I want all checkboxes in the button group to be made unselectable (grayed out) (Except the "All Checkboxes" one which is selected.) How do I this?
Also, how can I add an item/set the items to a Combo Box using NetBeans?
I have checkboxes. I have a buttongroup. When I click on the option "All Checkboxes," I want all checkboxes in the button group to be made unselectable (grayed out) (Except the "All Checkboxes" one which is selected.) How do I this?
A ButtonGroup cannot be used to grey-out (functionally and visibly disable) a JCheckBox. Better to put them in a List<JCheckBox> and in the ActionListener for "All CheckBoxes" iterate through calling setEnabled(true/false) on the items in the list, the parameter depending on the state of the "All Checkboxes" JCheckBox.
Also, how can I add an item/set the items to a Combo Box using NetBeans?
I have no idea how to do this "using NetBeans", but using Swing you simply get the JComboBox's model, usually a DefaultComboBoxModel, and add items to it.
Also, how can I add an item/set the items to a Combo Box using NetBeans?
Just like you would with Swing normally, comboBox.setModel(comboBoxModel). Don't rely on the form editor to do it for you. Somethings you just have to get your hands dirty with.
I'd like to change the color of the java titlebar and add some text to the ends and the middle.
The previous coder used setUndecorated(false) and a JPanel to achieve this effect but I am trying to change this to modify the actual title bar because the panel solution is an issue with menus and focus.
tl;dr Want to change the color of the titlebar and set text in the middle and one the ends.
I think you can change the title bar color with the method described here: http://www.coderanch.com/t/346141/GUI/java/set-JFrame-titlebar-color
Changing the text should be possible via setTitle(). You can call this method as many times as you want throughout the life of the application to change the title text on the fly.
I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.
The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:
Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).
So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.
with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
You can use the older approach of displaying the popup based on your own custom MouseListener.
See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.