2

我的单元格渲染器左对齐,但如何左对齐单元格编辑器?当我编辑其中一个单元格时会发生这种情况

样本

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TextPaneTest extends JFrame {

    public static void main(String args[]) {
        new TextPaneTest();
    }

    public TextPaneTest() {

        // Main panel
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JTable table = new JTable() { 
            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                if (column == 1)
                {
                    ////////////////////////
                    // What might go here //
                    ////////////////////////
                    return getDefaultEditor(Boolean.class);
                }
                return super.getCellEditor(row, column);
            }

            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                if (column == 1)
                {
                    TableCellRenderer renderer = getDefaultRenderer(Boolean.class);
                    ((JCheckBox)renderer).setHorizontalAlignment(SwingConstants.LEFT);
                    return renderer;
                }
                return super.getCellRenderer(row, column);
            }
        };

        String[] columnNames = {"Name", "Value"};
        Object[][] data = {{"one", true}, {"two", true}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);     
        table.setModel(model);

        panel.add(table);
        // Add everything to the frame
        this.add(panel);
        this.setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}
4

1 回答 1

2

作为改变这种情况的第一步:

if (column == 1)

经过

if(getValueAt(row, column) instanceof Boolean)

将使您的方法更加灵活(考虑带有布尔值的 5 列,您不想检查每个列号以返回默认渲染器/编辑器)。

我还建议您制作自己的单元格渲染器,JCheckBox并将其设置为Boolean类的默认单元格渲染器。它将避免您在getCellRenderer()方法内部进行对齐更改。

注意默认情况下getDefaultRenderer(Boolean.class)返回 aJCheckBox但是当逻辑说它也应该getDefaultEditor(Boolean.class)返回 a时返回 a 。因此,在某些时候,投射默认渲染器可能会成为问题。javax.swing.JTable.BooleanEditorJCheckBox

我让它这样工作:

JTable table = new JTable(dtm){
    @Override
    public TableCellEditor getCellEditor(int row, int column) {
        if(getValueAt(row, column) instanceof Boolean){
            return super.getDefaultEditor(Boolean.class);
        }
        return super.getCellEditor(row, column);
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        if(getValueAt(row, column) instanceof Boolean){
            JCheckBox cb = (JCheckBox) getDefaultRenderer(Boolean.class);
            cb.setHorizontalAlignment(SwingConstants.LEFT);
            return getDefaultRenderer(Boolean.class);
        }

        return super.getCellRenderer(row, column);
    }
};

JCheckBox cb = new JCheckBox();
cb.setHorizontalAlignment(SwingConstants.LEFT);
TableCellEditor tce = new DefaultCellEditor(cb);        
table.setDefaultEditor(Boolean.class, tce);

如您所见,键设置为DefaultCellEditorJCheckBox作为类的默认单元格编辑器开始Boolean

截屏

在此处输入图像描述

SSCCE

这是一个完整的SSCCE来测试它:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class Demo {

    private void initGUI(){

        DefaultTableModel dtm = new DefaultTableModel(new Object[]{"Property", "Value"}, 0);
        dtm.addRow(new Object[]{"Property 1", true});
        dtm.addRow(new Object[]{"Property 2", false});

        JTable table = new JTable(dtm){
            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                if(getValueAt(row, column) instanceof Boolean){
                    return super.getDefaultEditor(Boolean.class);
                }
                return super.getCellEditor(row, column);
            }

            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                if(getValueAt(row, column) instanceof Boolean){
                    JCheckBox cb = (JCheckBox) getDefaultRenderer(Boolean.class);
                    cb.setHorizontalAlignment(SwingConstants.LEFT);
                    return getDefaultRenderer(Boolean.class);
                }

                return super.getCellRenderer(row, column);
            }
        };

        JCheckBox cb = new JCheckBox();
        cb.setHorizontalAlignment(SwingConstants.LEFT);
        TableCellEditor tce = new DefaultCellEditor(cb);        
        table.setDefaultEditor(Boolean.class, tce);

        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(300,200));

        JPanel content = new JPanel(new BorderLayout());
        content.add(scrollPane);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);        
    }


    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }

}
于 2013-10-17T21:23:03.170 回答