Troubleshooting JTable Not Visible - Zulfi

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 5K views
zak100
Messages
462
Reaction score
11
Hi,
I am trying to add data in a table. I am not getting any error but table is not vi
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 can't see the table data?

Zulfi.
 
Physics news on Phys.org
Hi,This problem has been solved. I got its solution from other forum.
I can't 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.

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.