2

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();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shaji SS
  • 102
  • 1
  • 2
  • 10

4 Answers4

4
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

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
4

to @Sergiy Medvynskyy and @Krishna about 1. f.setVisible(true); 2. Initial Thread 3. p1.setVisible(true);

e.g.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Table /*extends JFrame implements ActionListener*/ {

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

    public Table() {
        p1 = new JPanel();
        p1.setBackground(Color.red);
        p1.setLayout(new BorderLayout(5, 5));        
        String[] headers = {"First Name", "Last Name", "Age"};
        Object[][] data = {
            {"Kathy", "Smith", new Integer(25)},};        
        jTable1 = new JTable(data, headers);
        JScrollPane scrollPane = new JScrollPane(jTable1);
        jTable1.setPreferredScrollableViewportSize(jTable1.getPreferredSize());
        p1.add(scrollPane);        
        f = new JFrame("Home123");
        //f.setSize(getMaximumSize());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p1);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Table();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You're adding the table directly to the frame's panel, instead of adding the scroll pane wrapping the table, and displaying the headers.

Change

p1.add(jTable1);

to

p1.add(scrollPane);

And definitely stop using a null layout and setting bounds. Learn using layout managers.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

You should add scroll pane to panel

import java.awt.BorderLayout;
import java.awt.Color;

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

public class Table extends JFrame {
    JFrame f;
    JPanel p1;
    JPanel jPanel1;
    JTable jTable1;

    Table() {

        p1=new JPanel();
        p1.setBackground(Color.red);
        p1.setVisible(true);
        p1.setLayout(new BorderLayout());

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

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

        p1.add(scrollPane);

        f=new JFrame("Home123");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p1);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new Table();
    }
}

P.S. Never use null-layout.

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48