【问题标题】:Remove JCombobox border inside JTable删除 JTable 内的 JCombobox 边框
【发布时间】:2016-12-19 02:31:20
【问题描述】:

我从JComoboBox 中删除了箭头按钮,使其看起来像JTextField,并将其添加为单元编辑器。目的是创建一个 AutoSuggest(不是 AutoComplete)JTable 单元格。

这样做边框看起来有点烦人。如何更改边框以使其看起来像右侧的文本字段边框。我试过删除边界创建的线边界。但它并没有消除蓝色边框。

使用 Nimbus 用户界面。

针对问题的 MCVE

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Sample extends JFrame {

    public Sample() {
        init();
    }

    private void init() {
        JTable table = new JTable(5, 5);
        DefaultCellEditor cellEditor = new DefaultCellEditor(new EditorCombo());
        cellEditor.setClickCountToStart(2);
        table.getColumnModel().getColumn(0)
                .setCellEditor(cellEditor);
        table.setRowHeight(30);
        table.setCellSelectionEnabled(true);
        add(new JScrollPane(table));
    }

    public static void main(String[] args) {
        setUpUI("Nimbus");
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Sample samp = new Sample();
                samp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                samp.setTitle("Table Test");
                samp.pack();
                samp.setLocationRelativeTo(null);
                samp.setVisible(true);
            }
        });
    }

    private static void setUpUI(String ui) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if (ui.equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    class EditorCombo extends JComboBox {

        public EditorCombo() {
            setEditable(true);
            for (int i = 0; i < 10; i++) {
                addItem("Sample" + i);
            }
            setUI(new javax.swing.plaf.synth.SynthComboBoxUI() {
                @Override
                protected JButton createArrowButton() {
                    JButton button = new JButton() {
                        @Override
                        public int getWidth() {
                            return 0;
                        }
                    };
                    button.setBorder(BorderFactory.createEmptyBorder());
                    button.setVisible(false);
                    return button;
                }

                @Override
                public void configureArrowButton() {
                }
            });
        }

    }
}

【问题讨论】:

    标签: java swing jcombobox tablecelleditor nimbus


    【解决方案1】:

    使用 Nimbus 用户界面

    也许这会对你有所帮助:Nimbus Defaults (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)

    • TextField[Focused].borderPainter
    • TextField[Enabled].borderPainter
    • ComboBox:\"ComboBox.textField\"[Enabled].backgroundPainter
    • ComboBox:\"ComboBox.textField\"[Selected].backgroundPainter
    import java.awt.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    
    public class Sample2 extends JFrame {
      public Sample2() {
        init();
      }
    
      private void init() {
        JTable table = new JTable(5, 5);
        DefaultCellEditor cellEditor = new DefaultCellEditor(new EditorCombo());
        cellEditor.setClickCountToStart(2);
        table.getColumnModel().getColumn(0).setCellEditor(cellEditor);
        table.setRowHeight(30);
        table.setCellSelectionEnabled(true);
        add(new JScrollPane(table));
      }
    
      public static void main(String[] args) {
        setUpUI("Nimbus");
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            Sample2 samp = new Sample2();
            samp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            samp.setTitle("Table Test");
            samp.pack();
            samp.setLocationRelativeTo(null);
            samp.setVisible(true);
          }
        });
      }
    
      private static void setUpUI(String ui) {
        try {
          for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if (ui.equals(info.getName())) {
              UIManager.setLookAndFeel(info.getClassName());
              break;
            }
          }
        } catch (ClassNotFoundException | InstantiationException
                 | IllegalAccessException | UnsupportedLookAndFeelException ex) {
          Logger.getLogger(Sample2.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }
    
    class EditorCombo extends JComboBox<String> {
      public EditorCombo() {
        super();
        setEditable(true);
        for (int i = 0; i < 10; i++) {
          addItem("Sample" + i);
        }
      }
      @Override public void updateUI() {
        //super.updateUI();
        setUI(new javax.swing.plaf.synth.SynthComboBoxUI() {
          @Override
          protected JButton createArrowButton() {
            JButton button = new JButton() {
              @Override
              public int getWidth() {
                return 0;
              }
            };
            button.setBorder(BorderFactory.createEmptyBorder());
            button.setVisible(false);
            return button;
          }
          @Override
          public void configureArrowButton() {
          }
        });
        UIDefaults d = new UIDefaults();
    //     putClientProperty("Nimbus.Overrides", d);
    //     putClientProperty("Nimbus.Overrides.InheritDefaults", false);
    //     JComponent c = (JComponent) getEditor().getEditorComponent();
    //     c.putClientProperty("Nimbus.Overrides", d);
    //     c.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
    //     c.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    
        Painter<JComponent> emptyPainter = new Painter<JComponent>() {
          @Override public void paint(Graphics2D g, JComponent c, int w, int h) {
            /* Empty painter */
          }
        };
        d.put("TextField.borderPainter", emptyPainter);
        d.put("TextField[Enabled].borderPainter", emptyPainter);
        d.put("TextField[Focused].borderPainter", emptyPainter);
        d.put("ComboBox:\"ComboBox.textField\"[Enabled].backgroundPainter", emptyPainter);
        d.put("ComboBox:\"ComboBox.textField\"[Selected].backgroundPainter", emptyPainter);
        d.put("ComboBox[Editable+Focused].backgroundPainter", emptyPainter);
        putClientProperty("Nimbus.Overrides", d);
        JComponent c = (JComponent) getEditor().getEditorComponent();
        c.putClientProperty("Nimbus.Overrides", d);
        c.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-21
      • 2011-03-11
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2012-05-03
      • 2020-01-02
      相关资源
      最近更新 更多