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 7721351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:54:11+00:00 2026-06-01T03:54:11+00:00

I’m working with Netbeans IDE in Java. I’ve a form with one JPanel. Each

  • 0

I’m working with Netbeans IDE in Java.

I’ve a form with one JPanel.
Each JPanel has a gridLayout 3×3 and in each place there is an image representing a number[0,1,2,3,4,5,6,7,8](the image is created used a custom class,not just fitting the image in a lab).

I want to be able to exchange two images in the panel when the user click them (First click: no action , second click: switch the two images fitted in the jPanel Components).

I already created a function exchangeComponents and with a test code (like:

exchangeComponents (0,8,jPanel1)

it exchanges correctly the images located in position1 (1st row,1st column) and in position2 (3rd row,3rd column).

The function a creted is the following:

public void exchangeComponents(int component1,int component2,JPanel jpanel){
    try{
   Component aux1 = jpanel.getComponent(component1);
   Point aux1Loc = aux1.getLocation();
   Component aux2 = jpanel.getComponent(component2);
   Point aux2Loc = aux2.getLocation();
   aux1.setLocation(aux2Loc);
   aux2.setLocation(aux1Loc);
   }
   catch (java.lang.ArrayIndexOutOfBoundsException ex){ /* error! bad input to the function*/
       System.exit(1);
   }
}

I suppose I neeed to have an event that call the function exchangeComponents() when the user click on one of the images on the jPanel1 but how should I do it? and how to check what components (images) the user has selected?
I just know that when I create a Button if a click on it (from the IDE) an event like

 private void button1ActionPerformed(java.awt.event.ActionEvent evt) {  
// some code..
}

is created and the code I fill in is executed.

Thank you in advance for any hint.

  • 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-06-01T03:54:12+00:00Added an answer on June 1, 2026 at 3:54 am

    You need to add the same mouse listener to all you JLabels or whatever container you have for your images, like:

    img1.addMouseListener(this);
    img2.addMouseListener(this);
    

    etc., then detect which Jlabel you clicked with MouseEvent.getSource(); , like this

    boolean hasclicked1=false;
    JLabel click1label=null;
    
    public void mouseClicked(MouseEvent me){
      if(!hasclicked1){ //clicked first pic
        hasclicked1 = true;
        click1label = (JLabel) me.getSource();
      } else { //clicked second pic
        hasclicked1 = false;
        exchangeComponents(click1label, (JLabel) me.getSource(), /*your jpanel here*/);
      }
      //now change exchangeComponents so it uses JLabels as parameters
    public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
      try{
        Component aux1 = component1;
        Point aux1Loc = aux1.getLocation();
        Component aux2 = component2;
        Point aux2Loc = aux2.getLocation();
        aux1.setLocation(aux2Loc);
        aux2.setLocation(aux1Loc);
      } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
       System.exit(1);
      }
    }
    

    If you are not using JLabels for the images though, replace JLabel in the code with whatever you are using…

    EDIT: Sorry, I don’t think I made this unclear, but your class with the method exchangeComponents has to implement MouseListener. Then, in the mouseClicked event put the code I gave for it. Make sure to include the variables hasclicked1 and click1label in your class. Make you class something like this

    public class ComponentExchanger implements MouseListener {
    boolean hasclicked1=false;
    JLabel click1label=null;
    JPanel mainPanel;
    public ComponentExchanger(){
       //create JFrame, JPanel, etc.
       JFrame f=new JFrame();
       //etc.
       mainPanel=new JPanel();
       f.add(mainPanel);
       //set layout of panel, etc.
       for(int i=0;i<9;i++){
          JLabel l=new JLabel(/*label image here*/);
          Point loc=new Point(/*coordinates here*/);
          l.setLocation(loc);
          mainPanel.add(l);
          /*more code*/
          f.setVisible(true);
       }
    }
    
    public static void main(String args[]){
       new ComponentExchanger();
    }
    
    
    public void mouseClicked(MouseEvent me){
      if(!hasclicked1){ //clicked first pic
        hasclicked1 = true;
        click1label = (JLabel) me.getSource();
      } else { //clicked second pic
        hasclicked1 = false;
        exchangeComponents(click1label, (JLabel) me.getSource(), mainPanel);
      }
      //now change exchangeComponents so it uses JLabels as parameters
    public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
      try{
        Component aux1 = component1;
        Point aux1Loc = aux1.getLocation();
        Component aux2 = component2;
        Point aux2Loc = aux2.getLocation();
        aux1.setLocation(aux2Loc);
        aux2.setLocation(aux1Loc);
      } catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
       System.exit(1);
      }
    }
    
    //Also, you will need to include the other mouselistener implemented methods, just 
    //leave them empty
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a text area in my form which accepts all possible characters from

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.