I am adding a heater JButton and a heater JSlider to each Tab that I am creating, the heater JButton to turn it off and on and the heater JSlider to choose the temp.
The JSlider status is preset to false, so when I click the heater JButton to turn the heater on,
I need it to set the status of the JSlider to TRUE, how can I access the heater JSlider in the HEATER BUTTON class? and if not what other way is there to do this??.
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 roomId = rooms.get(x).getId();
tabbedPane.addTab(roomName + " Room " + roomId, tmpPanel);
for(int i = 0; i < rooms.get(x).roomLights.size(); i++){
int lightId = rooms.get(x).roomLights.get(i).getId();
JButton lights = new JButton("Light" + lightId);
lights.setBackground(Color.red);
lights.addActionListener(new LightButton(roomId, lightId));
((JPanel) tabbedPane.getComponentAt(x)).add(lights);
}
JButton heater = new JButton("Heater");
heater.setBackground(Color.red);
heater.addActionListener(new HeaterButton(roomId));
JSlider heaterSlider = new JSlider(68, 73);
heaterSlider.setPaintTicks(true);
heaterSlider.setPaintLabels(true);
heaterSlider.setMajorTickSpacing(1);
heaterSlider.addChangeListener(new HeaterSlider(roomId));
heaterSlider.setEnabled(false);
((JPanel) tabbedPane.getComponentAt(x)).add(heater);
((JPanel) tabbedPane.getComponentAt(x)).add(heaterSlider);
}
master.add(tabbedPane, BorderLayout.CENTER);
master.setSize(800, 600);
content.add(tabbedPane);
master.setVisible(true);
}
HEATERBUTTON CLASS
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HeaterButton implements ActionListener{
int roomNumber;
public HeaterButton(int room){
roomNumber = room;
}
public void actionPerformed(ActionEvent e){
ArrayList<Rooms> rooms;
rooms = Building.getRoomList();
if(rooms.get(roomNumber - 1).roomHeater.getHeaterStatus() == true){
rooms.get(roomNumber - 1).roomHeater.setHeaterOff();
((JButton)e.getSource()).setBackground(Color.red);
}else{
rooms.get(roomNumber - 1).roomHeater.setHeaterOn();
((JButton)e.getSource()).setBackground(Color.green);
}
}
}
Create the slider first and then pass it to the button.
Change:
heater.addActionListener(new HeaterButton(roomId));
To:
heater.addActionListener(new HeaterButton(roomId, slider));