There was a small course assignment. I transfer data from the phone to the computer via Wi-Fi via sockets. I have 6 roles in the android application and from each I get a definite answer and write it down in the HashMap. The app needs to be done on a jframe. How do I display the contents of my Map in a JFrame window? The main problem is that the window in JFrame should be constantly updated, because not everyone will press a button at the same time. If possible, then with the example code, since I have not worked with JFrame for a long time. Answers like “go on the go” or “yes you don’t know anything at all, go teach” are not accepted. Thank you for understanding. If you need an example of my code, then write. Thank you in advance.

Closed due to the fact that off-topic participants Sergey Gornostaev , 0xdb , AivanF. , slippyk , Peter Samokhin Apr 25 '18 at 8:48 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- 0xdb, AivanF., slippyk, Peter Samokhin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What were you trying to do? What exactly did you fail? - Sergey Gornostaev
  • To be honest, I don’t know how to implement Map output. No mistakes. They need an elementary conclusion of the collection to the window - Artem Poteshkin
  • It depends on what output is expected in the window and what exactly is stored in the Map . You can, for example, convert Map to a string and stuff it into JTextArea , or you can loop around all Map elements and create a pair of JLabel for the key and JTextField for the value at each iteration. Thousands of them! ™ - Sergey Gornostaev
  • Probably the main problem is that my Map may be empty, and therefore I will throw a NullPointerException, because nobody their users can click on the button - Artem Poteshkin
  • And the Map should be displayed as "JUDGE 1: credited, JUDGE 2: not credited, JUDGE 3: credited" - Artyom Poteshkin

1 answer 1

A slightly modified example from the documentation.

 public class SimpleTableDemo extends JFrame { // Имена колонок private static final String[] columnNames = { "Судья", "Засчитано" }; private final JTable table; public SimpleTableDemo() { super("Demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(1, 0)); table = new JTable(new Object[0][0], columnNames) { @Override public Class getColumnClass(int column) { switch (column) { case 1: return Boolean.class; default: return String.class; } } @Override public boolean isCellEditable(int row, int col) { return false; } }; table.setPreferredScrollableViewportSize(new Dimension(500, 80)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); pack(); setVisible(true); } private void setData(Map<String, Boolean> map) { // Преобразовываем в двумерный массив для таблицы Object[][] data = map.entrySet() .stream() .map(e -> new Object[] { e.getKey(), e.getValue() }) .toArray(Object[][]::new); table.setModel(new DefaultTableModel(data, columnNames)); } private static void start() { SimpleTableDemo demo = new SimpleTableDemo(); // Исходные данные, приходящие из сети Map<String, Boolean> map = new HashMap<>(); map.put("Kathy", false); map.put("John", true); map.put("Sue", false); map.put("Jane", true); map.put("Joe", false); demo.setData(map); } public static void main(String[] args) { SwingUtilities.invokeLater(SimpleTableDemo::start); } } 

It turns out so

enter image description here

  • Thank you very much. - Artem Poteshkin
  • Can you tell me more about one topic? How is multithreading implemented in Swing? The fact is that people do not press the button at the same time and you need to constantly update the window for loading data. I wanted to register a constant window update in the second thread and after everyone answered, delete all data from the HashMap. - Artem Poteshkin
  • This answer should help you. - Sergey Gornostaev
  • Thank you, you really helped =) - Artem Poteshkin