如何将数据插入JTable?

时间:2011-02-24 15:43:39

标签: java swing jtable

我编写此代码以在表格中显示字符串。

但它没有显示,也没有效果。

什么是problrem?

 public pamnel() {
            initComponents();
             String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};



    Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
    };

     jTable1 = new JTable(data, columnNames);                      

 }
  

编辑:   我在面板上添加Jtable。

在主要的添加面板中添加到jframe。

 JFrame frame = new JFrame();
    frame.add(new pamnel());
    frame.setVisible(true);
    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

显示该表但未显示数据。 表的行和列是空的!

4 个答案:

答案 0 :(得分:3)

您必须将JTable添加到其父组件和setVisible()。

答案 1 :(得分:2)

你真的自己编写代码吗?我记得这是来自oracle swing教程。无论如何,您还没有将JTable添加到组件中。请注意以下source

package components;

/*
 * SimpleTableDemo.java requires no other files.
 */

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo extends JPanel {
    private boolean DEBUG = false;

    public SimpleTableDemo() {
        super(new GridLayout(1,0));

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
        };

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

JTable被添加到SimpleTableDemo构造函数中的JPanel中。然后将JPanel设置为主JFrame中的内容窗格,其作为frame.setVisible(true)。这种情况发生在createAndShowGUI方法中。你的代码没有显示JTable的原因是因为JTable是一个抽象的小部件。您需要将抽象小部件添加到组件,例如JFrame(在上面的例子中),以便显示它。

答案 2 :(得分:2)

尝试这样的事情,只是为了让它运行。请注意,您将void放在pammel方法中并添加了一个main。

import javax.swing.*;

class TableDemo { 
    JTable jTable1;
    public static void main( String ... args ) { 

       TableDemo tableDemo = new TableDemo();
       tableDemo.pamnel();

       JFrame frame = new JFrame();
       frame.add(  new JScrollPane(tableDemo.jTable1) );
       frame.pack();
       frame.setVisible( true );
    }
    public void initComponents(){
    }
    public void pamnel() {
            initComponents();
             String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};



        Object[][] data = {
            {"Kathy", "Smith",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"John", "Doe",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Sue", "Black",
             "Knitting", new Integer(2), new Boolean(false)},
            {"Jane", "White",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Joe", "Brown",
             "Pool", new Integer(10), new Boolean(false)}
        };

        jTable1 = new JTable(data, columnNames);                      

     }    
}

it works!

答案 3 :(得分:0)

新的JTable没有添加到pamnel 可能在initComponents()

中添加了另一个未使用数据初始化的实例
private JTable jTable1 = new JTable();    // empty table

public pamnel() {
    initComponents();

    String[] columnNames = ...
    Object[][] data = ...

    jTable1 = new JTable(data, columnNames);  // new instance created here
    // nothing happens with that new instance
}

private void initComponents() {
    ...
    add(jTable1);    // added empty table, not the one created above (constructor)
    ...
}

add方法不会向组件添加对变量的引用,仅 会添加变量的值。因此,如果更改变量,则组件仍包含先前(旧)值 更改create-add的顺序:

private JTable jTable1;

public pamnel() {
    initComponents();
}

private void initComponents() {
    ...
    createTable(); 
    add(jTable1);  
    ...
}

private void createTable() {
    String[] columnNames = ...
    Object[][] data = ...

    jTable1 = new JTable(data, columnNames);
}