Troubleshooting JTable Not Visible - Zulfi

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on troubleshooting the visibility of a JTable in a Java Swing application. The original code failed to display the table data due to improper layout management. The solution involved using a JScrollPane to wrap the JTable and initializing the DefaultTableModel with column names. The corrected code successfully displays the table with data when executed.

PREREQUISITES
  • Java Swing framework knowledge
  • Understanding of JTable and DefaultTableModel
  • Familiarity with JScrollPane usage
  • Basic Java programming skills
NEXT STEPS
  • Review Java Swing layout managers for better UI design
  • Explore advanced JTable features and customization options
  • Learn about event handling in Java Swing applications
  • Study best practices for using JScrollPane with JTable
USEFUL FOR

Java developers, particularly those working with GUI applications, and anyone looking to enhance their understanding of JTable and layout management in Swing.

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.
 

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