2

So I have a couple Jpanels in my frame, and the idea is that, when one of the panels is selected, it will be highlighted. This is just the first step, because in the end, I want to be able to have buttons that act on the panels, but only the one that is highlighted.

I'm thinking that I want some kind of mouselistener on the panels, but I'm not sure how to implement this to get the results I want.

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

public class CustomComponent {
    Border emptyBorder  = BorderFactory.createEmptyBorder(1,1,1,1);
    Border selectBorder = BorderFactory.createLineBorder(Color.blue);
    JPanel[] panels;

    private JPanel getContent() {
        JLabel label = new JLabel();
        label.setText("fffffffff");
        label.setBounds(5, 5, 25, 25);

        JLabel label2 = new JLabel();
        label2.setText("HHHHHHHHHH");
        label2.setBounds(25, 25, 25, 25);

        JLabel label3 = new JLabel();
        label3.setText("YYYYYYYY");
        label3.setBounds(50, 50, 25, 25);

        JPanel mainPanel = new JPanel();
        mainPanel.setSize(new Dimension(300,300));
        JPanel panel1 = new JPanel();
        panel1.add(label);
        JPanel panel2 = new JPanel();
        panel2.add(label2);
        JPanel panel3 = new JPanel();
        panel3.add(label3);

        panel1.setBackground(Color.WHITE);
        panel2.setBackground(Color.MAGENTA);
        panel3.setBackground(Color.orange);

        panel1.setBorder(emptyBorder);
        panel2.setBorder(emptyBorder);
        panel3.setBorder(emptyBorder);

        panels = new JPanel[] { panel1, panel2, panel3 };

        mainPanel.add(panel1);
        mainPanel.add(panel2);
        mainPanel.add(panel3);
        return mainPanel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CustomComponent().getContent());
        frame.pack();
        frame.setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
bforcer
  • 147
  • 3
  • 13
  • 1
    *"when one of the panels is selected, it will be highlighted. This is just the first step, because in the end, I want to be able to have buttons.."* I'd try to focus input and awareness on the `JButton` instances. It is much simpler for the programmer and more intuitive for the user. – Andrew Thompson May 25 '13 at 06:24
  • The buttons will only be doing work on one of the jpanels (the highlighted one). They must be clicked first – bforcer May 25 '13 at 06:32
  • OK - I'll relent a little. But my point is to add some focusable components, then activate the panel 'alert color' when any of the focusable elements *has* focus, via keyboard or mouse (or `Robot` etc.). – Andrew Thompson May 25 '13 at 06:37
  • See ***[this](http://stackoverflow.com/questions/2135223/obtaining-focus-on-a-jpanel)*** post. and after that you have to override the `mouseEntered` method and perform the highlighting(may be with a border) – Extreme Coders May 25 '13 at 06:49
  • So I would have a mouselistner that will requestfocus on click. Then set the mouseEntered method to have a border? – bforcer May 25 '13 at 07:18
  • Tip: Add @ExtremeCoders (or whoever - the `@` is important) to *notify* them of a new comment. – Andrew Thompson May 25 '13 at 07:20

1 Answers1

6

Here is a sample code to show how to implement highlighting when mouse is over one of the JPanel. If you would want to highlight on click rather than on hover then implement the mousePressed and mouseReleased methods instead of mouseEntered and mouseExited

enter image description here

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseHoverDemo
{
    public static void main(String[] args)
    {
        new MouseHoverDemo();
    }

    MouseHoverDemo()
    {
        JFrame jFrame = new JFrame("Mouse Hover Demo");
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(new GridLayout(5,5));
        for(int i=0;i<25;i++) jFrame.add(new CustomPanel());
        jFrame.pack();
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }

    class CustomPanel extends JPanel implements MouseListener
    {
        Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
        Border redBorder = BorderFactory.createLineBorder(Color.RED,5);
        CustomPanel()
        {
            addMouseListener(this);
            setBorder(blackBorder);
            setFocusable(true);
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(50, 50);
        }

        @Override public void mouseClicked(MouseEvent e){}
        @Override public void mousePressed(MouseEvent e){}
        @Override public void mouseReleased(MouseEvent e){}

        @Override
        public void mouseEntered(MouseEvent e)
        {
            setBorder(redBorder);
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            setBorder(blackBorder);
        }
    }
}

UPDATE

For your newly required functionality change the code of customPanel to this

class CustomPanel extends JPanel implements MouseListener
    {
        boolean isHighlighted;
        Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
        Border redBorder = BorderFactory.createLineBorder(Color.RED,5);
        CustomPanel()
        {
            addMouseListener(this);
            setBorder(blackBorder);
            setFocusable(true);
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(200, 100);
        }

        @Override public void mouseClicked(MouseEvent e)
        {
            if(isHighlighted) setBorder(blackBorder);
            else setBorder(redBorder);
            isHighlighted=!isHighlighted;
        }

        @Override public void mousePressed(MouseEvent e){}
        @Override public void mouseReleased(MouseEvent e){}
        @Override public void mouseEntered(MouseEvent e){}
        @Override public void mouseExited(MouseEvent e){}
    }
Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
  • that's great, thanks. The hover code works fine. But the mouse clicked/released code doesn't work consistently for some reason. Any idea? – bforcer May 25 '13 at 08:29
  • @user2133337 Okay, it was a typo, you should implement `mousePressed` instead of `mouseClicked`. BTW clicking is a composite operation consisting of pressing and releasing, hence it was not working previously. – Extreme Coders May 26 '13 at 02:38
  • what if I want the button to stay highlighted once clicked, instead of turning back once I let go of the mouse click? Then turn back on the second click. – bforcer May 26 '13 at 05:18