Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PanelSwitcherView.java creates two JPanels and a JButton and contains a method d

ID: 3805325 • Letter: P

Question

PanelSwitcherView.java creates two JPanels and a JButton and contains a method displayPanel to display one of the two panels. However, the JButton doesn't do anything because it is not registered with any event handler.

PanelSwitcherModel.java stores a 1 or a 2, which is intended to indicate which panel should be displayed.

PanelSwitcher.java contains a main method that creates PanelSwitcherView and PanelSwitcherModel objects. However, it cannot create a PanelSwitcherController object because you haven't written that code yet.

You need to write PanelSwitcherController.java and any additional code in the other classes so that a PanelSwitcherController object will handle events from the JButton by updating the model and the view. [Hint: This class should implement ActionListener.] If you find the carefully calibrated color coordination too disturbing, you may change the background colors of the JLabels.

PanelSwitcher.java

******************************************************************

import javax.swing.*;

/**
* You need to add an event handler that will switch the JPanels in the view
* when the user clicks the button.
*
* @author Tom Bylander
*/
public class PanelSwitcher {

public static void main(String[] args) {
PanelSwitcherModel model = new PanelSwitcherModel();
PanelSwitcherView view = new PanelSwitcherView();
   PanelSwitcherController controller = new PanelSwitcherController(view, model);
  
view.register(controller);

view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setSize(400,300);
view.setVisible(true);
}

}

PanelSwitcherModel.java

***************************************************************************

/**
* This very complicated class stores a 1 or a 2. It even allows you to switch
* values.
*
* @author Tom Bylander
*/
public class PanelSwitcherModel {
/**
* Either 1 or 2 is stored in panel.
*/
private int panel;

/**
* After weeks of meetings, it was decided to initialize panel to 1.
*/
public PanelSwitcherModel() {
panel = 1;
}

/**
* @return the value of panel
*/
public int whichPanel() {
return panel;
}

/**
* Switch panel from 1 to 2 or from 2 to 1, but not from any other number
* any other other number.
*/
public void switchPanel() {
if (panel == 1)
panel = 2;
else
panel = 1;
}
}

PanelSwitcherView.java

********************************************************************8

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

/**
* All this is missing here is a method to register the event handler with
* the button. You might need a couple changes so that the method can access
* the button.
*
* @author Tom Bylander
*/
public class PanelSwitcherView extends JFrame {
private JPanel panel1, panel2;

public PanelSwitcherView() {
super("Panel Switching Test");

Font font = new Font("SansSerif", Font.BOLD, 30);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2, 5, 5));

JLabel label = new JLabel("four", JLabel.CENTER);
label.setBackground(Color.MAGENTA);
label.setFont(font);
label.setOpaque(true);
panel1.add(label);

label = new JLabel("JLabels", JLabel.CENTER);
label.setBackground(Color.BLUE);
label.setFont(font);
label.setOpaque(true);
panel1.add(label);

label = new JLabel("are", JLabel.CENTER);
panel1.add(label);
label.setBackground(Color.CYAN);
label.setFont(font);
label.setOpaque(true);

label = new JLabel("shown", JLabel.CENTER);
panel1.add(label);
label.setBackground(Color.GREEN);
label.setFont(font);
label.setOpaque(true);

font = new Font("Serif", Font.ITALIC, 30);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());

label = new JLabel("five", JLabel.CENTER);
label.setBackground(Color.GREEN);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.NORTH);

label = new JLabel("JLabels", JLabel.CENTER);
label.setBackground(Color.YELLOW);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.WEST);

label = new JLabel("for", JLabel.CENTER);
label.setBackground(Color.ORANGE);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.CENTER);

label = new JLabel("this", JLabel.CENTER);
label.setBackground(Color.PINK);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.EAST);

label = new JLabel("panel", JLabel.CENTER);
label.setBackground(Color.RED);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.SOUTH);

font = new Font("Monospaced", Font.BOLD + Font.ITALIC, 30);
JButton button = new JButton("Switch Panels");
button.setFont(font);
add(button, BorderLayout.NORTH);

add(panel1, BorderLayout.CENTER);
}

/**
* This seems to be a way to switch JPanels. Let me know of any bugs
* or more elegant ways of doing the same task.
*
* @param whichPanel Should panel1 or panel2 be displayed?
*/
public void displayPanel(int whichPanel) {
remove(panel1); // doesn't seem to mind trying to remove
remove(panel2); // components that are not displayed
if (whichPanel == 1) {
System.out.println("Should display panel1");
add(panel1, BorderLayout.CENTER);
} else {
System.out.println("Should display panel2");
add(panel2, BorderLayout.CENTER);
}
validate();
repaint();
}
}

Explanation / Answer

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

public class PanelSwitcherView extends JFrame {
private JPanel panel1, panel2;

public class PanelSwitcher {

public static void main(String[] args) {
PanelSwitcherModel model = new PanelSwitcherModel();
PanelSwitcherView view = new PanelSwitcherView();
   PanelSwitcherController controller = new PanelSwitcherController(view, model);
  
view.register(controller);

view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setSize(400,300);
view.setVisible(true);

public PanelSwitcherView() {
super("Panel Switching Test");

Font font = new Font("SansSerif", Font.BOLD, 30);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2, 5, 5));

JLabel label = new JLabel("four", JLabel.CENTER);
label.setBackground(Color.MAGENTA);
label.setFont(font);
label.setOpaque(true);
panel1.add(label);

label = new JLabel("JLabels", JLabel.CENTER);
label.setBackground(Color.BLUE);
label.setFont(font);
label.setOpaque(true);
panel1.add(label);

label = new JLabel("are", JLabel.CENTER);
panel1.add(label);
label.setBackground(Color.CYAN);
label.setFont(font);
label.setOpaque(true);

label = new JLabel("shown", JLabel.CENTER);
panel1.add(label);
label.setBackground(Color.GREEN);
label.setFont(font);
label.setOpaque(true);

font = new Font("Serif", Font.ITALIC, 30);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());

label = new JLabel("five", JLabel.CENTER);
label.setBackground(Color.GREEN);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.NORTH);

label = new JLabel("JLabels", JLabel.CENTER);
label.setBackground(Color.YELLOW);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.WEST);

label = new JLabel("for", JLabel.CENTER);
label.setBackground(Color.ORANGE);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.CENTER);

label = new JLabel("this", JLabel.CENTER);
label.setBackground(Color.PINK);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.EAST);

label = new JLabel("panel", JLabel.CENTER);
label.setBackground(Color.RED);
label.setFont(font);
label.setOpaque(true);
panel2.add(label, BorderLayout.SOUTH);

font = new Font("Monospaced", Font.BOLD + Font.ITALIC, 30);
JButton button = new JButton("Switch Panels");
button.setFont(font);
add(button, BorderLayout.NORTH);

add(panel1, BorderLayout.CENTER);
}

   public void displayPanel(int whichPanel) {
remove(panel1); // doesn't seem to mind trying to remove
remove(panel2); // components that are not displayed
if (whichPanel == 1) {
System.out.println("Should display panel1");
add(panel1, BorderLayout.CENTER);
} else {
System.out.println("Should display panel2");
add(panel2, BorderLayout.CENTER);
}
validate();
repaint();
}
}