Cymbals
Cymbals

Reputation: 1174

JTable not appearing - contains addRow

The code runs without errors, but the JTable does not appear.

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Dimension;

public class CreateTable {

    public static void main(String[] args) { 
        //init table
        DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);
        model.addColumn("col1");
        model.addColumn("col2");
        model.addColumn("col3");
        model.addColumn("col4");  

        for (int i=1;i<=100;i++){
           model.addRow(new Object[]{i,1,2,3});  //output jtable row
        }

        table.setPreferredScrollableViewportSize(new Dimension(500,50));
        table.setFillsViewportHeight(true);
        table.setSize(600, 200);
        table.setVisible(true);  
    }
}

Upvotes: 1

Views: 74

Answers (2)

Alan
Alan

Reputation: 253

For GUI's you need a "Frame" which is basically a window. You then add things to the frame which is how you display components.

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CreateTable {

public static void main(String[] args) { 
    //Create Frame
    JFrame myFrame = new JFrame("Testing");
    //Create panel
    JPanel myPanel = new JPanel();
    //init table
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("col1");
    model.addColumn("col2");
    model.addColumn("col3");
    model.addColumn("col4");  

    for (int i=1;i<=100;i++){
       model.addRow(new Object[]{i,1,2,3});  //output jtable row
    }

    table.setPreferredScrollableViewportSize(new Dimension(500,50));
    table.setFillsViewportHeight(true);
    table.setSize(600, 200);
    table.setVisible(true);  

    //Add table to the panel
    myPanel.add(table);
    //Add panel to frame
    myFrame.getContentPane().add(myPanel);
    //Set size of the frame in px
    myFrame.setSize(400,300);
    //Set frame to visible
    myFrame.setVisible(true);
    }
}

Upvotes: 2

Andrey Pushin
Andrey Pushin

Reputation: 196

You can see tutorial or examples (http://docs.oracle.com/javase/tutorial/uiswing/examples/components/)

public static void main(String[] args) {
    //init frame - main component
    JFrame frame = new JFrame("SimpleTableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JPanel panel = new JPanel();
    frame.setContentPane(panel);

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("col1");
    model.addColumn("col2");
    model.addColumn("col3");
    model.addColumn("col4");

    for (int i=1;i<=100;i++){
        model.addRow(new Object[]{i,1,2,3});  //output jtable row
    }
    table.setSize(600, 200);
    JScrollPane scrollPane = new JScrollPane(table);
    panel.add(scrollPane);
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

Upvotes: 2

Related Questions