如何向JTable中插入数据?

发布于 2024-10-19 07:58:39 字数 1198 浏览 2 评论 0原文

我编写此代码用于在表中显示字符串。

但它没有显示并且没有任何效果。

什么是问题?

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

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

i write this code for showing strings in a table.

but it doesnt shown and has no effect.

what is 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);                      

 }

Edit:
I add Jtable on a panel.

in the main add panel to a jframe.

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

The table is shown but data isn't show.
the row and column of table is empty!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩心不撩汉 2024-10-26 07:58:39

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

You have to add the JTable to its parent-component and setVisible().

淤浪 2024-10-26 07:58:39

尝试这样的事情,只是为了让它运行。请注意,您删除了 pammel 方法中的 void,并添加了 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);                      

     }    
}

它有效!

Try something like this, just to make it run. Notice you drop the voidin the pammel method and a main was added.

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!

绝情姑娘 2024-10-26 07:58:39

代码真的是你自己写的吗?我记得这是在 Oracle Swing 教程中看到的。不管怎样,您还没有将 JTable 添加到组件中。观察下面的

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(在上述情况下)才能显示它。

Did you really write the code yourself? I remember this from an oracle swing tutorial. Anyway, you've not added the JTable to a component. Observe below 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();
            }
        });
    }
}

The JTable is added to the JPanel in the SimpleTableDemo constructor. Then the JPanel is set as the content pane in the for the main JFrame, which is made as frame.setVisible(true). This happens in the createAndShowGUI method. The reason your code doesn't display the JTable is because a JTable is an abstract widget. You need to add the abstract widget to a component, such as a JFrame (in the above case) for it to be displayed.

多彩岁月 2024-10-26 07:58:39

看起来新的 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);
}

It seams like the new JTable is not being added to pamnel.
Probably another instance, not initialized with data, is being added in 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)
    ...
}

The add method does not add a reference to the variable to the component, it only adds the value of the variable. So, if the variable is changed, the component still contains the previous (old) value.
Change the order of 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文