Skip to content

Commit

Permalink
GUI refresh fixed and GUI refresh test button added
Browse files Browse the repository at this point in the history
  • Loading branch information
danielharbor committed Feb 20, 2015
1 parent 6d11269 commit 2d43702
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Electronic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public Electronic(String name, Room room, boolean state) {

public void toggleSwitch() {
state = state ? false : true;
// GUI.refreshGUI();
((MainFrame) GUI.frame).refreshGUI();
}

public boolean getState() {
Expand Down
4 changes: 2 additions & 2 deletions GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import javax.swing.SwingUtilities;

public class GUI {

static JFrame frame;
public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new MainFrame("Smart Home");
frame = new MainFrame("Smart Home");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down
18 changes: 14 additions & 4 deletions MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MainFrame(String title) {
detailsTextArea.setEditable(false);
detailsTextArea.setLineWrap(true);

button = new JButton("Click me");
button = new JButton("Toggle");

for(Thing thing : Simulator.getThings()) {
listSelectionModel.addElement(thing);
Expand All @@ -49,13 +49,12 @@ public MainFrame(String title) {
ListenForList lForList = new ListenForList();
list.addListSelectionListener(lForList);

button.setText("Click");
ListenForButton lForButton = new ListenForButton();
button.addActionListener(lForButton);

button.setBorderPainted(false);
itemsPanel.add(list);
itemsPanel.add(scroller);
itemsPanel.add(button);

detailsPanel.add(detailsTextArea, BorderLayout.CENTER);

Expand All @@ -71,14 +70,20 @@ public void refreshGUI() {
detailsBuffer.append(s);
}
detailsTextArea.setText(detailsBuffer.toString());
if(thingBeingDisplayed instanceof Electronic) {
String btnDisplay = detailsInfo.get(2).toString().equals("ON") ? "Turn Off" : "Turn On";
button.setText(btnDisplay);
}
}

private class ListenForButton implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {

if(thingBeingDisplayed instanceof Electronic) {
((Electronic)thingBeingDisplayed).toggleSwitch();
}
}
}
}
Expand All @@ -88,6 +93,11 @@ private class ListenForList implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
if(e.getSource() == list) {
if(!(thingBeingDisplayed instanceof Electronic)) {
button.setVisible(false);
} else {
button.setVisible(true);
}
thingBeingDisplayed = list.getSelectedValue();
refreshGUI();
}
Expand Down

0 comments on commit 2d43702

Please sign in to comment.