Forum Stats

  • 3,769,989 Users
  • 2,253,043 Discussions
  • 7,875,261 Comments

Discussions

JTable not visible

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Aug 25, 2016 3:05AM in New To Java

Hi,

I am adding data in JTable but its not visible. I am not getting error. My code is:

import javax.swing.*;

import java.util.*;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

public class TableEg{

JFrame frame = new JFrame("Table Eg1");

JPanel panel = new JPanel();

JTable table = new JTable();

DefaultTableModel model = new DefaultTableModel(  );

JScrollPane scrollPane = new JScrollPane(table);

TableEg() {

  Vector<String> rowOne = new Vector<String>();

    rowOne.addElement("Row1-Column1");

    rowOne.addElement("Row1-Column2");

    rowOne.addElement("Row1-Column3");

    model.addRow(rowOne);

   

    Vector<String> rowTwo = new Vector<String>();

    rowTwo.addElement("Row2-Column1");

    rowTwo.addElement("Row2-Column2");

    rowTwo.addElement("Row2-Column3");

    model.addRow(rowTwo);

  

    table.setModel(model);

   

 

 

  frame.setSize(500,200);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.add(panel);

  panel.add(table);

  panel.add(scrollPane);

  frame.setVisible(true);

}

public static void main(String args[]){

 

  new TableEg();

}

}

This is a very simple program. Some body please tell me what's the problem with this code.

Zulfi.

Best Answer

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Aug 24, 2016 5:28PM Accepted Answer

    Your code has 2 Probems.

    The first one is a somehow unexpected behavior of DefaultTableModel.

    Frankly it needs to know the column count in advance which is why you need to invoke a different constructor:

    DefaultTableModel model = new DefaultTableModel(new Object[]{"1","2","3"},0  );

    alternatively you could overwrite the getColumnCount() method in DefaultTableModel:

    DefaultTableModel model = new DefaultTableModel(){    @Override    public int getColumnCount() {    return 3;    }    };

    Then your Table gets displayed, but without the ScrollPane, which is because of the second problem in your code.

    The reason for that is that you add the table twice to your Fame:

    first as child of the Scrollpane:

    JScrollPane scrollPane = new JScrollPane(table);

    and later as direct child of the panel:

    panel.add(table);

    The latter wins. So the Frame shows the table without a ScrollPane and the ScrollPane without content. The ScrollPane without Content is too small to be seen.

    If you comment the line

    panel.add(table);

    then the frame shows the table inside the ScrollPane.

    bye

    TPD

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Aug 24, 2016 5:28PM Accepted Answer

    Your code has 2 Probems.

    The first one is a somehow unexpected behavior of DefaultTableModel.

    Frankly it needs to know the column count in advance which is why you need to invoke a different constructor:

    DefaultTableModel model = new DefaultTableModel(new Object[]{"1","2","3"},0  );

    alternatively you could overwrite the getColumnCount() method in DefaultTableModel:

    DefaultTableModel model = new DefaultTableModel(){    @Override    public int getColumnCount() {    return 3;    }    };

    Then your Table gets displayed, but without the ScrollPane, which is because of the second problem in your code.

    The reason for that is that you add the table twice to your Fame:

    first as child of the Scrollpane:

    JScrollPane scrollPane = new JScrollPane(table);

    and later as direct child of the panel:

    panel.add(table);

    The latter wins. So the Frame shows the table without a ScrollPane and the ScrollPane without content. The ScrollPane without Content is too small to be seen.

    If you comment the line

    panel.add(table);

    then the frame shows the table inside the ScrollPane.

    bye

    TPD

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Aug 25, 2016 3:05AM

    Hi,

    Thanks for your response. Its working.

    >

    I have changed the code to:

    import javax.swing.*;

    import java.util.*;

    import javax.swing.JTable;

    import javax.swing.table.DefaultTableModel;

    public class TableEg{

    JFrame frame = new JFrame("Table Eg1");

    JPanel panel = new JPanel();

    JTable table = new JTable();

    //DefaultTableModel model = new DefaultTableModel(  ); ***Doesnt work

    DefaultTableModel model = new DefaultTableModel(new Object[]{"1","2","3"},0  );

    JScrollPane scrollPane = new JScrollPane(table);

    TableEg() {

      Vector<String> rowOne = new Vector<String>();

        rowOne.addElement("Row1-Column1");

        rowOne.addElement("Row1-Column2");

        rowOne.addElement("Row1-Column3");

        model.addRow(rowOne);

        Vector<String> rowTwo = new Vector<String>();

        rowTwo.addElement("Row2-Column1");

        rowTwo.addElement("Row2-Column2");

        rowTwo.addElement("Row2-Column3");

        model.addRow(rowTwo);

        table.setModel(model);

      frame.setSize(500,200);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.add(panel);

      //panel.add(table);

      panel.add(scrollPane);

      frame.setVisible(true);

    }

    public static void main(String args[]){

      new TableEg();

    }

    }

    Zulfi.

This discussion has been closed.