Troubleshooting JTable Not Visible - Zulfi

  • Java
  • Thread starter zak100
  • Start date
In summary, the person is trying to add data in a table, but they are not getting any error but the table is not visible.
  • #1
zak100
462
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
  • #3
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.
 

Related to Troubleshooting JTable Not Visible - Zulfi

What could be causing my JTable to not be visible?

There are a few potential reasons why your JTable may not be visible. One possibility is that the JTable is not added to a visible container or panel. Another possibility is that the JTable's size is set to zero, making it invisible. Finally, the data within the JTable may be empty or not properly formatted, causing it to not display.

How can I troubleshoot a JTable that is not visible?

To troubleshoot a JTable that is not visible, you can start by checking that the JTable is added to a visible container and that its size is not set to zero. You can also try printing out the data within the JTable to make sure it is properly formatted. Additionally, you can use a debugger to step through your code and see if there are any errors or issues with your JTable's creation or data.

Why is my JTable only partially visible?

If your JTable is only partially visible, it could be due to its size being too small to display all of its data. You can try increasing the size of the JTable or its container to see if that resolves the issue. It could also be due to the JTable's data exceeding the size of the container it is in, in which case you may need to use scrolling or pagination to display all of the data.

How can I make my JTable scrollable?

To make your JTable scrollable, you can place it within a JScrollPane. This will allow you to scroll through the table's data if it exceeds the size of the visible area. You can also add horizontal and vertical scroll bars to the JScrollPane to make it easier to navigate through the data.

What can I do if my JTable is not displaying the correct data?

If your JTable is not displaying the correct data, it could be due to an error in your code or improper formatting of the data. You can try printing out the data to see if it is being properly retrieved and formatted. You can also check for any errors in your code that may be causing the incorrect data to be displayed. Additionally, make sure that you are using the correct methods and parameters to set the data in your JTable.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top