自动刷新jtable提取集合数据

发布于 2024-11-08 20:35:07 字数 6409 浏览 0 评论 0原文

我是java编程的初学者。

我正在尝试制作一个读取集合集 hashSet 的 JTable,并在每次更改集合时刷新,还想在表所在的 JPanel 上每 3 秒重新绘制一次自动刷新(这在原始程序中有效)。 另外,类 Mats 会随着时间的推移更改布尔值,我想根据集合中该垫子中的 Mats.take 值来更改其在 JTable 中扭动的 Mats.name 的颜色,这就是自动刷新的原因。

这是表的示例,它从头开始重新制作表,每次添加新行都会抛出 java.lang.NullPointerException。 我根本不明白为什么? 感谢您提前提供帮助

public class Table extends JFrame {
    private static JTable table;
    private static JButton addbuttin;
    private static TableModel model;
    private static Set<Mats> set = null;

    public static String colorString(String str, Mats a) {
        if (a.getTake().equals(new Boolean(true))) {
            return "<html><body><font color=red>" + str
                    + "</font></body></html>";
        } else {
            return "<html><body><font color=blue>" + str
                    + "</font></body></html>";
        }
    }

    public static void main(String[] argv) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addbuttin = new JButton("Dodaj");
        Mats a = new Mats(10, 10, 5, "WOOD", true);
        Mats b = new Mats(10, 12, 5, "WOOD", false);

        set = new HashSet<Mats>();
        set.add(a);
        set.add(b);

        addbuttin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Mats c = new Mats(10, 12, 6, "WOOD", true);
                set.add(c);
            }
        });

        JPanel p = new JPanel();

        model = new DefaultTableModel() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
/**here is java.lang.NullPointerException*/
        table.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.equals(TableModelEvent.INSERT)) {
                    model = new DefaultTableModel();
                    addColumns();
                    adddata(set);
                }

            }
        });
        addColumns();
        adddata(set);
        table = new JTable(model);

        JScrollPane scrollPane = new JScrollPane(table,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        p.add(scrollPane, BorderLayout.CENTER);
        p.add(addbuttin, BorderLayout.EAST);
        p.setSize(500, 400);

        frame.add(p);
        frame.setSize(600, 600);
        frame.setVisible(true);

    }

    private static void addColumns() {
        ((DefaultTableModel) model).addColumn("NAME");
    }

    private static void adddata(Set<Mats> set) {
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            Mats mats = (Mats) iterator.next();

            String n = colorString(mats.getName(), mats);
            ((DefaultTableModel) model).insertRow(model.getRowCount(), new Object[] { n });
        }
    }

}

这是 Mats 类简化的

public class Mats implements Comparable<Mats> {


    private String name;
    private Boolean take;

    /**
     *
     */
    public Mats() {
    }

    /**
     * 
     * @param name
     * @param uzet
     */
    public Mats(String name, boolean take) {

        this.name = name.toUpperCase();
        this.take = take;
        taken();
    }

    /**
     * 
     * @param Material
     */
    public Mats(Mats Material) {
                this.name = Material.getName().toUpperCase();
        this.take = Material.getTake();
        Material.taken();
    }

        /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name.toUpperCase();
    }

    /**
     * @param take
     *            the take to set
     */
    public void setTake(Boolean take) {
        this.take = take;
    }

    /**
     * @return the take
     */
    public Boolean getTake() {
        return take;
    }



    /**
     * 
     * @return
     */
    @Override
    public String toString() {
        return "Material: ".toUpperCase() + getName() + "\n";
    }

    /**
     * 
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        Mats a = (Mats) obj;
        if (obj == null) {
            return false;
        }
        if (a.getName() == this.getName()) {
            return true;
        }
        return false;
    }

    /**
     * 
     * @return
     */
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + this.getX();
        hash = 83 * hash + this.getY();
        hash = 83 * hash
                + (this.getName() != null ? this.getName().hashCode() : 0);
        return hash;
    }

    /**
     *
     */
    public void taken() {
        MatRespawn s = new MatRespawn(1000, take);
        s.start();
    }

    @Override
    public int compareTo(Mats o) {
        Integer a = this.getName();
        if (a.compareTo(o.getName()) != 0)
            return a.compareTo(o.getName());
        return 0;
    }
}

MatRespawn 类,它更改了 Mats.take

public class MatRespawn extends Thread {
    private int time;
    private Boolean taken;

