2

我需要隐藏 JTable 的标题栏吗?所以我只需要没有任何实际名称的行和列来显示给用户吗?有没有其他秋千可以做类似的事情?我无法添加

jTable1.setTableHeader(null);

initComponents 函数内部的任何地方,我尝试过但出现错误?

import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class table extends javax.swing.JFrame {

    public table() {
        initComponents();
        jTable1.setTableHeader(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Name", "Age"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("jButton2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(jButton1)
                .addGap(51, 51, 51)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(0, 30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    class employee{
        private int age;
        private String name;
        public  employee(int a, String n){ this.age = a; this.name = n;}
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         




        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object[]{"behzad",14});

    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new table().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   
}
4

8 回答 8

5

也许你可以使用:

initComponents();
//jTable1.setTableHeader(null);
jScrollPane1.getColumnHeader().setVisible(false);
于 2013-08-30T10:31:47.083 回答
3

您可以使用

table.setTableHeader(null);
于 2013-08-30T09:45:44.400 回答
0

您只需要将方法 setTableHeader(null) 设置为 null。

于 2013-08-30T09:54:01.673 回答
0

利用table.setTableHeader(null);

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class NullColumnHeader {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
        { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "1", "2", "3" };
    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    table.setTableHeader(null);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

  }
}
于 2013-08-30T09:49:24.423 回答
0

您可以尝试将 tableHeader 设置为 null(我没有尝试过): http ://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setTableHeader%28javax.swing.table .JTableHeader%29

于 2013-08-30T09:52:15.397 回答
0

您可以使用 setTableHeader() 方法将表头设置为空。

table.setTableHeader(null);
于 2013-08-30T10:05:49.927 回答
0

很多人建议使用 .setTableHeader (null) 但这不起作用,并且当 JDialog 可见时会导致空指针异常。

引用 JavaDocs .. “拥有一个空 tableHeader 是合法的。”

https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#setTableHeader-javax.swing.table.JTableHeader-

我认为没有一种简单的方法可以删除标题,或者至少我找不到。

于 2019-01-11T20:36:15.420 回答
0

您可以扩展 JTable 类并将其标头设置为null(在构造函数的末尾)。

public class TableWithoutHeader extends JTable{
    public TableWithoutHeader(){
        //Code to construct your table
        setTableHeader(null);
    }
}

现在您可以插入一个TableWithoutHeader,就好像它是您的调色板的一部分一样。

于 2017-06-27T21:34:17.753 回答