Showing JTable Header Without Using JScrollPane java中的JTab

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);}}

运行之后显示的效果

而只有在充满了艰辛的人生旅途中,始终调整好自己观风景的心态,

Showing JTable Header Without Using JScrollPane java中的JTab

相关文章:

你感兴趣的文章:

标签云: