0

Hey I don't have enough knowledge on JTables, How to insert data in JTables by the way I entered the data in my table but I don't know why the column names not appear in my table.

Here's my code:

public class tab {
    public tab() {
        initComponents();

    }

    public void initComponents() {
        JFrame fr = new JFrame("Score Card");

        JScrollPane pane = new JScrollPane();
        fr.setSize(500, 350);
        JTable scoreTab = new JTable(5, 4);
        DefaultTableModel model = new DefaultTableModel(1, 4);
        // model.setColumnIdentifiers(new Object[]{"No","Name","Score","Date"});

        scoreTab.setModel(new javax.swing.table.DefaultTableModel(

        new Object[][] { { 1, 2, 2 },

        }, new String[] { "Name", "Score", "Date" }));
        pane.setViewportView(scoreTab);
        fr.add(scoreTab);
        fr.setLocationRelativeTo(null);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
    }

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

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Umer Ali
  • 89
  • 8

1 Answers1

2

add JScrollPane to the jframe

fr.add(pane);

you are directly adding jtable to the frame fr.add(scoreTab);

if you add a table without jscrollpane you have to add headers separately.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • will u please tell me how to dynamically input the data in table,Actually i want to read data from text file and then put it intothe table plz give some hint it means alot to me – Umer Ali Sep 21 '15 at 18:13
  • @UmerAli you can read file then you can create model after you finish you can apply model to the table. another way is to create table then read file and add row by row.using `insertRow(Vector v);` method.for example see this question http://stackoverflow.com/questions/22371720/how-to-add-row-dynamically-in-jtable – Madhawa Priyashantha Sep 21 '15 at 18:18
  • see this question .http://stackoverflow.com/questions/22371720/how-to-add-row-dynamically-in-jtable. – Madhawa Priyashantha Sep 21 '15 at 18:20