开发者

Simple Focus Listener in Java

开发者 https://www.devze.com 2023-02-18 21:16 出处:网络
I have created a simple application with a Panel(extended by JPanel) which contains a small box(myMessagePanel - see the code below).

I have created a simple application with a Panel(extended by JPanel) which contains a small box(myMessagePanel - see the code below).

Basically, I want to generate focusevents from both the Panel and the box when the box is pressed. However, only focusGained and focusLost in the Panel is called. Here is the code:

myMessageBox is the box, Panel is the panel and FocusListenerTest contains the main

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Panel extends J开发者_StackOverflow中文版Panel implements MouseListener, FocusListener {
myMessageBox msgBox;

public Panel() {
    addMouseListener(this);
    addFocusListener(this);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.GREEN);
    msgBox = new myMessageBox(g);
    g.dispose();
}

public void focusGained(FocusEvent fe) {
    System.out.println("Focus gained in JPanel");
}

public void focusLost(FocusEvent fe){
    System.out.println("Focus lost in JPanel");
}       

public void mousePressed(MouseEvent me) {
    requestFocus();
    System.out.println("Mouse Pressed in JPanel");
}
public void mouseReleased(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
}

class myMessageBox extends Component implements FocusListener, MouseListener{
myMessageBox(Graphics g) {
    addMouseListener(this);
    addFocusListener(this);
    paintComponent(g);
    System.out.println("Done");
}

public void paintComponent(Graphics g) {
    g.fillRect(150,100,100,100);
}

public void mousePressed(MouseEvent me) {
    requestFocus();
    System.out.println("Mouse pressed on box");
}

public void mouseReleased(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}

public void focusGained(FocusEvent fe){
    System.out.println("Focus gained by box");
}

public void focusLost(FocusEvent fe) {
    System.out.println("Focus lost by box");
}
}

public class FocusListenertest {
public static void main(String args[]) {
    JFrame window = new JFrame();
    Panel content = new Panel();
    window.setContentPane(content);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLocation(100,75);
    window.setSize(400, 400);
    window.setVisible(true);
}
}

When I run this code, "Focus gained by box" is not printed when the myMessageBox is pressed.

Thanks

(PS: Didn't write comments as it is a simple application. Please let me know if comments are required)


In fact, there is no msgBox on your panel - it is just drawn over a panel, but from the structure's point of view, it does not exist at all. You need to add msgBox to the panel's children list, using standart JPanel's add method. Your myMessageBox class must inherit a JComponent from Swing, not Component from AWT, then your paintComponent method will override Swing's default and it will be invoked automatically from it's parent, Panel.

You must set your new component's size using setSize. Exact location of msgBox depends on your Panel's layout. It can be set explicitly with setLocation, if Panel's layout is null, or you may use one of more comfortable layouts.

As a side note, i recommend you to use some sort of a visual editor, such as an Eclipse's VE plugin.

See the followin working example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

public class SwingTestPanel extends JPanel implements MouseListener, FocusListener {
    MyMessageBox    msgBox    = new MyMessageBox();

    public SwingTestPanel(){
        initialize();
        addMouseListener(this);
        addFocusListener(this);
    }

    private void initialize(){
        this.setBackground(Color.GREEN);
        this.setLayout(null);
        this.setSize(new Dimension(446, 265));
        this.add(msgBox);
    }

    @Override
    public void focusGained(FocusEvent fe){
        System.out.println("Focus gained in JPanel");
    }

    @Override
    public void focusLost(FocusEvent fe){
        System.out.println("Focus lost in JPanel");
    }

    @Override
    public void mousePressed(MouseEvent me){
        requestFocus();
        System.out.println("Mouse Pressed in JPanel");
    }

    @Override
    public void mouseReleased(MouseEvent me){}

    @Override
    public void mouseClicked(MouseEvent me){}

    @Override
    public void mouseEntered(MouseEvent me){}

    @Override
    public void mouseExited(MouseEvent me){}

    static class MyMessageBox extends JComponent implements FocusListener, MouseListener {
        MyMessageBox(){
            initialize();
            addMouseListener(this);
            addFocusListener(this);
            System.out.println("Done");
        }

        private void initialize(){
            this.setName("msgBox");
            this.setEnabled(true);
            this.setSize(100, 100);
            this.setLocation(new Point(150, 100));
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.black);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        @Override
        public void mousePressed(MouseEvent me){
            requestFocus();
            System.out.println("Mouse pressed on box");
        }

        @Override
        public void mouseReleased(MouseEvent me){}

        @Override
        public void mouseClicked(MouseEvent me){}

        @Override
        public void mouseEntered(MouseEvent me){}

        @Override
        public void mouseExited(MouseEvent me){}

        @Override
        public void focusGained(FocusEvent fe){
            System.out.println("Focus gained by box");
        }

        @Override
        public void focusLost(FocusEvent fe){
            System.out.println("Focus lost by box");
        }
    }

    public static void main(String args[]){
        JFrame window = new JFrame();
        SwingTestPanel content = new SwingTestPanel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(100, 75);
        window.setSize(400, 400);
        window.setVisible(true);
    }


This should work

class Panel extends JPanel implements MouseListener, FocusListener {
    myMessageBox msgBox;

    public Panel() {
        addMouseListener(this);
        addFocusListener(this);
        msgBox = new myMessageBox();
        setLayout(null);
        add(msgBox);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.GREEN);
    }

    @Override
    public void focusGained(FocusEvent fe) {
        System.out.println("Focus gained in JPanel");
    }

    @Override
    public void focusLost(FocusEvent fe) {
        System.out.println("Focus lost in JPanel");
    }

    @Override
    public void mousePressed(MouseEvent me) {
        requestFocus();
        System.out.println("Mouse Pressed in JPanel");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    public static void main(String args[]) {
        JFrame window = new JFrame();
        Panel content = new Panel();
        window.setContentPane(content);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocation(100, 75);
        window.setSize(400, 400);
        window.setVisible(true);
    }

}

class myMessageBox extends JComponent implements FocusListener, MouseListener {
    myMessageBox() {
        addMouseListener(this);
        addFocusListener(this);
        System.out.println("Done");
        setBounds(150, 100, 100, 100);
        setVisible(true);
    }

    public void paintComponent(Graphics g) {
        Rectangle r = getBounds();
        Color saveColor = g.getColor();
        try {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, r.width, r.height);
        } finally {
            g.setColor(saveColor);
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
        requestFocus();
        System.out.println("Mouse pressed on box");
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void focusGained(FocusEvent fe) {
        System.out.println("Focus gained by box");
    }

    @Override
    public void focusLost(FocusEvent fe) {
        System.out.println("Focus lost by box");
    }
}
0

精彩评论

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