    public MatRespawn(int time, Boolean take) {
        this.time = time;
        this.taken = take;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public Boolean getTaken() {
        return taken;
    }

    public void setTaken(Boolean taken) {
        this.taken = taken;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (taken.equals(new Boolean(true))) {
                    sleep(time * 10);
                    //JOptionPane.showMessageDialog(null, "dostupnost: " + taken);
                    taken = false;
                }

                sleep(time);
                //JOptionPane.showMessageDialog(null, "dostupnost: " + taken);
                taken = true;

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

I am a begginer with java programming.

I am trying to make a JTable that reads collection set hashSet, and refreshes eachtime set is changed, also want to make autorefresh every 3 sec repaint on JPanel where table will be on (which works in original program).
Also Class Mats changes Boolean take over time, i want to change color its writhing Mats.name in JTable depending on the value Mats.take in that mat in set which is the reason for autorefresh.

This is a sample for table its remakeing table from scrach with each time new line is added throws java.lang.NullPointerException.
I simply do not see why?
Thx for the help in advance

public class Table extends JFrame {
    private static JTable table;
    private static JButton addbuttin;
    private static TableModel model;
    private static Set<Mats> set = null;

    public static String colorString(String str, Mats a) {
        if (a.getTake().equals(new Boolean(true))) {
            return "<html><body><font color=red>" + str
                    + "</font></body></html>";
        } else {
            return "<html><body><font color=blue>" + str
                    + "</font></body></html>";
        }
    }

    public static void main(String[] argv) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addbuttin = new JButton("Dodaj");
        Mats a = new Mats(10, 10, 5, "WOOD", true);
        Mats b = new Mats(10, 12, 5, "WOOD", false);

        set = new HashSet<Mats>();
        set.add(a);
        set.add(b);

        addbuttin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Mats c = new Mats(10, 12, 6, "WOOD", true);
                set.add(c);
            }
        });

        JPanel p = new JPanel();

        model = new DefaultTableModel() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
/**here is java.lang.NullPointerException*/
        table.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.equals(TableModelEvent.INSERT)) {
                    model = new DefaultTableModel();
                    addColumns();
                    adddata(set);
                }

            }
        });
        addColumns();
        adddata(set);
        table = new JTable(model);

        JScrollPane scrollPane = new JScrollPane(table,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        p.add(scrollPane, BorderLayout.CENTER);
        p.add(addbuttin, BorderLayout.EAST);
        p.setSize(500, 400);

        frame.add(p);
        frame.setSize(600, 600);
        frame.setVisible(true);

    }

    private static void addColumns() {
        ((DefaultTableModel) model).addColumn("NAME");
    }

    private static void adddata(Set<Mats> set) {
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            Mats mats = (Mats) iterator.next();

            String n = colorString(mats.getName(), mats);
            ((DefaultTableModel) model).insertRow(model.getRowCount(), new Object[] { n });
        }
    }

}

This is Class Mats simplified

public class Mats implements Comparable<Mats> {


    private String name;
    private Boolean take;

    /**
     *
     */
    public Mats() {
    }

    /**
     * 
     * @param name
     * @param uzet
     */
    public Mats(String name, boolean take) {

        this.name = name.toUpperCase();
        this.take = take;
        taken();
    }

    /**
     * 
     * @param Material
     */
    public Mats(Mats Material) {
                this.name = Material.getName().toUpperCase();
        this.take = Material.getTake();
        Material.taken();
    }

        /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name.toUpperCase();
    }

    /**
     * @param take
     *            the take to set
     */
    public void setTake(Boolean take) {
        this.take = take;
    }

    /**
     * @return the take
     */
    public Boolean getTake() {
        return take;
    }



    /**
     * 
     * @return
     */
    @Override
    public String toString() {
        return "Material: ".toUpperCase() + getName() + "\n";
    }

    /**
     * 
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        Mats a = (Mats) obj;
        if (obj == null) {
            return false;
        }
        if (a.getName() == this.getName()) {
            return true;
        }
        return false;
    }

    /**
     * 
     * @return
     */
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + this.getX();
        hash = 83 * hash + this.getY();
        hash = 83 * hash
                + (this.getName() != null ? this.getName().hashCode() : 0);
        return hash;
    }

    /**
     *
     */
    public void taken() {
        MatRespawn s = new MatRespawn(1000, take);
        s.start();
    }

    @Override
    public int compareTo(Mats o) {
        Integer a = this.getName();
        if (a.compareTo(o.getName()) != 0)
            return a.compareTo(o.getName());
        return 0;
    }
}

Class MatRespawn which changes Mats.take

public class MatRespawn extends Thread {
    private int time;
    private Boolean taken;

    public MatRespawn(int time, Boolean take) {
        this.time = time;
        this.taken = take;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public Boolean getTaken() {
        return taken;
    }

    public void setTaken(Boolean taken) {
        this.taken = taken;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (taken.equals(new Boolean(true))) {
                    sleep(time * 10);
                    //JOptionPane.showMessageDialog(null, "dostupnost: " + taken);
                    taken = false;
                }

                sleep(time);
                //JOptionPane.showMessageDialog(null, "dostupnost: " + taken);
                taken = true;

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

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

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

发布评论

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

评论(2

枕梦 2024-11-15 20:35:07

当您调用 table.getModel() 时,您尚未调用 table = new JTable(model);
这意味着 table 仍然是 null,因此您会收到异常。尝试将构造 table 的行移动到访问它的行之前。

At the time you call table.getModel(), you haven't yet called table = new JTable(model);
That means that table is still null, and so you get an exception. Try moving the line that constructs table to before the line that accesses it.

歌枕肩 2024-11-15 20:35:07

发现还有什么问题

private static TableModel model;

后来我做了

model = new DefaultTableModel() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };

应该是
私有静态DefaultTableModel模型;

Found what else was wrong

private static TableModel model;

later i made

model = new DefaultTableModel() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };

Should have been
private static DefaultTableModel model;

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