How can i update my Jpanel with the labels from another class in certain intervals?

Recommended Answers

All 14 Replies

Use a javax,swing.Timer to run the necessary code at the appropriate intervals?

can u send me some glimpse of code

Just Google - there's enough examples on the web, you don't need me to write another one!

I tried but I'm not done yet. Actually i need to update my UserNameListPaneL with the names that are available in the socket. I did this with the actionPerformed of a Button but when i try to update it automatically using timer I got null pointer exception i wrote code in timer and started timer in the constructor of UserNameListPaneL class. What mistake am i making? Please suggest.

Null pointer means an uninitialized variable. The Exception tells you which line of your program it was, and that's often enough to know which variable. If not print all the vars to see which it is. Then trace your logic backwards to find out why it's uninitialized. At a guess it's going to be a variable that you set in your actionPerformed of the Button, so it's uninitialized if the Timer runs first.

Timer timer = new Timer(5000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                usernamepanel.removeAll();
                usernamepanel.updateUI();

                System.out.println(MainFrame.userList);
                String[] arr = MainFrame.userList.split(";");//Error occurs here //when another user is entered
                for (int i = 0; i < arr.length; i++) {
                    //System.out.println(""+arr[i]);
                    label[i] = new JLabel(arr[i]);
                    usernamepanel.add(label[i]);
                    usernamepanel.revalidate();
                }
                System.out.println("timer acivated");
                usernamepanel.repaint();
            }

        });

Code is working for single user . Now i am starting tiimer with the help of button but i want to start it when the UserNameListPaneL is created.

So is it MainFrame.userList that is null? Only when the user or userList is changed somewhere else?

Let's pause for a moment and go back to your original question
How can i update my Jpanel with the labels from another class in certain intervals?
I now think I should have challenged the question, rather than answering it!

What really is your requirement? To update the GUI when the user changes? If so, a "polling" regular update isn't a good way to do it.

I'm not ignoring your advice.Its the loadshadding problem in our country ...i'm trying to implement it now. please co-operate that was the case of receiving data from the socket anytime but here i want t update my panel.

According to ur suggestion,""polling" regular update isn't a good way to do it. " what could be the best way..help please

what could be the better way to do it if polling regular update isn't a good way to do..

I'm not understanding why Mainframe.Userlist is null when it is modified by the addition of another user.According to you if pooling regular update is not a good way then how can i do it in another way...suggest plz

I did explain the two better ways when I replied to your other post. Did you read that?

Anyway, here's a new complete answer:

Synchronizing data between 2 classes
You have some data in object1 (an instance of Class1) that changes from time to time. You want object2 to use the latest data from object1. Eg Object1 gets data from incoming internet connections, object2 displays it in a GUI

1. The Worst way: Have loop or timer in object2 that checks object1 at regular intervals - either it's laggy or it's a resource hog.

2. A better way: write a public method in Class2 that accepts new data as a parameter and updates itself accordingly. Pass a reference to object2 into object1 and use that to call the public method. This is efficient and lag-free, but involves tangling the objects together more than is desirable.

3. The best way: Implement a Listener pattern in Class2 to allow other objects to register as listeners. When new data comes in, call all the registered listeners passing the new data. When setting up object1 add it as a listener to object2. This is better architecture (eg it's used throughout Swing), but more code. Depending on how "serious" the app solution 2 may be good enough.

hey thanx buddy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.