JTable 表头绘制问题
用如下的方法绘制了Jtable表头的第一列为JCheckBox,绘制完后,第一列与第二列之间的竖线没有了,不知道是什么原因,请各位大侠帮忙看一下,谢谢~package com.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import tets.table.CheckHeaderCellRenderer;
import tets.table.CheckTableModle;
public class QQ extends JFrame{
private static final long serialVersionUID = 1L;
//table
private JTable deleteTable;
private String IMEI = "imei";
private String FILE_PATH = "path";
private String SELECT = "select";
private final Object[] deleteColumeNames = {SELECT ,"name", "storage", "size", "date",IMEI, FILE_PATH};
final TableHeadCellRenderer deleteCheckHeadRenderer = new TableHeadCellRenderer();
ExtendedTableModel deleteModel = new ExtendedTableModel(deleteColumeNames, null);
final TableHeadCellRenderer copyMoveCheckHeadRenderer = new TableHeadCellRenderer();
ExtendedTableModel copyMoveModel = new ExtendedTableModel(deleteColumeNames, null);
// color
Color headColor = new Color(0, 0, 0);
Color contentColor = new Color(86, 86, 92);
Color tableLineColor = new Color(205, 205, 205);
Color coupuleColor = new Color(224, 224, 224);
private DefaultTableCellRenderer tableCellRender = new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column) {
if (row % 2 == 0) {
setBackground(Color.WHITE);
} else if (row % 2 == 1) {
setBackground(coupuleColor);
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
};
private JScrollPane createDeleteTablePane(){
deleteTable = new JTable();
deleteTable.setModel(deleteModel);
deleteTable.getTableHeader().setForeground(headColor);
deleteTable.getTableHeader().setReorderingAllowed(false);
deleteTable.getTableHeader().setPreferredSize(new Dimension(new Dimension(deleteTable.getTableHeader().getPreferredSize().width,45)));
//set table
deleteTable.setRowSelectionAllowed(true);
deleteTable.setRowHeight(24);
// deleteTable.setFont(contentFont);
deleteTable.setGridColor(tableLineColor);
deleteTable.setForeground(contentColor);
// tableCellRender.setHorizontalAlignment(JLabel.CENTER);
deleteTable.setDefaultRenderer(Object.class, tableCellRender);
deleteTable.setSelectionForeground(headColor);
// setTableColumns(deleteTable, columeWidth);
deleteTable.setShowHorizontalLines(true);
deleteTable.setShowVerticalLines(false);
deleteTable.getColumn(IMEI).setPreferredWidth(0);
deleteTable.getColumn(IMEI).setMaxWidth(0);
deleteTable.getColumn(IMEI).setWidth(0);
deleteTable.getColumn(IMEI).setMinWidth(0);
deleteTable.getColumn(IMEI).setResizable(false);
deleteTable.getColumn(FILE_PATH).setPreferredWidth(0);
deleteTable.getColumn(FILE_PATH).setMaxWidth(0);
deleteTable.getColumn(FILE_PATH).setWidth(0);
deleteTable.getColumn(FILE_PATH).setMinWidth(0);
deleteTable.getColumn(FILE_PATH).setResizable(false);
deleteTable.getTableHeader().getColumnModel().getColumn(5).setMaxWidth(0);
deleteTable.getTableHeader().getColumnModel().getColumn(5).setMinWidth(0);
deleteTable.getTableHeader().getColumnModel().getColumn(6).setMaxWidth(0);
deleteTable.getTableHeader().getColumnModel().getColumn(6).setMinWidth(0);
deleteTable.getColumn(SELECT).setHeaderRenderer(deleteCheckHeadRenderer);
deleteTable.getColumn(SELECT).setCellRenderer(new CheckBoxTableCellRenderer());
deleteTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
deleteTable.setSelectionBackground(deleteTable.getBackground());
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(deleteTable);
return scrollPane;
}
class CheckBoxTableCellRenderer extends JCheckBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
// private Color selectBackgroundColor;
public CheckBoxTableCellRenderer(){
setSelected(false);
setHorizontalAlignment(SwingConstants.CENTER);
// this.setBorder(new EmptyBorder(0,0,0,0));
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,int column) {
Boolean b = (Boolean) value;
if (row % 2 == 0) {
setBackground(Color.WHITE);
} else if (row % 2 == 1) {
setBackground(coupuleColor);
}
this.setSelected(b.booleanValue());
return this;
}
}
class TableHeadCellRenderer extends JCheckBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public TableHeadCellRenderer() {
setSelected(false);
setBackground(new Color(238,238,238));
this.setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
return this;
}
}
class ExtendedTableModel extends DefaultTableModel {
private static final long serialVersionUID = 1L;
public ExtendedTableModel(Object[] columnTitle, Object[][] cells) {
super(cells, columnTitle);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == 0) {
return true;
} else {
return false;
}
}
}
public QQ(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(createDeleteTablePane(),BorderLayout.CENTER);
this.getContentPane().add(panel);
this.setUndecorated(false);
setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new QQ();
}
}
Jtable JCheckBox --------------------编程问答-------------------- give you a sample:
--------------------编程问答--------------------
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class TestFrame extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("CheckBox Table");
this.setPreferredSize(new Dimension(400, 300));
// setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentPane.add(scrollPane, BorderLayout.CENTER);
initTable();
pack();
}
private void initTable(){
Vector headerNames=new Vector();
headerNames.add("列选择");
headerNames.add("姓名");
headerNames.add("年龄");
Vector data=this.getData();
CheckTableModle tableModel=new CheckTableModle(data,headerNames);
table.setModel(tableModel);
table.getTableHeader().setDefaultRenderer(new CheckHeaderCellRenderer(table));
}
/**
* 获得数据
* @return
*/
private Vector getData(){
Vector data = new Vector();
Vector rowVector1=new Vector();
rowVector1.add(false);
rowVector1.add("Benson");
rowVector1.add("25");
Vector rowVector2=new Vector();
rowVector2.add(false);
rowVector2.add("Laura");
rowVector2.add("26");
Vector rowVector3=new Vector();
rowVector3.add(false);
rowVector3.add("YOYO");
rowVector3.add("1");
data.add(rowVector1);
data.add(rowVector2);
data.add(rowVector3);
return data;
}
}
class CheckTableModle extends DefaultTableModel {
public CheckTableModle(Vector data, Vector columnNames) {
super(data, columnNames);
}
// /**
// * 根据类型返回显示空间
// * 布尔类型返回显示checkbox
// */
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void selectAllOrNull(boolean value) {
for (int i = 0; i < getRowCount(); i++) {
this.setValueAt(value, i, 0);
}
}
}
class CheckHeaderCellRenderer implements TableCellRenderer {
CheckTableModle tableModel;
JTableHeader tableHeader;
final JCheckBox selectBox;
public CheckHeaderCellRenderer(JTable table) {
this.tableModel = (CheckTableModle)table.getModel();
this.tableHeader = table.getTableHeader();
selectBox = new JCheckBox(tableModel.getColumnName(0));
selectBox.setSelected(false);
tableHeader.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
//获得选中列
int selectColumn = tableHeader.columnAtPoint(e.getPoint());
if (selectColumn == 0) {
boolean value = !selectBox.isSelected();
selectBox.setSelected(value);
tableModel.selectAllOrNull(value);
tableHeader.repaint();
}
}
}
});
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TODO Auto-generated method stub
String valueStr = (String) value;
JLabel label = new JLabel(valueStr);
label.setHorizontalAlignment(SwingConstants.CENTER); // 表头标签剧中
selectBox.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
selectBox.setBorderPainted(true);
JComponent component = (column == 0) ? selectBox : label;
component.setForeground(tableHeader.getForeground());
component.setBackground(tableHeader.getBackground());
component.setFont(tableHeader.getFont());
component.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return component;
}
}
这个例子我也用了,同样的代码在mac上面第一列与第二列之间的竖线还是没有划出来
补充:Java , Java EE