Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:12:57+00:00 2026-05-27T04:12:57+00:00

Is there a way to toggle a read-only mode so when you click any

  • 0

Is there a way to toggle a read-only mode so when you click any object in your window it simply returns what you clicked, ignoring the object’s usual event handling? IE, while in this “read-only” mode, if you click on a Button, it simply returns the button, not actually pressing the button. Then I could do something like:

if ("thing pressed" == button) "do this";
else if ("thing pressed" == panel) "do that";
else "do nothing";

Here’s my code, its a frame with 3 colored boxes. Clicking the 2nd box, the 3rd box, or the background will display a message. Clicking box 1 does nothing. I like using new mouse adapters so I want to do it this way.

Now what I want is when you click box 1, box 1 is treated as selected (if that helps you get the picture). Then if you click anywhere, including box 1 again, box 1 is deselected and nothing else (meaning that box 2, box 3. or the background’s message will display). At that time, only if box 2 or 3 were clicked, they will still not display their normal message but a different message would be displayed.

I’m very sorry if I come off a little short.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

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

    Square l1, l2, l3;
    public Labels() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        l1 = new Square();
        l2 = new Square();
        l3 = new Square();

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(120, 150);
        frame.setResizable(false);

        panel.setVisible(true);
        panel.setLayout(null);
        l1.setLocation(5, 5);
        l2.setLocation(5, 60);
        l3.setLocation(60, 5);

        l2.setColor("yellow");
        l3.setColor("black");

        l1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //do nothing
            }
        });

        l2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Pushed label 2");
            }
        });
        l3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Pushed label 3");
            }
        });
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("pushed background");
            }
        });

        frame.add(panel);
        panel.add(l1);
        panel.add(l2);
        panel.add(l3);
    }

    class Square extends JLabel{
        Color color = Color.blue;

        public Square() {
            // TODO Auto-generated constructor stub\
            setVisible(true);
            setSize(50,50);
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(color);
            g.fillRect(0, 0, 50, 50);
        }
        public void setColor(String color){
            if (color == "white") this.color = Color.white;
            else if (color == "black") this.color = Color.black;
            else if (color == "yellow") this.color = Color.yellow;
            else {
                System.out.println("Invalid color");
                return;
            }
            repaint();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:12:58+00:00Added an answer on May 27, 2026 at 4:12 am

    Don’t disable anything. Simply change the state of your class, perhaps by using a few boolean flag variables/fields and change these flags depending on what is pressed.

    So have boolean fields called label1PressedLast, label2PressedLast, and label3PressedLast or something similar, and when a label is pressed, check the states of all other flags and have your program’s behavior change depending on the state of these flags and the label that was just pressed. Then set all flags to false except for the one corresponding to the label that was just pressed.

    For example, this little program reacts only if the first and then the third JLabel have been pressed:

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.*;
    
    public class FlagEg extends JPanel {
       private static final int LABEL_COUNT = 3;
       private JLabel[] labels = new JLabel[LABEL_COUNT];
       private boolean[] flags = new boolean[LABEL_COUNT];
    
       public FlagEg() {
          setLayout(new GridLayout(1, 0, 20, 0));
          setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
    
          // panel mouse listener
          addMouseListener(new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent arg0) {
                inactivateAll();
             }
          });
    
          MouseListener labelsMouseListener = new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent mouseEvt) {
                myMousePressed(mouseEvt);
             }
          };
    
          // create JLabels and add MouseListener
          for (int i = 0; i < labels.length; i++) {
             labels[i] = new JLabel("Label " + (i + 1));
             labels[i].addMouseListener(labelsMouseListener);
             labels[i].setOpaque(true);
             labels[i].setBorder(BorderFactory.createLineBorder(Color.black));
             add(labels[i]);
          }
       }
    
       private void inactivateAll() {
          for (int i = 0; i < labels.length; i++) {
             labels[i].setBackground(null);
             flags[i] = false;
          }
       }
    
       private void myMousePressed(MouseEvent mouseEvt) {
          JLabel label = (JLabel) mouseEvt.getSource();
    
          // which label was pressed?
          int index = -1;
          for (int i = 0; i < labels.length; i++) {
             if (label == labels[i]) {
                index = i;
             }
          }
    
          // check if first label and then third pressed:
          if (flags[0] && index == 2) {
             System.out.println("first and then third label pressed!");
          }
    
          // reset all labels and flags to initial state
          inactivateAll();
    
          // set pressed label background color and set flag of label just pressed
          labels[index].setBackground(Color.pink);
          flags[index] = true;
    
       }
    
       private static void createAndShowGui() {
          FlagEg mainPanel = new FlagEg();
    
          JFrame frame = new JFrame("Flag Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    Logic iteration two: only label 1 is the “primer” JLabel. This is actually easier to implement, because now you only need one boolean flag, that representing label 1 being pressed:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FlagEg2 extends JPanel {
       private static final int LABEL_COUNT = 3;
       private JLabel[] labels = new JLabel[LABEL_COUNT];
    
       private boolean label1Flag = false;
    
       public FlagEg2() {
          setLayout(new GridLayout(1, 0, 20, 0));
          setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
    
          // panel mouse listener
          addMouseListener(new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent arg0) {
                inactivateAll();
             }
          });
    
          MouseListener labelsMouseListener = new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent mouseEvt) {
                myMousePressed(mouseEvt);
             }
          };
    
          // create JLabels and add MouseListener
          for (int i = 0; i < labels.length; i++) {
             labels[i] = new JLabel("Label " + (i + 1));
             labels[i].addMouseListener(labelsMouseListener);
             labels[i].setOpaque(true);
             labels[i].setBorder(BorderFactory.createLineBorder(Color.black));
             add(labels[i]);
          }
       }
    
       private void inactivateAll() {
          for (int i = 0; i < labels.length; i++) {
             labels[i].setBackground(null);
             label1Flag = false;
          }
       }
    
       private void myMousePressed(MouseEvent mouseEvt) {
          JLabel label = (JLabel) mouseEvt.getSource();
    
          // which label was pressed?
          int index = -1;
          for (int i = 0; i < labels.length; i++) {
             if (label == labels[i]) {
                index = i;
             }
          }
    
          if (label1Flag) {
             if (index == 1) {
                System.out.println("Label 1 and label 2 pressed");
             } else if (index == 2) {
                System.out.println("Label 1 and label 3 pressed");
             }
    
          }
    
          // reset all labels and flags to initial state
          inactivateAll();
    
          // if label1, then activate it
          if (index == 0) {
             labels[0].setBackground(Color.pink);
             label1Flag = true;
          }
    
       }
    
       private static void createAndShowGui() {
          FlagEg2 mainPanel = new FlagEg2();
    
          JFrame frame = new JFrame("Flag Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any easy way to toggle do/end and {} in ruby in Vim?
I'm using simple jQuery toggle method: $('fieldset').click(function () { $('fieldset>ul').toggle(slow); }); Is there any
Without using Javascript, is there a way to make a CSS property toggle on
Is there way in next piece of code to only get the first record?
Is there a way to enforce constraint checking in MSSQL only when inserting new
Is there any way to check whether a file is locked without using a
I wanted to ask if there is any way to control the volume of
Is there a short way to toggle a boolean? With integers we can do
Using JavaScript, is there a way to update window.location.hash without scrolling the web page?
UPDATED As the title says, is there a way to toggle the Hidden or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.