|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
[Java] Color JComboBox...
...stavo customizzando i colori per una combo box...ora il cruccio sta nel fatto che la parte che visualizza il selectedItem non viene influenzato dai colori...come mai?...per quanto la lista sia cromaticamente giusta qualsiasi scelta si faccia verrà mostrato il colore di default...
...la comboBox... Codice:
Object elements[][] = {
{ Color.RED, "Cd" },
{ Color.BLUE,"Dvd" },
{ Color.GREEN, "Game" }};
ListCellRenderer renderer = new CellRenderer();
typeComboBox = new JComboBox(elements);
typeComboBox.setRenderer(renderer);
Codice:
class CellRenderer implements ListCellRenderer {
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) {
Font theFont = null;
Color theForeground = null;
String theText = null;
JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,isSelected, cellHasFocus);
if (value instanceof Object[]) {
Object values[] = (Object[]) value;
theForeground = (Color) values[0];
theText = (String) values[1];
}
if (!isSelected)
renderer.setBackground(theForeground);
if (isSelected)
renderer.setBackground(theForeground);
renderer.setText(theText);
renderer.setFont(theFont);
return renderer;
}
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Ciao ally, penso che tu debba impostare la proprietà "opaque" a true per la JLabel che usi come renderer, che di default è impostata a false, se non erro.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#3 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...non sembra funzionare...setOpaque blocca la visualizzazione dei colori per l'elenco...non per l'elemento attivo della comboBox...
|
|
|
|
|
|
#4 | ||
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Quote:
Leggo i javadoc e noto che JComboBox ha sia un ListCellRenderer che un ComboBoxEditor. Stando alla documentazione: Codice:
public void setEditor(ComboBoxEditor anEditor)
Sets the editor used to paint and edit the selected item in the JComboBox
field. The editor is used only if the receiving JComboBox is editable.
If not editable, the combo box uses the renderer to paint the selected item.
Parameters:
anEditor - the ComboBoxEditor that displays the selected item
Dato che le implementazioni di ComboBoxEditor dipendono dal Look and Feel (vedo implmentazioni di ComboBoxEditor solo in javax.swing.plaf) potresti "mettere le mani" su quello in uso, invocando getEditor sul JComboBox, per poi invocare su di esso il metodo getEditorComponent e al Component così ottenuto potresti impostare il backgrond/foreground in base all'item selected del JComboBox (dovrai aggarciare un action listener per intercettare ogni cambio di selezione dell'item selected). Ci ho azzecato sta volta?
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 13-04-2011 alle 15:41. |
||
|
|
|
|
|
#5 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...si ora funge...grazissime...solo un appunto : si deve settare il parametro setEditable a true o la comboBox resta immutata...
Codice:
typeComboBox.setEditable(true); |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
*edit
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 13-04-2011 alle 16:37. Motivo: rimosse idee sparse e confuse... lol |
|
|
|
|
|
#7 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:08.




















