Java Troubleshooting JTable Not Visible - Zulfi

  • Thread starter Thread starter zak100
  • Start date Start date
Click For Summary
Zulfi encountered an issue with a JTable not displaying data despite no errors being reported. The initial code lacked a proper layout for the table, which was identified as a potential problem. A solution was found by using a different constructor for the DefaultTableModel, allowing the table to be populated correctly. The final code included the JScrollPane for the table, ensuring it was added to the panel properly. The problem was resolved, and Zulfi expressed gratitude for the assistance received.
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.
 
Technology 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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
13K