Java添加表数据问题

发布于 2024-12-02 10:05:20 字数 1695 浏览 1 评论 0 原文

有人可以告诉我,我的错误在哪里? 我试图用数据填充表,但我不能,总是得到空表。 这是我的框架代码:

import java.util.*;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class Rinktis extends JFrame {
    private final JScrollPane scrollPane = new JScrollPane();
    private JTable table;

        public Rinktis() 
        {

        super();
        setBounds(100, 100, 861, 375);      
        try {
            jbInit();
        } catch (Throwable e) {
            e.printStackTrace();
        }       
    }

    Vector<Vector<String>> data1 =new Vector<Vector<String>>(); 
Vector<String> heading1 = new Vector<String>();
    public void tab(Vector heading, Vector data)
    {   System.out.println(data);
        System.out.println(heading);    
        data1=data;
        heading1=heading;
        System.out.println(data1);
        System.out.println(heading1);
    }   
    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        getContentPane().add(scrollPane);
        scrollPane.setBounds(10, 10, 825, 176);     

        table = new JTable(data1,heading1);
        scrollPane.setViewportView(table);
    }

}

这是我从另一个框架调用它的方法:

    protected void rinktisButton_1_actionPerformed(ActionEvent e) 
{       Rinktis frame = new Rinktis();
        frame.setVisible(true); 
    try {
        db.getClients();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

db.getClients(); 我调用 rinktis.tab(columnHeads,v); System.out.println 正确地为我提供了所有数据,但表是空的。

Can someone tell me, where is my mistake?
I'm trying to fill up table with data but I can't, always get empty table.
Here is my code with frame:

import java.util.*;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class Rinktis extends JFrame {
    private final JScrollPane scrollPane = new JScrollPane();
    private JTable table;

        public Rinktis() 
        {

        super();
        setBounds(100, 100, 861, 375);      
        try {
            jbInit();
        } catch (Throwable e) {
            e.printStackTrace();
        }       
    }

    Vector<Vector<String>> data1 =new Vector<Vector<String>>(); 
Vector<String> heading1 = new Vector<String>();
    public void tab(Vector heading, Vector data)
    {   System.out.println(data);
        System.out.println(heading);    
        data1=data;
        heading1=heading;
        System.out.println(data1);
        System.out.println(heading1);
    }   
    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        getContentPane().add(scrollPane);
        scrollPane.setBounds(10, 10, 825, 176);     

        table = new JTable(data1,heading1);
        scrollPane.setViewportView(table);
    }

}

Here's how I call it from another frame:

    protected void rinktisButton_1_actionPerformed(ActionEvent e) 
{       Rinktis frame = new Rinktis();
        frame.setVisible(true); 
    try {
        db.getClients();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

and from db.getClients(); I call rinktis.tab(columnHeads,v);
System.out.println gives me all data correctly, but table is empty.

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

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

发布评论

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

评论(3

永言不败 2024-12-09 10:05:20

将选项卡功能更改为如下所示:

public void tab(Vector heading, Vector data)
    {       
        table.setModel(new DefaultTableModel(data,heading) );
    }   

change your tab function to be like this:

public void tab(Vector heading, Vector data)
    {       
        table.setModel(new DefaultTableModel(data,heading) );
    }   
想你只要分分秒秒 2024-12-09 10:05:20

要获得严肃的答案,您需要发布 SSCCE。无论如何,您似乎将错误的类型传递给 JTable 构造函数

您需要传递一个 2d 对象数组 (Object [][]) 或 Vector> 而不是普通的 1D Vector,

我认为 与 TableHeader 相同问题是:

table = new JTable(data1, heading1); 
      //data1 need to of type Object[][] or Vector<Vector<Object>>
      //heading1 need to of type String[] or Vector<String>  

这是一个教程

For a serius answer you need to post an SSCCE. Anyway it seems that you are passing a wrong type to JTable constructor.

You need to pass a 2d array of object (Object [][]) or Vector<Vector<Object>> not plain 1D Vector, same as for TableHeader

I think here's the problem :

table = new JTable(data1, heading1); 
      //data1 need to of type Object[][] or Vector<Vector<Object>>
      //heading1 need to of type String[] or Vector<String>  

Here's a tutorial.

深海蓝天 2024-12-09 10:05:20

问题是该表不知道您更改了数据。您可以在创建表时加载数据。然后它应该会很好地显示(也许可以先尝试一下)。然后,在第二步中,您应该直接使用表模型并更改其中的数据。每当您这样做时,您都可以通知表进行更新。

或者,如果您已经知道标题,则可以设置它并稍后添加数据。这也应该有效。不过我不推荐这样做。

The problem is that the table does not know that you changed the data. You can either load your data on creation of the table. Then it should show up just fine (maybe try this out first). Then in a second step you should work directly with the tablemodel and change data there. Whenever you do so call the you can notify the table to update.

Alternatively, if you already know your header, you can set it and only add the data later. This also should work. However I do not recommend this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文