i am creating a program for hotel management,i want to get the source from JButton and JCheckBox at the same time,how do i do with actionListener?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class HotelManagementGUI extends JFrame implements ActionListener
{static final String[] rommcouplesea={"1","2","3","4","5","6","7","8","9","10"};

static final String[] roomcoupleroad={"1","2","3","4","5","6","7","8","9","10"};

static final String[] roomonesea={"1","2","3","4","5","6","7","8","9","10"};

static final String[] roomoneroad={"1","2","3","4","5","6","7","8","9","10"};
static final String[] roomtwoplusone={"1","2","3","4","5","6","7","8","9","10"};
    public HotelManagementGUI()
      {super("Hotel management");
        setSize(350,250);
        setVisible(true);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel name=new JLabel("Name Surname");
     JTextField inname=new JTextField(7);
JLabel nrdays=new JLabel("how long are you staying");
JTextField indays=new JTextField(7);
JPanel namePanel=new JPanel();
JPanel nrditePanel=new JPanel();
JRadioButton room1=new JRadioButton("Couple room looking the sea");
JRadioButton room2=new JRadioButton("One person room looking the road");
JRadioButton room3=new JRadioButton("One person room looking the sea");
JRadioButton room4=new JRadioButton("Couple room looking the road");
JRadioButton room5=new JRadioButton("2+1 room");
JTextArea v=new JTextArea("Select meals");
JCheckBox breakfast=new JCheckBox("Breakfast");
JCheckBox lunch=new JCheckBox("Lunch");
JCheckBox dinner=new JCheckBox("Dinner");
JCheckBox meal=new JCheckBox("3 meals");
JPanel mealsPanel=new JPanel();
mealsPanel.setLayout(new BoxLayout(mealsPanel,BoxLayout.X_AXIS));
mealsPanel.add(v);
mealstPanel.add(breakfast);
mealstPanel.add(lunch);
mealsPanel.add(dinner);
mealsPanel.add(meal);



JButton save= new JButton("Save");
JPanel roomPanel=new JPanel();
roomPanel.setLayout(new BoxLayout(roomPanel,BoxLayout.Y_AXIS));
roomPanel.add(room1);
roomPanel.add(room2);
roomPanel.add(room3);
roomPanel.add(room4);
roomPanel.add(room5);

Container content=getContentPane();
FlowLayout lay =new FlowLayout(FlowLayout.LEFT);
content.setLayout(lay);
innrdite.addActionListener(this);
room1.addActionListener(this);
room2.addActionListener(this);
room3.addActionListener(this);
room4.addActionListener(this);
room5.addActionListener(this);
breakfast.addActionListener(this);
lunch.addActionListener(this);
dinner.addActionListener(this);
meal.addActionListener(this);
namePanel.add(name);
namePanel.add(inname);
nrdaysPanel.add(nrdays);
nrdaysPanel.add(indays);
 
content.add(namePanel);
content.add(nrdaysePanel);
content.add(roomPanel);
content.add(mealsPanel);
content.add(save);
save.addActionListener(this);
setContentPane(content);}
    
    public void actionPerformed(ActionEvent event)
    {
    	if(event.getSource()==room1 && event.getSource==breakfast)
    	{}
    	
    	
    }


public static void main (String [] args){
	HotelManagementGUI manage=new HotelManagementGUI();
	
}








	


}

please answer as soon as possible.thanks.

Hi,

public void actionPerformed(ActionEvent event)
    {
    	if(event.getSource()==room1 && event.getSource==breakfast)
    	{
    	    	// never reach this part of code
    	}
    	
    	
    }

the event is triggered only by one component, not by 2, in the same time, I don't know what you understand by "source from JButton and JCheckBox", but event.getSource() returns the object on which the event occurred, JButton or JCheckBox have no "source", they are sources for the event.

You probably need to get the selected state from the JCheckBox when a button is pressed. In that case:

public void actionPerformed(ActionEvent event)
    {
    	if(event.getSource()==room1) // if button room1 is pressed
    	{
    	    	boolean breakfastSelected = breakfast.isSelected(); // get state of check box
                // do more ...
    	}
    	
    	
    }

GL

This article has been dead for over six months. Start a new discussion instead.