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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:20:41+00:00 2026-05-31T14:20:41+00:00

I am fairly new to java so bear with me please, basically, below I

  • 0

I am fairly new to java so bear with me please, basically, below I have a tabbed pane for each of the four rooms in the rooms Arraylist, and I am creating buttons in each tab depending how many lights each room has, How can I associate the buttons in each tab with a specified the rooms?. So like when I click the light button in the Room 1 tab, the event listener knows that the button belongs to the room1?

Any help is appreciated, thanks.

import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MasterGUI extends JFrame implements ActionListener{

public MasterGUI(){

}

public void DisplayFrame(){
    ArrayList<Rooms> rooms;
    rooms = Building.getRoomList();

    JFrame master = new JFrame("Solar Master Control Panel"); 
    master.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = master.getContentPane();
    content.setBackground(Color.lightGray);

    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel tmpPanel;

    for(int x = 0; x < rooms.size(); x++){
        tmpPanel = new JPanel();
        String roomName = rooms.get(x).getName();
        int id = rooms.get(x).getId();
        tabbedPane.addTab(roomName + " Room " + id, tmpPanel);
    }

    for(int x = 0; x < rooms.size(); x++){
        for(int i = 0; i < rooms.get(x).roomLights.size(); i++){
            int num = i + 1;
            ((JPanel) tabbedPane.getComponentAt(x)).add(new JButton("Light" + num));
        }
    }

    master.add(tabbedPane, BorderLayout.CENTER);
    master.setSize(800, 600);
    content.add(tabbedPane);
    master.setVisible(true);
}

public void actionPerformed(ActionEvent e){

}  
  • 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-31T14:20:43+00:00Added an answer on May 31, 2026 at 2:20 pm

    First of, you need to add the ActionListener to the button so it will be called when the button is clicked.

    ...
    JButton button = new JButton("Light" + num);
    button.addActionListener(this);
    ((JPanel) tabbedPane.getComponentAt(x)).add(button);
    ...
    

    As far as differentiating between which button was clicked, there are two main ways to address this. The first is to use getSource() on the ActionEvent to get a reference to the object that triggered the event. You can use this to decide how you want to proceed further. The other option is to have MasterGUI not implement ActionListener. Instead, make a unique ActionListener for each button that immediately know what action needs to occur when it was called. The first option makes it easier to register listeners, but requires more work in the handler to determine the source. I prefer the second method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on Eclipse recently. I am fairly new to java programming,
I'm relatively new to java (specifically swing) and have recently been making some fairly
I'm fairly new to Java. Currently trying to have the filename given in args[]
I'm fairly new to Java and have a doubt that needs to be cleared.
I'm fairly new to Java development, and people have been suggesting I use Eclipse
I'm fairly new to OO programming specifically with Java. I have a question pertaining
I am fairly new to Java. I have constructed a single JUnit test class
I'm fairly new to Java so please forgive me if this is a stupid
I'm fairly new to Android programming and to Java in general, but I have
I'm fairly new to Java (been writing other stuff for many years) and unless

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.