gpt4 book ai didi

java - 可编辑的 JTableHeader

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:16 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Accessing a JTextField in JTableHeader

如何使 JTableHeader 可编辑。 F.e.我将 ColumnHeader 设为 JTextField。我必须做什么才能使 JTextField 可编辑。看例子:

import java.awt.*;
import javax.swing.*;

import javax.swing.table.*;

import java.awt.event.*;

public class Test extends JFrame {

String[][] cellValues = { { "v00", "v01", "v02" }, { "v10", "v11", "v12" },
{ "v20", "v21", "v22" }, { "v30", "v31", "v32" },
{ "v40", "v41", "v42" }, { "v50", "v51", "v52" } };
String[] columnNames = { "v00", "v01", "v02" };

javax.swing.JTable jTable1 = new javax.swing.JTable(cellValues, columnNames);

public Test() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().add(new JScrollPane(jTable1), null);
for (int i = 0; i < 3; i++)
jTable1.getColumnModel().getColumn(i)
.setHeaderRenderer(new Renderer());
}

public static void main(String[] args) {
Test frame = new Test();
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}

}

class Renderer extends JPanel implements TableCellRenderer {

JLabel label = new JLabel(" ");
JTextField field = new JTextField("%");

public Renderer() {
super(new BorderLayout());
add(label, BorderLayout.NORTH);
add(field, BorderLayout.CENTER);
setBorder(BorderFactory.createEtchedBorder());
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
label.setText(value.toString());
return (JComponent) this;
}

public String toString() {
return label.toString();
}

public void setText(String text) {
label.setText(text);
}

}

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