Search code examples
javaswingjtablejtableheader

Table Header is not displaying on Java Swing


I add full code Here. Check the following code which I am using to create the table. While executing the program, Values are displaying clearly, but the header is not displaying.

public class Table extends JFrame implements ActionListener {

    JFrame f;
    JPanel p1;
    JPanel jPanel1;
    JTable jTable1;

    Table() {

        f=new JFrame("Home123");
        f.setSize(getMaximumSize());
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);

        p1=new JPanel();
        p1.setBackground(Color.red);
        p1.setVisible(true);
        p1.setLayout(null);
        f.add(p1);

        String[] headers = {"First Name","Last Name","Age"};
        Object[][] data = {
        {"Kathy", "Smith",new Integer(25)},
        };  

         jTable1 =new JTable(data, headers);
         JScrollPane scrollPane = new JScrollPane(jTable1);

         p1.add(jTable1);

         p1.setBounds(200, 100, 500, 500);
         jTable1.setBounds(70,250, 375,80);
    }

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

Solution

  • public class TableExample extends JFrame implements ActionListener {
    
            JFrame f;
            JPanel p1;
            JPanel jPanel1;
            JTable jTable1;
    
            TableExample() {
    
                JFrame f;
            JPanel p1;
            JPanel jPanel1;
            JTable jTable1;
    
            f = new JFrame("Home123");
            f.setSize(getMaximumSize());
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            p1 = new JPanel();
            p1.setBackground(Color.red);
            p1.setLayout(new BorderLayout());
            f.add(p1);
    
            String[] headers = {"First Name", "Last Name", "Age"};
            Object[][] data = {
                {"Kathy", "Smith", new Integer(25)},};
    
            jTable1 = new JTable(data, headers);
            JScrollPane scrollPane = new JScrollPane(jTable1);
            scrollPane.setSize(500,500);
            jTable1.setSize(500,500);
            p1.add(scrollPane);
            f.pack();
            f.setVisible(true);
            }
    
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TableExample();
                }
            });
            }
    
            @Override
                public void actionPerformed(ActionEvent e) {
                try {
                    System.out.println("");
    
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    

    try this its working and showing all thing that u need.enter image description here