| |

VerySource

 Forgot password?
 Register
Search
View: 1053|Reply: 3

Floating menu and tree issues

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-19 16:30:01
| Show all posts |Read mode
The problem requirements are as follows: Click the floating menu on the tree and select "Add Child Node" to generate 100 nodes. The 100 nodes are required to be dynamically displayed in the tree during the generation process, but now the situation is: 1, only all nodes are generated After that, they are displayed together. 2.When all nodes are not generated, the floating menu is maintained. Only when all nodes are generated, the floating menu disappears.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 India

Post time: 2020-6-24 22:30:01
| Show all posts
Typical Swing/AWT Event Dispatch Thread long-term occupation problem.
The method of handling events Before returning, all subsequent events of AWT's EventQueue, including the PAINT event, are blocked, so your interface cannot be refreshed, although you have updated the tree model.
Pay attention to Swing/AWT including SWT programming, and pay attention to two issues when dealing with long-term and heavy workload tasks:
* Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive.
Don't run tasks that consume events on the Event Dispatch Thread. Otherwise, the interface will be dead.
* Swing components should be accessed on the Event Dispatch Thread only.
Swing components can only be accessed in the Event Dispatch Thread.
A new class called SwingWorker was introduced in JDK 5 and JDK 6 to specifically solve this problem. For details, please refer to java API help:
http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/zh_CN/api/javax/swing/SwingWorker.html
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 India

Post time: 2020-6-25 18:15:01
| Show all posts
Give you a sample code:
//This method is the event processing method of the floating menu
private void actionPerform(ActionEvent e){
MySwingWorker msw=new MySwingWorker(yourTree);
   msw.execute();
}
//The following is the implementation of MySwingWorker:
public class MySwingWorker extends SwingWorker{
    private JTree tree;
    public MySwingWorker(JTree tree) {
        this.tree=tree;
    }
    protected Object doInBackground() throws Exception {
        for(int i=0;i<100;i++){
            //Generate a node and add it to the tree...
            //...;
            //Notify the EDT thread to refresh the tree
            publish();
        }
        return null;
    }
    protected void process(List<Object> chunks) {
        tree.repaint();
    }
}
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-28 20:45:01
| Show all posts
I used another thread to handle it.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list