gpt4 book ai didi

java - 更改 TableCellEditor 对象的对齐方式

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:33 26 4
gpt4 key购买 nike

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

sample

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

最佳答案

作为改变这个条件的第一步:

if (column == 1)

if(getValueAt(row, column) instanceof Boolean)

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

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

注意默认情况下getDefaultRenderer(Boolean.class) 返回一个JCheckBoxgetDefaultEditor(Boolean.class) 返回一个javax。 swing.JTable.BooleanEditor 当逻辑说它也应该返回一个 JCheckBox 时。因此,在某些时候,转换默认渲染器可能会成为一个问题。

我是这样实现的:

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 类的默认单元格编辑器。

截图

enter image description here

中南合作商会

这是一个完整的 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();
}
});
}

}

关于java - 更改 TableCellEditor 对象的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19436163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com