下面列出了javax.swing.JComboBox#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Create a choice menu.
* @param name The name used to identify the entry (when calling get).
* @param label The label to attach to the entry.
* @param values The list of possible choices.
* @param defaultChoice Default choice.
* @param editable True if an arbitrary choice can be entered, in addition
* to the choices in values.
* @param background The background color for the editable part.
*/
public void addChoice(
String name,
String label,
String[] values,
String defaultChoice,
boolean editable,
Color background) {
JLabel lbl = new JLabel(label + ": ");
lbl.setBackground(_background);
JComboBox combobox = new JComboBox(values);
combobox.setEditable(editable);
// FIXME: Typical of Swing, the following does not set
// the background color. How does one set the background
// color?
combobox.setBackground(background);
combobox.setSelectedItem(defaultChoice);
_addPair(name, lbl, combobox, combobox);
// Add the listener last so that there is no notification
// of the first value.
combobox.addItemListener(new QueryItemListener(name));
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
List<String> usedTypes = new LinkedList<String>();
for (int i = 0; i < Ontology.VALUE_TYPE_NAMES.length; i++) {
if ((i != Ontology.ATTRIBUTE_VALUE) && (i != Ontology.FILE_PATH)
&& (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(i, Ontology.DATE_TIME))) {
usedTypes.add(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(i));
}
}
String[] valueTypes = new String[usedTypes.size()];
int vCounter = 0;
for (String type : usedTypes) {
valueTypes[vCounter++] = type;
}
JComboBox<String> typeBox = new JComboBox<>(valueTypes);
typeBox.setBackground(javax.swing.UIManager.getColor("Table.cellBackground"));
return new DefaultCellEditor(typeBox);
}
private static JComboBox<String> buildProductFormatNamesComboBox(Insets defaultListItemMargins, int textFieldPreferredHeight, String[] availableFormatNames) {
JComboBox<String> productFormatNameComboBox = new JComboBox<String>(availableFormatNames);
Dimension formatNameComboBoxSize = productFormatNameComboBox.getPreferredSize();
formatNameComboBoxSize.height = textFieldPreferredHeight;
productFormatNameComboBox.setPreferredSize(formatNameComboBoxSize);
productFormatNameComboBox.setMinimumSize(formatNameComboBoxSize);
LabelListCellRenderer<String> renderer = new LabelListCellRenderer<String>(defaultListItemMargins) {
@Override
protected String getItemDisplayText(String value) {
return value;
}
};
productFormatNameComboBox.setMaximumRowCount(5);
productFormatNameComboBox.setRenderer(renderer);
productFormatNameComboBox.setBackground(new Color(0, 0, 0, 0)); // set the transparent color
productFormatNameComboBox.setOpaque(true);
return productFormatNameComboBox;
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
JComboBox<String> typeBox = new JComboBox<>(Attributes.KNOWN_ATTRIBUTE_TYPES);
typeBox.setEditable(true);
typeBox.setBackground(javax.swing.UIManager.getColor("Table.cellBackground"));
return new DefaultCellEditor(typeBox);
}
private static<T> void setComboBoxProperties(JComboBox<T> comboBox, ImageInfoReader imageInfoReader) {
comboBox.setOpaque(false);
comboBox.setBackground(ColorPalette.FOREGROUND_COLOR);
comboBox.setForeground(ColorPalette.FOREGROUND_COLOR);
comboBox.setFont(Fonts.FONT);
comboBox.setUI(new MetalComboBoxUI() {
@Override
protected ComboPopup createPopup() {
return new TiledImageComboPopup( comboBox, imageInfoReader );
}
});
}
public static JComboBox<String> createJComboBox(String[] items, int width, int height, String toolTip) {
JComboBox<String> comboBox = new JComboBox<String>(items);
comboBox.setPreferredSize(new java.awt.Dimension(width, height));
comboBox.setBackground(java.awt.Color.white);
comboBox.setFont(JPlagCreator.SYSTEM_FONT);
if (toolTip != null)
comboBox.setToolTipText(toolTip);
return comboBox;
}
public JComboBox addChoice(String label, String[] items, int selected, Setter setter) {
addLabel(label);
JComboBox choice = new JComboBox(items);
choice.setBackground(Color.white);
choice.setSelectedIndex(selected);
choice.addActionListener(tl);
all.add(choice);
addField(choice, setter);
return choice;
}
public URLImportDialog(SwingClientApplication client) {
super(client.getMainFrame(), true);
if(recentURLs == null){
recentURLs = new LinkedList<String>();
}
this.client = client;
this.setTitle("Import data from URL");
this.setModal(true);
label = new JLabel("Insert URL");
label.setFont(label.getFont().deriveFont(Font.BOLD));
label.setPreferredSize(LABEL_SIZE);
folderNameCombo = new JComboBox(ImportUtils.getFolderNames(true).toArray());
folderNameCombo.setEditable(true);
skipCheckBox = new JCheckBox(VisualConstants.getImportDirectlyText());
if (!Session.getSession().getApplication().isStandalone()) {
skipCheckBox.setSelected(true);
} else {
skipCheckBox.setSelected(false);
}
okButton = new JButton("OK");
okButton.setPreferredSize(BUTTON_SIZE);
okButton.addActionListener(this);
cancelButton = new JButton("Cancel");
cancelButton.setPreferredSize(BUTTON_SIZE);
cancelButton.addActionListener(this);
String[] comboValues = new String[recentURLs.size() + 1];
comboValues[0] = "http://";
for(int i = 0; i < recentURLs.size(); i++){
comboValues[i+1] = recentURLs.get(i);
}
URLComboBox = new JComboBox(comboValues);
URLComboBox.setBackground(Color.WHITE);
URLComboBox.setPreferredSize(COMBO_SIZE);
URLComboBox.setEditable(true);
GridBagConstraints c = new GridBagConstraints();
this.setLayout(new GridBagLayout());
// Label
c.anchor = GridBagConstraints.WEST;
c.insets.set(10, 10, 5, 10);
c.gridx = 0;
c.gridy = 0;
this.add(label, c);
// Combobox
c.insets.set(0,10,10,10);
c.gridy++;
this.add(URLComboBox, c);
c.insets.set(10,10,5,10);
c.gridy++;
this.add(new JLabel("Insert in folder"),c);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.set(0,10,10,10);
c.gridy++;
this.add(folderNameCombo,c);
c.insets.set(10, 10, 10, 10);
c.anchor = GridBagConstraints.EAST;
c.gridy++;
this.add(skipCheckBox,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy++;
JPanel keepButtonsRightPanel = new JPanel();
keepButtonsRightPanel.add(okButton);
keepButtonsRightPanel.add(cancelButton);
this.add(keepButtonsRightPanel, c);
this.pack();
this.setLocationRelativeTo(null);
URLComboBox.requestFocusInWindow();
this.setVisible(true);
}