jtable 列问题
需求:准备写一个jtable然后更改她的样式后续所有的table都继承这个jtable;问题:展现出来的table无column列名称;
代码展现:
package gov.service.product.gfmis.ResultDialogModel;
import gov.
import gov.
import gov.service.product.gfmis.balance.ui.BalanceMainUI;
import gov.service.product.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class Test extends JTable{
private static GeneralDAO dao = ComponentManager.generalDAO;
public Test()
{
JScrollPane jsp=new JScrollPane();
jsp.setViewportView(this);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setRowHeight(20);
table.setFont(new Font("微软儒雅",Font.PLAIN,14));
int columnCount=table.getColumnModel().getColumnCount();
System.out.println(columnCount);
for(int i=0;i<columnCount;i++)
{
int width=table.getColumnModel().getColumn(i).getWidth();
table.getColumnModel().getColumn(i).setPreferredWidth(width+20);
}
JTableHeader head=new JTableHeader();
head.setFont(new Font("微软雅黑",Font.PLAIN,16));
head.setPreferredSize(new Dimension(head.getWidth(),25));
getModel();
}
protected void getModel() {
ThreadGroup group = Thread.currentThread().getThreadGroup();// 获得当前线程所在线程组
Thread[] threads = new Thread[group.activeCount()];// 使用数组保存活动状态的线程
group.enumerate(threads);// 获得所有线程
DefaultTableModel model = (DefaultTableModel) this.getModel(); // 获得表格模型
model.setRowCount(0); // 清空表格模型中的数据
model.setColumnIdentifiers(new Object[] { "线程ID", "线程名称", "优先级" }); // 定义表头
for (Thread thread : threads) {// 增加行数据
model.addRow(new Object[] { thread.getId(), thread.getName(), thread.getPriority() });
}
this.setModel(model);// 更新表格模型
}
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 800, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content=new JPanel();
content.add(new Test());
frame.setContentPane(content);
frame.setVisible(true);
frame.setResizable(true);
}
});
}
}
展现结果:见附件