简体   繁体   中英

Changing JLabel text from another class

As the title implies, I want to change the text of a JLabel from another class. My code is the following:

public class SelectSpaceShipScreen extends JPanel {
    private static final long serialVersionUID = 1L;

    SelectSpaceShipScreen() {
        this.setLayout(new BorderLayout());
        this.add(createCenterPanel(), BorderLayout.CENTER);
        this.add(createSouthPanel(), BorderLayout.SOUTH);
    }


    protected JPanel createSouthPanel() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel();
        panel.setPreferredSize(new Dimension(100, 100));
        panel.setBackground(Color.BLACK);
        panel.add(label);
        return panel;
    }
    private JPanel createCenterPanel() {
        JButton buttonSpaceShipZERO = new JButton();
        buttonSpaceShipZERO.setIcon(new ImageIcon(SpaceShipZERO.img));

        buttonSpaceShipZERO.addActionListener(new SpaceShipSelectionButtonHandler("ZERO"));
        buttonSpaceShipZERO.addMouseListener(new SpaceShipHoverhandler("ZERO"));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout());
        panel.add(buttonSpaceShipZERO);;
        panel.setBackground(Color.BLACK);
        return panel;
    }

And for the Mouse Handler Class:

class SpaceShipHoverhandler implements MouseListener {
    String name;

    public SpaceShipHoverhandler(String name) {
        this.name = name;
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}

I just want to change the label text in the createSouthPanel when the mouse enters. What would be the optimal approach? I've been stuck on this for a while.

This is a common problem that is solved using the Model-View-Control (MVC) pattern. Sometimes these are all in one class, and sometimes separate classes.

Here is a link to a solution for treating Areas(ie shapes) like (almost) first class components ( https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/ )

Inside AreaManager, you can see (at line 205) how AreaManager has an AreaModel, as well as a selection listener and a few other things. You can see in the main method, that after adding shapes and some styles to the AreaModel, AreaManager creates a JPanel and installs an AreaRenderer on it. The first line of the overridden paintComponent method creates the renderer, and later in paintComponent it is called to render all the shapes in the ShapeModel.

This is the way to do it: You need to have a controller "brains" class that can coordinate the model with the view (rendering) and UI events.

I solved it by creating a static label and changing that through the mouse handler.

static JLabel southLabel = new JLabel();
...

private JPanel createSouthPanel() {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(100, 100));
        panel.setBackground(Color.BLACK);
        panel.add(southLabel);
        return panel;
    }

And then simply:

@Override
    public void mouseEntered(MouseEvent e) {
        SelectSpaceShipScreen.southLabel.setText(this.name);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        SelectSpaceShipScreen.southLabel.setText("");
    }

A big thanks to my friend Hlias for the assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM