Note that there are some explanatory texts on larger screens.

plurals
  1. POautorefreshing jtable extracting collection data
    text
    copied!<p>I am a begginer with java programming.</p> <p>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.</p> <p>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</p> <pre><code>public class Table extends JFrame { private static JTable table; private static JButton addbuttin; private static TableModel model; private static Set&lt;Mats&gt; set = null; public static String colorString(String str, Mats a) { if (a.getTake().equals(new Boolean(true))) { return "&lt;html&gt;&lt;body&gt;&lt;font color=red&gt;" + str + "&lt;/font&gt;&lt;/body&gt;&lt;/html&gt;"; } else { return "&lt;html&gt;&lt;body&gt;&lt;font color=blue&gt;" + str + "&lt;/font&gt;&lt;/body&gt;&lt;/html&gt;"; } } 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&lt;Mats&gt;(); 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&lt;Mats&gt; 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 }); } } } </code></pre> <p>This is Class Mats simplified</p> <pre><code>public class Mats implements Comparable&lt;Mats&gt; { 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; } } </code></pre> <p>Class MatRespawn which changes Mats.take</p> <pre><code>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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload