Results 1 to 4 of 4
  1. #1
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default How to make a JPanel highlighted on mouseclick

    Hi all,
    I have a main JPanel and 3 sub JPanels.
    sub JPanels are added to main JPanel.
    How can I highlight each of these sub JPanels on clicking on each of them.
    I need to have multiple select- deselect mode.
    Just the same working, like when we select deselect files/folders(CTRL+mouseclick).

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class Trial {
    	public static void main(String[] args) {
    		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);
    		
    		mainPanel.add(panel1);
    		mainPanel.add(panel2);
    		mainPanel.add(panel3);
    		
    		JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setVisible(true);
    	}
    }
    Rony
    Last edited by aneesahamedaa; 09-01-2008 at 07:38 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posted in the Sun Java forums: Swing - How to make a JPanel highlighted on mouseclick

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    public class TrialRx {
        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.addMouseMotionListener(mml);
            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 TrialRx().getContent());
            frame.pack();
            frame.setVisible(true);
        }
    
        private MouseMotionListener mml = new MouseMotionAdapter() {
            int selectedIndex = -1;
    
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                boolean hover = false;
                for(int i = 0; i < panels.length; i++) {
                    if(panels[i].getBounds().contains(p)) {
                        hover = true;
                        if(i != selectedIndex) {
                            panels[i].setBorder(selectBorder);
                            if(selectedIndex != -1) {
                                panels[selectedIndex].setBorder(emptyBorder);
                            }
                            selectedIndex = i;
                        }
                        break;
                    }
                }
                if(!hover && selectedIndex != -1) {
                    panels[selectedIndex].setBorder(emptyBorder);
                    selectedIndex = -1;
                }
            }
        };
    }

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  3. Styled Text with highlighted Odd Line
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:31 PM
  4. How to make a MouseClick paint an object
    By Devilsfutbol17 in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 12:54 PM
  5. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •