A
A
Artem Poteshkin2018-04-24 22:39:07
Java
Artem Poteshkin, 2018-04-24 22:39:07

How to output HashMap to JFrame window?

Good evening. There was a small assignment. I am transferring data from phone to computer over wifi via sockets. I have 6 roles in an android application and from each I get a specific response and write it to the HashMap. Application needs to be made on JFrame. How to display the contents of my Map in a JFrame window? If possible, then with a code example, because. Haven't worked with JFrame for a long time. The main problem is that the HashMap can be empty, because no one will press the button immediately, but will press, for example, after 30 seconds. It is necessary that the window is updated and loaded with new data. Answers like "google" or "yes, you don't know anything at all, go learn" are not accepted. Thanks for understanding. This class processes the HashMap and sends it to another class with a JFrame. Thanks in advance.

public class Server{
    private static Socket socket;
    private static ServerSocket serverSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;
    private static int user_id;
    private static boolean answer;
    private static String answerString;
    private static Map<String,Object> answerMap = new HashMap<String,Object>();
        public static Map getAnswerMap() {
        return answerMap;
    }

    public static void setAnswerMap(Map answerMap) {
        Server.answerMap = answerMap;
    }

    public Server(){}

    public static int getUser_id() {
        return user_id;
    }

    public static void setUser_id(int user_id) {
        Server.user_id = user_id;
    }

    public static boolean isAnswer() {
        return answer;
    }

    public static void setAnswer(boolean answer) {
        Server.answer = answer;
    }

    public static String getAnswerString() {
        return answerString;
    }

    public static void setAnswerString(String answerString) {
        Server.answerString = answerString;
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        try {
            serverSocket = new ServerSocket(6000);
            while (true){
                socket = serverSocket.accept();
                inputStreamReader = new InputStreamReader(socket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
                message = bufferedReader.readLine();

                int[] messageToInt = new int[message.length()];
                for (int i = 0; i < message.length(); i++){
                    messageToInt[i] = message.charAt(i) - '0';
                    user_id = messageToInt[0];
                    if (messageToInt[1] == 1){
                        answer = true;
                        answerString = "Засчитано";
                    } else {
                        answer = false;
                        answerString = "Не засчитано";
                    }
                    answerMap.put(user_id,answerString);
                }
                for (Object key : answerMap.keySet()) {
                    System.out.println("Key: " + key);
                }
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2018-04-25
@jamakasi666

The dumbest option is to wrap the answerMap with your own Wishlist, for example, the put and remove methods, and already shove them onto the form, for example, in jtable. Stupid but the fastest option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question