JTableHeader添加控件

  • A+
所属分类:Java

JTableHeader添加控件 - JackTanbb的博客 - CSDN博客

JTableHeader添加控件

话不多说直接上图
1.
package JTableTest;
2.

3.
import java.awt.BorderLayout;
4.
import java.awt.Color;
5.
import java.awt.Component;
6.
import java.awt.Dimension;
7.
import java.awt.FlowLayout;
8.
import java.awt.event.ActionEvent;
9.
import java.awt.event.ActionListener;
10.
import java.awt.event.MouseAdapter;
11.
import java.awt.event.MouseEvent;
12.

13.
import javax.swing.AbstractCellEditor;
14.
import javax.swing.BorderFactory;
15.
import javax.swing.ImageIcon;
16.
import javax.swing.JButton;
17.
import javax.swing.JCheckBox;
18.
import javax.swing.JFrame;
19.
import javax.swing.JLabel;
20.
import javax.swing.JOptionPane;
21.
import javax.swing.JPanel;
22.
import javax.swing.JScrollPane;
23.
import javax.swing.JTable;
24.
import javax.swing.SwingConstants;
25.
import javax.swing.UIManager;
26.
import javax.swing.table.JTableHeader;
27.
import javax.swing.table.TableCellEditor;
28.
import javax.swing.table.TableCellRenderer;
29.
import javax.swing.table.TableColumn;
30.

31.
public class TableHeaderRenderer extends JFrame implements ActionListener {
32.

33.
Object[][] rows = {
34.

35.
};
36.

37.
Object[] cols = {"phone","menoy"};
38.

39.
JTable tab;
40.

41.
public TableHeaderRenderer() {
42.
tab = new JTable(rows, cols);
43.
TableColumn column = tab.getTableHeader().getColumnModel().getColumn(1);
44.
TableColumn col = tab.getColumnModel().getColumn(1);
45.
col.setPreferredWidth(200);
46.

47.
TableCellRenderer ren = new RendererDeconrator();
48.
tab.getTableHeader().addMouseListener(new MouseAdapter() {
49.

50.
@Override
51.
public void mouseClicked(MouseEvent e) {
52.
JTableHeader tableHeader = (JTableHeader) e.getSource();
53.
//根据鼠标点选获取当前是第一列
54.
int selColumn = tableHeader.getColumnModel().getColumnIndexAtX(e.getPoint().x);
55.

TableCellRenderer cellRenderer = tableHeader.getColumnModel().getColumn(selColumn).getHeaderRenderer();

56.
if ( cellRenderer instanceof RendererDeconrator ) {
57.
RendererDeconrator deconrator= (RendererDeconrator) cellRenderer;
58.
System.out.println(tableHeader.getColumnModel().getColumn(1).getHeaderValue());
59.
JOptionPane.showMessageDialog(null, "添加表格!");
60.
}
61.
}
62.
});
63.
column.setHeaderRenderer(ren);
64.
//column.setCellEditor(cellEditor);
65.
getContentPane().add(new JScrollPane(tab),BorderLayout.CENTER);
66.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
67.
setLocationRelativeTo(null);
68.
setVisible(true);
69.
pack();
70.
}
71.

72.
public static void main(String[] args) {
73.
new TableHeaderRenderer();
74.
}
75.

76.
@Override
77.
public void actionPerformed(ActionEvent e) {
78.

79.
}
80.
}
81.

82.
class RendererDeconrator implements TableCellRenderer{
83.
public JPanel panel;
84.
public JButton btn = new JButton("添加");
85.
JLabel iconLabel = new JLabel(new ImageIcon("D:/toggle.gif"));
86.

87.
public RendererDeconrator() {
88.
panel = new JPanel();
89.
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
90.
panel.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
91.
panel.add(iconLabel);
92.
panel.add(btn);
93.
}
94.

95.
@Override
96.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,

97.
int row, int column) {
98.
if ( value != null ) {
99.
iconLabel.setText(value.toString());
100.
}
101.
return panel;
102.
}
103.
}
104.

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin