设置表单元格内容对齐方式在开发中也会经常遇到。
需要继承DefaultCellEditor类,并重写:
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)实现 TableCellEditor 接口。
指定者:
接口 TableCellEditor 中的 getTableCellEditorComponent
参数:
table – 要求编辑器进行编辑的 JTable;可以为 null
value – 要编辑的单元格的值;由具体的编辑器解释和绘制该值。例如,如果 value 是字符串 “true”,则它可呈现为字符串,或者也可作为已选中的复选框来呈现。null 是有效值
isSelected – 如果使用突出显示来呈现该单元格,则为 true
row – 要编辑的单元格所在行
column – 要编辑的单元格所在列
返回:
要编辑的组件
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Font; import javax.swing.DefaultCellEditor; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableModel; public class DefaultCellEditorJTextFieldAlignment extends JFrame { DefaultTableModel model = new DefaultTableModel(new Object[][] { { "some", "text" }, { "any", "text" }, { "even", "more" }, { "text", "strings" }, { "and", "other" }, { "text", "values" } }, new Object[] { "Column 1", "Column 2" }); public DefaultCellEditorJTextFieldAlignment() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(model); //设置默认编辑对象 table.setDefaultEditor(Object.class, new MyEditor()); getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); pack(); } public static void main(String arg[]) { new DefaultCellEditorJTextFieldAlignment().setVisible(true); } } class MyEditor extends DefaultCellEditor { public MyEditor() { super(new JTextField()); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //获得默认表格单元格控件 JTextField editor = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); if (value != null) editor.setText(value.toString()); if (column == 0) { //设置对齐方式 editor.setHorizontalAlignment(SwingConstants.CENTER); editor.setFont(new Font("Serif", Font.BOLD, 14)); } else { editor.setHorizontalAlignment(SwingConstants.RIGHT); editor.setFont(new Font("Serif", Font.ITALIC, 12)); } return editor; } }
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
, 设置表单元格内容对齐方式在开发中也会经常遇到。
需要继承DefaultCellEditor类,并重写:
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)实现 TableCellEditor 接口。
指定者:
接口 TableCellEditor 中的 getTableCellEditorComponent
参数:
table – 要求编辑器进行编辑的 JTable;可以为 null
value – 要编辑的单元格的值;由具体的编辑器解释和绘制该值。例如,如果 value 是字符串 “true”,则它可呈现为字符串,或者也可作为已选中的复选框来呈现。null 是有效值
isSelected – 如果使用突出显示来呈现该单元格,则为 true
row – 要编辑的单元格所在行
column – 要编辑的单元格所在列
返回:
要编辑的组件
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Font; import javax.swing.DefaultCellEditor; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableModel; public class DefaultCellEditorJTextFieldAlignment extends JFrame { DefaultTableModel model = new DefaultTableModel(new Object[][] { { "some", "text" }, { "any", "text" }, { "even", "more" }, { "text", "strings" }, { "and", "other" }, { "text", "values" } }, new Object[] { "Column 1", "Column 2" }); public DefaultCellEditorJTextFieldAlignment() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(model); //设置默认编辑对象 table.setDefaultEditor(Object.class, new MyEditor()); getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); pack(); } public static void main(String arg[]) { new DefaultCellEditorJTextFieldAlignment().setVisible(true); } } class MyEditor extends DefaultCellEditor { public MyEditor() { super(new JTextField()); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //获得默认表格单元格控件 JTextField editor = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); if (value != null) editor.setText(value.toString()); if (column == 0) { //设置对齐方式 editor.setHorizontalAlignment(SwingConstants.CENTER); editor.setFont(new Font("Serif", Font.BOLD, 14)); } else { editor.setHorizontalAlignment(SwingConstants.RIGHT); editor.setFont(new Font("Serif", Font.ITALIC, 12)); } return editor; } }