我的单元格渲染器左对齐,但如何左对齐单元格编辑器?当我编辑其中一个单元格时会发生这种情况
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);
}
}