2

I need to hide the title bar from JTable? So I need only row and columns without any actual name to show touser? Is there any other swing to do the similar thing? I can not add

jTable1.setTableHeader(null);

anywhere inside initComponents function and I tried but got an error?

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                   
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Bernard
  • 4,240
  • 18
  • 55
  • 88

8 Answers8

5

Maybe you can use:

initComponents();
//jTable1.setTableHeader(null);
jScrollPane1.getColumnHeader().setVisible(false);
aterai
  • 9,658
  • 4
  • 35
  • 44
3

You can use

table.setTableHeader(null);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    you are victim of generated code for Swing GUI, (you forgot to mentioned Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException), but works correctly if JTable is inialized – mKorbel Aug 30 '13 at 10:16
  • One of the reasons why I never use generated code builders - coding by hand allows for more control of code :)) – Reimeus Aug 30 '13 at 10:18
0

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

  }
}
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

You could try and set the tableHeader to null (I haven't tried): http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setTableHeader%28javax.swing.table.JTableHeader%29

Puce
  • 37,247
  • 13
  • 80
  • 152
0

You just need to set to null the method setTableHeader(null).

arvind
  • 9
  • 1
  • 8
0

You can set the table header null using the method setTableHeader().

table.setTableHeader(null);
0

You can extend the JTable class and set its header to null (at the end of constructor).

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

Now you can insert a TableWithoutHeader as if it was part of your Palette.

blu
  • 1
  • 4
0

So many people suggesting .setTableHeader (null) but this does not work and will cause a null pointer exception when the JDialog is made visible.

Quoting JavaDocs .. "It is legal to have a null tableHeader."

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

I think there is not an easy way to remove the title, or at least I couldn't find one.

RichEarle
  • 21
  • 3