删除JTable

时间:2016-08-12 07:01:24

标签: java swing jcombobox tablecelleditor nimbus

我已移除JComoboBox中的箭头按钮,使其看起来像JTextField,并将其添加为细胞投标人。目的是创建一个AutoSuggest(非自动完成)JTable单元。

On That that the border kinda看起来很烦人。如何更改边框使其看起来像右边的文本框边框。我试过删除边框创建的线条边框。但它并没有消除蓝色边界。

使用Nimbus UI。

enter image description here

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() {
                }
            });
        }

    }
}

1 个答案:

答案 0 :(得分:2)

  

使用Nimbus UI

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

  • // Disable Policies let policies: [String: ServerTrustPolicy] = [ "https://example.entrydns.org:1012/": .DisableEvaluation, "https://52.70.13.2:1010/": .DisableEvaluation ] let manager = Manager( configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies) ) // --------------- let networkLogger = NetworkLoggerPlugin(verbose: true, responseDataFormatter: nil) let endpointClosure = { (target: ExampleAPI) -> Endpoint<ExampleAPI> in let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString let endpoint: Endpoint<ExampleAPI> = Endpoint<ExampleAPI> (URL: url, sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters, parameterEncoding: .URLEncodedInURL) return endpoint.endpointByAddingHTTPHeaderFields(target.header()) } let ExampleProvider = MoyaProvider<ExampleAPI>(manager: manager, plugins:[networkLogger], endpointClosure: endpointClosure)
  • TextField[Focused].borderPainter
  • TextField[Enabled].borderPainter
  • ComboBox:\"ComboBox.textField\"[Enabled].backgroundPainter
ComboBox:\"ComboBox.textField\"[Selected].backgroundPainter