Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable not visible

Zulfi KhanAug 24 2016 — edited Aug 25 2016

Hi,

I am adding data in JTable but its not visible. I am not getting error. My 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(  );

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();

}

}

This is a very simple program. Some body please tell me what's the problem with this code.

Zulfi.

This post has been answered by TPD-Opitz on Aug 24 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 22 2016
Added on Aug 24 2016
2 comments
3,515 views