One JList/JCheckBox combination not working

26 views Asked by At

I am doing an exercise in a book where you have to test event handling. I have a JList with 3 font choices (Serif, SansSerif, and Monospaced) and two checkboxes for Bold and Italic. I have a JLabel that displays the user's choice. For some crazy reason, nothing shows up on the JLabel when I choose Monospaced from the JList and both Bold and Italic from the JCheckBoxes. I need another set of eyes to look at it because I can't find anything. The code is posted below:

import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Events extends JFrame {
    private JLabel insLabel;
    private JLabel fontLabel;
    private JLabel mouseLabel;
    private JLabel resultLabel;
    private JList fontList;
    private static final String[] fontNames = {"Serif", "SansSerif", "Monospaced"};
    private JCheckBox boldCb;
    private JCheckBox italicCb;
    private JButton submitButton;
    private JPanel middlePanel;
    private JPanel bottomPanel;
    private String fontName;
    private String fontStyle;
    
    public Events() {
        super("Demonstrating Events");
        setLayout(new BorderLayout());
        insLabel = new JLabel("Please feel free to click anywhere or press any key.");
        fontLabel = new JLabel("Choose a font:");
        fontList = new JList(fontNames);
        fontList.setVisibleRowCount(3);
        fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(new JScrollPane(fontList));
        boldCb = new JCheckBox("Bold");
        italicCb = new JCheckBox("Italic");
        submitButton = new JButton("Submit");
        mouseLabel = new JLabel("");
        resultLabel = new JLabel("");
        middlePanel = new JPanel();
        middlePanel.setLayout(new FlowLayout());
        bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        add(insLabel, BorderLayout.NORTH);
        middlePanel.add(fontLabel);
        middlePanel.add(fontList);
        middlePanel.add(boldCb);
        middlePanel.add(italicCb);
        middlePanel.add(submitButton);
        add(middlePanel, BorderLayout.CENTER);
        bottomPanel.add(mouseLabel);
        bottomPanel.add(resultLabel);
        add(bottomPanel, BorderLayout.SOUTH);
        
        // set up event handling
        
        fontList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                fontName = fontNames[fontList.getSelectedIndex()];
            }
        });
        
        CheckBoxHandler handler = new CheckBoxHandler();
        boldCb.addItemListener(handler);
        italicCb.addItemListener(handler);
        
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(fontStyle == "both") {
                    resultLabel.setFont(new Font(fontName, Font.BOLD + Font.ITALIC, 18));
                    resultLabel.setText("You chose " + fontName + " bold and italic");
                } else if(fontStyle == "bold") {
                    resultLabel.setFont(new Font(fontName, Font.BOLD, 18));
                    resultLabel.setText("You chose " + fontName + " bold");
                } else if(fontStyle == "italic") {
                    resultLabel.setFont(new Font(fontName, Font.ITALIC, 18));
                    resultLabel.setText("You chose " + fontName + " italic");
                } else {
                    resultLabel.setFont(new Font(fontName, Font.PLAIN, 18));
                    resultLabel.setText("You chose " + fontName + " plain");
                }
            }
        });
        
        addMouseListener(new MouseClickHandler());
        addMouseMotionListener(new MouseMotionHandler());
        addKeyListener(new KeyHandler());
    } // end constructor
    
    private class CheckBoxHandler implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            if(boldCb.isSelected() && italicCb.isSelected())
                fontStyle = "both";
            else if(boldCb.isSelected())
                fontStyle = "bold";
            else if (italicCb.isSelected())
                fontStyle = "italic";
            else
                fontStyle = "plain";
        }
    }
    
    private class MouseClickHandler extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            int xPos = e.getX();
            int yPos = e.getY();
            mouseLabel.setText("Mouse clicked at (" + xPos + ", " + yPos + ")");
        }
    }
    
    private class MouseMotionHandler extends MouseMotionAdapter {
        public void mouseMoved(MouseEvent e) {
            int xPos = e.getX();
            int yPos = e.getY();
            mouseLabel.setText("Mouse moved at (" + xPos + ", " + yPos + ")");
        }
    }
    
    private class KeyHandler extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            resultLabel.setText("You pressed " + KeyEvent.getKeyText(e.getKeyCode()));
        }
    }
}
    
    

0

There are 0 answers