Hi, I'm new to this forum

I'm trying to allow my menu items to work by allowing a user to press it and then display a message such as 'door locks' etc and keep a progress that the door is locked or unlocked... pic below highlighted in red


however with the check door status I want it to allow it to print the status of each door either using the JOptionpane message dialog or the printscreen command

would I be right in using boolean (true/false), if so how would I implement it for every menu item so that it keeps a check on whats open and whats closed (i'm guessing I would need to change a few things in my actionlistener)

heres my code so far

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.geom.AffineTransform;
import javax.swing.JOptionPane;



public class Project extends JFrame{
	ImageIcon icon;
	Image image;
	private JList itemList, copyList;
	private JButton copy;
	private JButton remove;
	private JButton dispatch;
	private JTextField statusField;
	JMenuBar menuBar;
	Container contentPane;
	JMenu fileMenu;
	JMenu fileLights;
	//private lights light; // variable to control the light
	JScrollPane scrollPane;
	
	
	private String itemNames[] = {"Chocolate £" + 1.99, "Toy £" + 4.99, "Bike £" + 199.99, "Laptop £" + 399.99, "Phone £" + 79.99, "Dog", "TV £" + 299.99, "Microwave £" + 89.99}; // list of items
	
	public Project(){
		
		super("Home Automation System");
		
				icon = new ImageIcon("home.jpg");
 
		JPanel panel = new JPanel()
		{
			protected void paintComponent(Graphics g)
			{
            	//  Scale image to size of component
				Dimension d = getSize();
				g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
				
				super.paintComponent(g);
			}
		};
        	
        Container c = getContentPane();// copy container
		Container r = getContentPane();// remove container
		Container d = getContentPane();// disptach container
		contentPane = getContentPane();
		c.setBackground(Color.orange);// background colour is set to orange
		
		
		setLayout(new FlowLayout());
		DefaultListModel model = new DefaultListModel();
		fileMenu = new JMenu("House Locks");// declares a filemenu named "House Locks"
		fileLights = new JMenu("Lights");
		JMenuItem lockdoor; // menu items for house locks
		JMenuItem unlockdoor;
		JMenuItem lockfrontdoor;
		JMenuItem unlockfrontdoor;
		JMenuItem lockbackdoor;
		JMenuItem unlockbackdoor;
		JMenuItem Alllightson; // menu items for lights
		JMenuItem Alllightsoff;
		JMenuItem Bedroomlighton;
		JMenuItem Bedroomlightoff;
		JMenuItem Kitchenlighton;
		JMenuItem Kitchenlightoff;
		JMenuItem Livingroomlighton;
		JMenuItem Livingroomlightoff;
		JMenuItem Doorstatus;
		JMenuItem Lightstatus;
		JLabel Price; // variable for price to be displayed
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
        setSize(400,400);
        setVisible(true);
        lockdoor = new JMenuItem("Lock all doors"); // menu items used
        lockdoor.addActionListener(new PopupActionListener());// actionlistener added to the menu item
        
        unlockdoor = new JMenuItem("Unlock all doors");
        unlockdoor.addActionListener(new PopupActionListener());
        
        lockfrontdoor = new JMenuItem("Lock front door");
        lockfrontdoor.addActionListener(new PopupActionListener());
        
        unlockfrontdoor = new JMenuItem("Unlock front door");
        unlockfrontdoor.addActionListener(new PopupActionListener());
        
        lockbackdoor = new JMenuItem("Lock back door");
        lockbackdoor.addActionListener(new PopupActionListener());
        
        unlockbackdoor = new JMenuItem("Unlock back door");
        unlockbackdoor.addActionListener(new PopupActionListener());
        
        Doorstatus = new JMenuItem("Check Door Status");
        Doorstatus.addActionListener(new PopupActionListener());
        
        Alllightson = new JMenuItem("All lights on");
        Alllightsoff = new JMenuItem("All lights off");
        Bedroomlighton = new JMenuItem("Bedroom light on");
        Bedroomlightoff = new JMenuItem("Bedroom light off");
        Kitchenlighton = new JMenuItem("Kitchen light on");
        Kitchenlightoff = new JMenuItem("Kitchen light off");
        Livingroomlighton = new JMenuItem("Living room light on");
        Livingroomlightoff = new JMenuItem("Living room light off");
        
        Lightstatus = new JMenuItem("Check Light Status");
        menuBar.add(fileMenu);
        menuBar.add(fileLights);
        fileMenu.add(lockdoor); // adding the menu items
        fileMenu.add(unlockdoor);
        fileMenu.add(lockfrontdoor);
        fileMenu.add(unlockfrontdoor);
        fileMenu.add(lockbackdoor);
       	fileMenu.add(unlockbackdoor);
       	fileMenu.add(Doorstatus);
       	fileLights.add(Alllightson);
       	fileLights.add(Alllightsoff);
       	fileLights.add(Bedroomlighton);
       	fileLights.add(Bedroomlightoff);
       	fileLights.add(Kitchenlighton);
       	fileLights.add(Kitchenlightoff);
       	fileLights.add(Livingroomlighton);
       	fileLights.add(Livingroomlightoff);
       	fileLights.add(Lightstatus);
		
		panel.setOpaque( false );// displays the image for logo
		panel.setPreferredSize( new Dimension(600, 100) );
		scrollPane = new JScrollPane( panel );
		getContentPane().add( scrollPane );
		
		itemList = new JList(itemNames);
		itemList.setVisibleRowCount( 5);
		itemList.setFixedCellHeight( 30 );// size of cell height
		itemList.setFixedCellWidth( 200 );
		itemList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
		add(new JScrollPane ( itemList ) );
		
		copy = new JButton (" Copy Item >>"); // button to copy the items
		copy.addActionListener (new CopyListener());// action listener added
		c.add(copy);
		
		remove = new JButton (" << Remove Item");
		remove.addActionListener (new RemoveListener());
		r.add(remove);
		
		
		copyList = new JList(model);
		copyList.setVisibleRowCount( 5);// amount of visible rows for the shopping list
		copyList.setFixedCellHeight( 30 );// cell height
		copyList.setFixedCellWidth( 200 );// cell width
		copyList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
		c.add(new JScrollPane ( copyList ) );
		
		//price = new itemNames ();
		//add(price);
		
		dispatch = new JButton (" << Dispatch Item >> ");// dispatch button
		Price = new JLabel ("Price £");// label that displayed the word "Price £"
		statusField = new JTextField(10);
		dispatch.addActionListener (new DispatchListener());
		d.add(dispatch);
		d.add(Price);
		d.add(statusField);
		
	   
		
		setSize( 800, 400);
		setVisible(true);
		
		
	}
	
	class CopyListener implements ActionListener{ // listener to copy the item once the item button is pressed
		
		public void actionPerformed (ActionEvent e) {
			DefaultListModel temp = (DefaultListModel) copyList.getModel();
			Object[] values = itemList.getSelectedValues();
			updateTotalPrice();
			
		/*	for(Object obj : values) {
				temp.addElement(obj);	
			}*/
			
			for(int i=0; i<values.length;i++){
				temp.addElement(values[i]);
			}
		}
	}
	
	class RemoveListener implements ActionListener{ // listener to remove the item once the remove button is pressed
		public void actionPerformed (ActionEvent e){
			DefaultListModel temp = (DefaultListModel) copyList.getModel();
			Object[] values = copyList.getSelectedValues();
			
		/*	for(Object obj : values) {
				temp.addElement(obj);	
			}*/
			
			for(int i=0; i<values.length;i++){
				temp.removeElement(values[i]);
			}
		}
	}
	
	class DispatchListener implements ActionListener{ // listener to remove the item once the remove button is pressed
		public void actionPerformed (ActionEvent e){
			DefaultListModel temp = (DefaultListModel) copyList.getModel();
			//Object[] values = copyList.getSelectedValues();
			temp.clear();
			JOptionPane.showMessageDialog(null,"Items have been dispatched.");
			
			
			
		}
	}
	
	class PopupActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
  	
            JOptionPane.showMessageDialog(null,"Selected : "  +  e.getActionCommand());// action listener message that displays what menu item was selected
        }
  

    }

	public void updateTotalPrice() {
         DefaultListModel temp = (DefaultListModel) copyList.getModel();
         Double poundValue = 0.0;
         for (int i = 0; i < temp.getSize(); i++) {
         String valueString = temp.get(i).toString();
         poundValue += Double.parseDouble(valueString.substring(valueString.indexOf('£')+1));
        }
      statusField.setText("" + poundValue);
    }
   
    public class Light{
   	
   	private String LightName;
   	private boolean on, off;
    }
   
   
   
   
   
	public static void main (String args []){
		Project lister = new Project();
		lister.addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e ){
				System.exit(0);
			}
		});
		
		
	}
}

Thanks

You'll need a mechanism to associate the Light or Door object with the action for a given menu item. One way you might do it is with an action listener that takes a reference to the object you want to operate on, such as

class LightOnAction implements ActionListener{
    Light light;
    public LightOnAction(Light light){
        this.light = light;
    }

    public void actionPerformed(ActionEvent e) {
        light.on = true;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.