关闭

Showing JTable Header Without Using JScrollPane java中的JTable不显示表头的原因

标签: javajava swingswingjava swing JTablejava JTable
1915人阅读 评论(0) 收藏 举报
分类:

Most tutorials and examples about JTable put it within a JScrollPane. If you do not place the JTable inside a JScrollPane, the header is not automatically shown.

//即标题1 标题2标题3标题4标题5再面板上显示出来
package train;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Table1 {

	public static void main(String[] args) {
		new FrmMain();
	}
}

class FrmMain extends JFrame {
	JPanel contentPane;
	DefaultTableModel dtm = new DefaultTableModel();
	JTable tblStudent = new JTable(dtm);
	Vector data = new Vector();
	Vector<String> title = new Vector<String>();

	public FrmMain() {
		try {
			this.setVisible(true);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void jbInit() {
		Vector data1 = new Vector();
		data1.add("标题1的内容");
		data1.add("标题2的内容");
		data1.add("标题3的内容");
		data1.add("标题4的内容");
		data1.add("标题5的内容");
		data.add(data1);
		contentPane = (JPanel) getContentPane();
		setSize(new Dimension(700, 300));
		tblStudent.setBounds(new Rectangle(50, 65, 375, 300));
		contentPane.add(tblStudent);
		showTableData();
	}

	public void showTableData() {
		title.add("标题1");
		title.add("标题2");
		title.add("标题3");
		title.add("标题4");
		title.add("标题5");
		dtm.setDataVector(data, title);
	}

}

以上代码运行的结果:


However, JTable does create it. To show it, simply call JTable.getTableHeader() and add it to the Container. Just as importantly, you can continue to control the columns (such as changing column width and switching columns) from the header.

For example, you can easily put the header NORTH of a BorderLayout:

setLayout(new BorderLayout());
JTable table = new JTable(data, headerLabels);
add(table.getTableHeader(), BorderLayout.NORTH);
add(table, BorderLayout.CENTER);
Note that you could conceivably put the header in the SOUTH position. However, you can only have one header showing at a time. If you add it in both the NORTH and then the SOUTH location, it will only show up in the SOUTH location.
我的例子:

//即标题1 标题2标题3标题4标题5再面板上显示出来
package train;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Table1 {

	public static void main(String[] args) {
		new FrmMain();
	}
}

class FrmMain extends JFrame {
	JPanel contentPane;
	DefaultTableModel dtm = new DefaultTableModel();
	JTable tblStudent = new JTable(dtm);
	Vector data = new Vector();
	Vector<String> title = new Vector<String>();

	public FrmMain() {
		try {
			this.setVisible(true);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void jbInit() {
		Vector data1 = new Vector();
		data1.add("标题1的内容");
		data1.add("标题2的内容");
		data1.add("标题3的内容");
		data1.add("标题4的内容");
		data1.add("标题5的内容");
		data.add(data1);

		title.add("标题1");
		title.add("标题2");
		title.add("标题3");
		title.add("标题4");
		title.add("标题5");
		dtm.setDataVector(data, title);

		contentPane = (JPanel) getContentPane();
		setSize(new Dimension(700, 300));
		tblStudent.setBounds(new Rectangle(50, 65, 375, 300));
		contentPane.add(tblStudent);
		// notice the below codes.
		setLayout(new BorderLayout());
		add(tblStudent.getTableHeader(), BorderLayout.NORTH);
		add(tblStudent, BorderLayout.CENTER);
	}

}

运行之后显示的效果






0
0

猜你在找
深度学习基础与TensorFlow实践
【在线峰会】前端开发重点难点技术剖析与创新实践
【在线峰会】一天掌握物联网全栈开发之道
【在线峰会】如何高质高效的进行Android技术开发
机器学习40天精英计划
Python数据挖掘与分析速成班
微信小程序开发实战
JFinal极速开发企业实战
备战2017软考 系统集成项目管理工程师 学习套餐
Python大型网络爬虫项目开发实战(全套)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:315638次
    • 积分:4451
    • 等级:
    • 排名:第6273名
    • 原创:53篇
    • 转载:444篇
    • 译文:0篇
    • 评论:4条
    统计