Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java JTable not Visible

  1. Aug 24, 2016 #1
    Hi,
    I am trying to add data in a table. I am not getting any error but table is not vi
    Code (Java):
    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();
    }
    }
     

    Some body please guide me why i cant see the table data?

    Zulfi.
     
  2. jcsd
  3. Aug 24, 2016 #2

    jedishrfu

    Staff: Mentor

    Last edited by a moderator: May 8, 2017
  4. Aug 25, 2016 #3
    Hi,


    This problem has been solved. I got its solution from other forum.
    I cant understand what you mean by layout of the table.:
    <Off the bat, I don't see where you've defined a layout for the table.>
    if you mean the dimension of the table then you are right. They suggested me to use another constructor.

    Code (Java):
    Complete 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(  ); ***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();
    }
    }
    Thanks for your response.
    Zulfi.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: JTable not Visible
Loading...