JTableHeader添加控件

话不多说直接上图

package JTableTest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class TableHeaderRenderer extends JFrame implements ActionListener {

	Object[][] rows = {

	};
	
	Object[] cols = {"phone","menoy"};
		
	JTable tab;
	
	public TableHeaderRenderer() {
		tab = new JTable(rows, cols);
		TableColumn column = tab.getTableHeader().getColumnModel().getColumn(1);
		TableColumn col = tab.getColumnModel().getColumn(1);
		col.setPreferredWidth(200);
		
		TableCellRenderer ren = new RendererDeconrator();
		tab.getTableHeader().addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				JTableHeader tableHeader = (JTableHeader) e.getSource();
				//根据鼠标点选获取当前是第一列
				int selColumn = tableHeader.getColumnModel().getColumnIndexAtX(e.getPoint().x);
				TableCellRenderer cellRenderer = tableHeader.getColumnModel().getColumn(selColumn).getHeaderRenderer();
				if ( cellRenderer instanceof RendererDeconrator ) {					
					RendererDeconrator deconrator= (RendererDeconrator) cellRenderer;
					System.out.println(tableHeader.getColumnModel().getColumn(1).getHeaderValue());
					JOptionPane.showMessageDialog(null, "添加表格!");
				}
			}
		});
		column.setHeaderRenderer(ren);
		//column.setCellEditor(cellEditor);
		getContentPane().add(new JScrollPane(tab),BorderLayout.CENTER);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
		pack();
	}
	
	public static void main(String[] args) {
		new TableHeaderRenderer();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
	}	
}

class RendererDeconrator implements TableCellRenderer{
	public JPanel panel;
	public JButton btn = new JButton("添加");
	JLabel iconLabel = new JLabel(new ImageIcon("D:/toggle.gif"));
	
	public RendererDeconrator() {
		panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
		panel.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
		panel.add(iconLabel);
		panel.add(btn);
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
			int row, int column) {
		if ( value != null ) {			
			iconLabel.setText(value.toString());
		}
		return panel;
	}
}

 

你可能感兴趣的:(Swing)