Change JComboBox colourms how? - java

I would like to change the color of the selected combobox.
I do not know name of this event.
It is not a background or foreground, so what then?
More on Image:

A renderer works for the items in the dropdown.
The same renderer is also used to render the value displayed in the combo box when the index value is -1. However, the background color is controlled by the UI so you can't just override it. That is the
UIManager.getColor("ComboBox.selectionBackground");
property is used to control the background so you have a consistent LAF that indicates when the combo box has focus. I don't know of any easy way to control this behaviour. You would need to write a custom UI that sets the background based on the selected value.

You would have to write a new renderer for the combo box. There are some good examples here: http://www.java2s.com/Code/Java/Swing-JFC/AfancyexampleofJComboBoxwithacustomrendererandeditor.htm
Specifically you will be writing a new Cell Renderer. There's a good tutorial for that here: http://www.java2s.com/Tutorial/Java/0240__Swing/Comboboxcellrenderer.htm
I hope that helps.
Greg

Related

Size of cell-component is zero in Swing JTable

I’m currently writing a custom TableCellRenderer using a JEditorPane as cell-component for a JTable. I want to make hyperlinks clickable in that component.
Therefore, I wrote a tiny hack that forwards mouse events that are fired on the Table to the specific component. The problem is, that I cannot use the viewToModel function of the JEditorPane to determine the text under the mouse cursor, because the size of my editor pane is always zero and therefore viewToModel always returns -1.
Is there any way to get the components rendered in the table’s cells to have their correct sizes set? Do you have any suggestions on doing it in another way?
Some side information: I cannot use JTextPane since I'm using the scala programming language and there is currently no implementation for it. Furthermore, I have read that it may be possible to use the native event implementation by using custom cell editors. But I could not get this working in the first place. I want that, for instance, links are highlighted by hovering over. But, as far as I can see, that is not possible with the cell editor approach, since you have to click at the cell to start editing first.

LWUIT in J2ME: setting the style of a list cell when pressed

I have a LWUIT J2ME app with a List. I would like for the background of a cell to change when it is clicked.
The first thing I had to do was set the background transparency and color of the whole list when it is selected. Otherwise, the background of the form behind it shows through when the list is clicked. I did that with this code:
catList.getSelectedStyle().setBgTransparency(255);
catList.getSelectedStyle().setBgColor(0x23222a);
This seems to work fine. When clicked, the list bg remains the same color.
Now I want the background of the clicked cell to change color when it is pressed. I tried this (cellCon is a Container):
cellCon.getPressedStyle().setBgTransparency(255);
cellCon.getPressedStyle().setBgColor(0xFFFFFF);
cellCon.getSelectedStyle().setBgTransparency(255);
cellCon.getSelectedStyle().setBgColor(0xFFFFFF);
But it has no effect. How can I get the effect that I want?
Thanks!
You have to get the listCellRender component to set the style of the pressed cell. I think that you can do that trying list.getRender and after that, set the Style.
If you are building a custom Render, take a look on this LWUIT Blog ListCellRender There is a method called getListFocusComponent, there you can return a component (like a label) whit the custom style that you want for your focus.

Java SWT - Set ForeGround/Background Color of CTabItem (of CTabFolder)

I'm using a CTabFolder with several CTabItems. I would like to be able to set the background and foreground color of only a single CTabItem's tab.
There is an option for me to set the Font, but I can't find anything for the color or background of just the TabItem itself. Can anyone help?
Similar question was already addressed in How to correctly style borders of a CTabItem.
It's possible to set foreground (font) color and background (tabitem itself) color/gradient for selected and not selected (others) tabitems, see CTabFolder javadoc.
To do it just for one custom tabitem, you'll have to write your own CTabFolderRenderer, but it's really heavyweight solution.

using a JLabel+icon with JTable cellrenderer + selection highlight

I have a JTable with one column that has a custom cell renderer that shows one of several icons.
It works well except the selection highlight does not automatically appear, and I don't know how to apply a highlight in my custom cell renderer.
Any suggestions?
I have a JTable with one column that has a custom cell renderer that shows one of several icons.
JTable supports the display of Icons. Just add your Icon to the model and then override the getColumnClass(...) method to return Icon and the proper renderer will be used.
In your renderer code, you will have to explicitly set the background in case of selection. The usual way to do it is asking the UIManager to provide you the color for Table.background and Table.selectionBackground
In your getTableCellRendererComponent() method there is a parameter (boolean isSelected), that indicates when the row is selected. You will need to check that and do highlighting yourself in the renderer.

How do I "combine" JButton with JComboBox?

How do I "combine" JButton with JComboBox and place it on a JToolbar, as shown in this image:
?
You'll want to use a custom renderer I suspect, and have it display clickable buttons (with the appropriate actions attached, etc).
Just create regular combo box and put image as an item. I did it once. Unfortunately I do not have a source code here but as far as I remember it was not a problem. You have to implement your custom 1ListCellRenderer. Its methodgetListCellRendererComponent()` should return for example Label with your image.

Categories