开发者

Java Swing Threading Problem

I have a problem related to using threads in swing. I will first explain the problem, and then I will post my code.

The problem is the following. I have two classes, one containing the business logic, and the other one containing the user interface. Based on user actions in the interface, I call methods in the class that contains business logic. However, my next task is to have a user click on an UI element, and that based on that click I attach a new mouse listener to other UI element, and I wait for the user to click that other UI element, after which I would like to change the initialy clicked UI element. However, my application blocks after clicking on the other UI element. In the example code, both the initial UI element, and the other UI element are the same, but in general they will not be.

Example Classes:

====================================================================

package threadtests;

import java.awt.Color;

public class LogicClass extends Thread {

    FrameClass fc;
    SelectSynchronizerObject mysso;

    public void run() {
        while(true);
    }

    public void startThisFromFrame() {
        MyMouseListener2 m2 = new MyMouseListener2();
        m2.initializ开发者_StackOverflowe(fc.jp, mysso);
        fc.jp.addMouseListener(m2);
        mysso.getSelected();
        fc.jp.setBackground(new Color(100,100,100));
        fc.jp.removeMouseListener(m2);
    }

    public static void main(String args[]) {
        SelectSynchronizerObject sso = new SelectSynchronizerObject();
        LogicClass lc = new LogicClass();
        lc.mysso = sso;
        lc.start();
        FrameClass fc = new FrameClass();
        fc.lc = lc;
        lc.fc = fc;
        fc.mysso = sso;
        fc.initialize(lc);
    }

}

====================================================================

package threadtests;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameClass extends JFrame {

    LogicClass lc;
    SelectSynchronizerObject mysso;
    JPanel jp = new JPanel();

    public void initialize(LogicClass arg) {
        lc = arg;
        setSize(100,100);
        MyMouseListener m = new MyMouseListener(); 
        jp.addMouseListener(m);
        m.initialize(jp, lc);
        jp.setBackground(new Color(255,200,200));
        add(jp);

        setVisible(true);
    }

}

====================================================================

package threadtests;

public class SelectSynchronizerObject {

    public int selectednumer = 0;
    public boolean numberset = false;

    public synchronized void panelSelected(int a) {
        selectednumer = a;
        notify();
    }

    public synchronized int getSelected() {
        try {
            wait();
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        return selectednumer;
    }

}

====================================================================

package threadtests;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class MyMouseListener implements MouseListener  {

    JPanel mypanel;
    LogicClass lc;
    public void initialize(JPanel arg, LogicClass arg2) {
        mypanel = arg;
        lc = arg2;
    }
    public void mouseClicked(MouseEvent e) {
        lc.startThisFromFrame();
    }
    public void mousePressed(MouseEvent e) {

    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
        mypanel.setBackground(new Color(200,255,200));
    }
    public void mouseExited(MouseEvent e) {
        mypanel.setBackground(new Color(255,200,200));
    }

}

====================================================================

package threadtests;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class MyMouseListener2 implements MouseListener  {

    JPanel mypanel;
    SelectSynchronizerObject sso;
    public void initialize(JPanel arg, SelectSynchronizerObject arg1) {
        mypanel = arg;
        sso = arg1;
    }
    public void mouseClicked(MouseEvent e) {
        sso.panelSelected(1);
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }

}

==========================

Thanks for the help.


First:

public void run() {
    while(true);
}

just takes CPU from doing other stuff for nothing.

Then:

wait();

is being called in the main Thread and thus blocks.

Actually you don't need invokelater, swingworker or anything, you have just a little GUI event programming do you . Wire event listeners together at the beginning, then you get your selected panel without any Threads.

Generally while Threads are necessary for quite a lot of programs you don't need to force them where they are not necessary, because they add a lot of complexity to programming.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