显示ArrayList的表

发布于 2024-10-26 01:23:16 字数 1302 浏览 1 评论 0 原文

我有一个 main.java,它有一个按钮,当您按下它时,它会调用一个方法并重新调整节点的 ArrayList; 我想在表中显示 ArrayList(Node 类中描述的 5 个字段) 如何做到这一点,问题是显示一些列表类型的字段?

Node.java

public class Node {
    private String name;
    private double value;
    private List<Node> first;
    private List<Node> second;
    private List<Double> values;

        //some methods... 
}

main.java

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
             JFrame frame = new JFrame("Red Bayesiana Visita a Asia ");
             JPanel panel = new JPanel();
             boton = new Button( "Get");
             panel.add(boton);
             frame.add(panel);

             ArrayList<Node>  arrayList;

             boton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         arrayList = method("file.txt");
                         //insert into table arrayList of 5 fields? 
                    }
            });

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(400, 400);
         frame.setVisible(true);
    }
}

I have a main.java that has a button, when you press it, it calls a method and retuns an ArrayList of Nodes;
I want to display the ArrayList in a table ( 5 fields as described in class Node)
How to do that, The problem is to display some fields as they are List type?

Node.java

public class Node {
    private String name;
    private double value;
    private List<Node> first;
    private List<Node> second;
    private List<Double> values;

        //some methods... 
}

main.java

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
             JFrame frame = new JFrame("Red Bayesiana Visita a Asia ");
             JPanel panel = new JPanel();
             boton = new Button( "Get");
             panel.add(boton);
             frame.add(panel);

             ArrayList<Node>  arrayList;

             boton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         arrayList = method("file.txt");
                         //insert into table arrayList of 5 fields? 
                    }
            });

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(400, 400);
         frame.setVisible(true);
    }
}

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

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

发布评论

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

评论(1

网名女生简单气质 2024-11-02 01:23:16

正如创建表模型<中所讨论的/a>,让Nodes扩展AbstractTableModel并实现所需的方法。使用生成的模型创建您的 JTable

附录:这是模型的概要。字段namevalue可以使用默认的渲染器,但您需要决定如何渲染每个节点中找到的列表

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/** @see http://stackoverflow.com/questions/5438516 */
public class Nodes extends AbstractTableModel {

    private List<Node> nodes = new ArrayList<Node>();

    @Override
    public int getRowCount() {
        return nodes.size();
    }

    @Override
    public int getColumnCount() {
        return 5; // A Node has five members
    }

    @Override
    public Object getValueAt(int row, int col) {
        Node node = nodes.get(row);
        switch (col) {
            case 0:
                return node.name;
            case 1:
                return node.value;
            case 2:
                return node.first;
            case 3:
                return node.second;
            case 4:
                return node.values;
            default:
                return null;
        }
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    private class Node {

        private String name;
        private double value;
        private List<Node> first;
        private List<Node> second;
        private List<Double> values;
    }
}

As discussed in Creating a Table Model, let Nodes extend AbstractTableModel and implement the required methods. Use the resulting model to create your JTable.

Addendum: Here's an outline of the model. The fields name and value can use the default renderer, but you'll have decide how to render the Lists found in each Node.

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/** @see http://stackoverflow.com/questions/5438516 */
public class Nodes extends AbstractTableModel {

    private List<Node> nodes = new ArrayList<Node>();

    @Override
    public int getRowCount() {
        return nodes.size();
    }

    @Override
    public int getColumnCount() {
        return 5; // A Node has five members
    }

    @Override
    public Object getValueAt(int row, int col) {
        Node node = nodes.get(row);
        switch (col) {
            case 0:
                return node.name;
            case 1:
                return node.value;
            case 2:
                return node.first;
            case 3:
                return node.second;
            case 4:
                return node.values;
            default:
                return null;
        }
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    private class Node {

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