java swing的可输入可多选的下拉框
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java swing的可输入可多选的下拉框相关的知识,希望对你有一定的参考价值。
import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.swing.BorderFactory; import javax.swing.CellRendererPane; import javax.swing.DefaultListSelectionModel; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JTextField; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; import javax.swing.plaf.basic.BasicComboPopup; import javax.swing.plaf.basic.ComboPopup; import javax.swing.plaf.metal.MetalComboBoxUI; import javax.swing.text.Document; import javax.swing.text.JTextComponent; public class MyJComboBox { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setLocationRelativeTo(null); ReJComboBox reJComboBox = new ReJComboBox(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "4", "5", "6", "1", "2", "3", "4", "5", "6" }); reJComboBox.setPreferredSize(new Dimension(150, 26)); reJComboBox.setBorder(BorderFactory.createLineBorder(Color.GREEN)); reJComboBox.setEditable(true); jTextFieldall =(JTextField) reJComboBox.getEditor().getEditorComponent(); frame.add(reJComboBox); frame.setVisible(true); } //************************重写JComboBox组件以便于直接使用 protected static JTextField jTextFieldall; public static class ReJComboBox extends JComboBox { protected Set set = new HashSet(); private ReJComboBox(String[] string) { super(string); setUI(new ReMetalComboBoxUI()); setRenderer(new ReJCheckBox()); } public String Stringmanipulation() { StringBuilder sb; String spliceRegex = ","; List list = new ArrayList(set); Collections.sort(list); sb = new StringBuilder(); for (int i=0;i<list.size();i++) { Object obj = getModel().getElementAt( Integer.parseInt(list.get(i).toString())); sb.append(obj == null ? "" : obj).append(spliceRegex); } if(sb.length()>1) sb.setLength(sb.length()-1); return sb.toString(); } } public static class ReBasicComboPopup extends BasicComboPopup { public ReBasicComboPopup(JComboBox combo) { super(combo); } protected void configureList() { super.configureList(); list.setSelectionModel(new DefaultListSelectionModel() {public boolean isSelectedIndex(int index) { return ((ReJComboBox) comboBox).set.contains(Integer.valueOf(index)); }}); } protected MouseListener createListMouseListener() { return new ReMouseAdapter(list,comboBox); } } //************************重写鼠标监听事件 private static class ReMouseAdapter extends MouseAdapter { protected JList jList; protected JComboBox jComboBox; private ReMouseAdapter(JList list, JComboBox comboBox) { this.jList = list; this.jComboBox = comboBox; } public void mousePressed(MouseEvent anEvent) { int index = jList.getSelectedIndex(); ReJComboBox reJComboBox = (ReJComboBox) jComboBox; if (jList.getSelectionMode() == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) if (reJComboBox.set.contains(Integer.valueOf(index))) reJComboBox.set.remove(Integer.valueOf(index)); else reJComboBox.set.add(Integer.valueOf(index)); else if (!reJComboBox.set.contains(Integer.valueOf(index))) { reJComboBox.set.clear(); reJComboBox.set.add(Integer.valueOf(index)); } jComboBox.repaint(); jList.repaint(); } } //************************重写MetalComboBoxUI弹出框 public static class ReMetalComboBoxUI extends MetalComboBoxUI { public void paint( Graphics g, JComponent c ) { Rectangle r = rectangleForCurrentValue(); paintCurrentValueBackground(g,r,hasFocus); paintCurrentValue(g,r,hasFocus); } public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) { JTextField jTextField = new JTextField(); jTextField.setText(((ReJComboBox) comboBox).Stringmanipulation()); jTextField.setFont(comboBox.getFont()); jTextField.setOpaque(comboBox.isOpaque()); if (comboBox.getRenderer() instanceof JComponent) { JComponent r = (JComponent) comboBox.getRenderer(); jTextField.setBorder(r.getBorder()); } int x = bounds.x, y = bounds.y, w = bounds.width, h = bounds.height; if (padding != null) { x = bounds.x + padding.left; y = bounds.y + padding.top; w = bounds.width - (padding.left + padding.right); h = bounds.height - (padding.top + padding.bottom); } new ReCellRendererPane(currentValuePane).paintComponent(g, jTextField, comboBox, x, y, w, h, true); } protected ComboPopup createPopup() { return new ReBasicComboPopup(comboBox); } } //************************重写录入框 public static class ReCellRendererPane extends CellRendererPane { protected CellRendererPane cellRendererPane; private ReCellRendererPane(CellRendererPane cellRendererPane) { this.cellRendererPane = cellRendererPane; } public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) { if (c == null) { if (p != null) { Color oldColor = g.getColor(); g.setColor(p.getBackground()); g.fillRect(x, y, w, h); g.setColor(oldColor); } return; } Document doc = ((JTextComponent)c).getDocument(); if (c.getParent() != this) { this.add(c); } c.setBounds(x, y, w, h); if(shouldValidate) { c.validate(); } boolean wasDoubleBuffered = false; if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) { wasDoubleBuffered = true; ((JComponent)c).setDoubleBuffered(false); } jTextFieldall.setDocument(doc); Graphics cg = g.create(x, y, w, h); try { c.paint(cg); } finally { cg.dispose(); } if (wasDoubleBuffered && (c instanceof JComponent)) { ((JComponent)c).setDoubleBuffered(true); } c.setBounds(-w, -h, 0, 0); } } //************************修改弹出框的选单样式 public static class ReJCheckBox extends JCheckBox implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setComponentOrientation(list.getComponentOrientation()); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setEnabled(list.isEnabled()); setSelected(isSelected); setText(value == null ? "" : value.toString()); setFont(list.getFont()); return this; } } }
以上是关于java swing的可输入可多选的下拉框的主要内容,如果未能解决你的问题,请参考以下文章
javascript 可多选的下拉框 multiselect 动态删除option值,动态添加option值,动态生成表格