📄 multivaluetextfield.java
字号:
package crms.ui;import javax.swing.*;import java.awt.*;import java.util.*;import java.awt.event.*;public class MultiValueTextField extends JPanel { protected JComboBox combo = new JComboBox(); protected JButton delButton = new JButton("X"); ArrayList values = null; public MultiValueTextField() { setLayout(new BorderLayout()); combo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String command = e.getActionCommand(); if (command.compareToIgnoreCase("comboBoxEdited") != 0) return; // new item if (cb.getSelectedIndex() == -1) { String selected = (String)cb.getSelectedItem(); if (selected.length() == 0) return; if (values == null) values = new ArrayList(); values.add(selected); cb.addItem(selected); cb.setSelectedItem(""); } } } ); delButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int index = combo.getSelectedIndex(); if (index >= 0) { String value = (String)combo.getSelectedItem(); combo.removeItem(value); values.remove(values.indexOf(value)); } if (values == null) return; if (values.size() > 0) { combo.setSelectedItem(values.get(0)); } else { combo.setSelectedItem(""); } } } ); delButton.setToolTipText("Remove the currently selected value for the field."); add(combo, BorderLayout.CENTER); add(delButton, BorderLayout.EAST); // default to editable as we *are* a text field setEditable(true); } public void addActionListener(ActionListener listener) { combo.addActionListener(listener); } public void setValues(ArrayList values) { this.values = values; combo.removeAllItems(); if (values == null) return; for (int i = 0; i < values.size(); i++) { combo.addItem(values.get(i)); } } public ArrayList getValues() { return this.values; } public void setEditable(boolean editable) { // always want to use the editor text component combo.setEditable(true); // control the text component's editable status ((JTextField)combo.getEditor().getEditorComponent()).setEditable(editable); delButton.setVisible(editable); String tip = ""; if (values != null && values.size() > 0) tip += values.size() + " items. "; if (editable == true) tip += "Type in a value and press return to add to list."; combo.setToolTipText(tip); } public void setEnabled(boolean enabled) { combo.setEnabled(enabled); delButton.setEnabled(enabled); } public void setBackground(Color color) { super.setBackground(color); if (combo == null) return; ((JTextField)combo.getEditor().getEditorComponent()).setBackground(color); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -