Hello!
So my problem is that my radio buttons are not responding when I click them. I made sure there was an ActionListener, and I think my if statements are fine. So I'm pretty lost as to why they aren't doing their job.
In case my code is vague, I want the radio buttons to change the color of the font when it is clicked.
Thank you!
package UserInterface; import BusinessModel.Controller; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.*; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class ProductPanel extends JPanel implements ActionListener, ListSelectionListener { private Controller ctrl = new Controller(); private JButton btnInventory; private JButton btnAdd; private JButton btnReturn; private JList lstInventory; private JList lstCart; private JTextArea txtDetails; private JScrollPane scroll; private DefaultListModel model; private DefaultListModel modelCart; private JRadioButton black, blue, red; ProductPanel() { // Create Graphical Objects ButtonGroup buttonGroup = new ButtonGroup(); black = new JRadioButton("Black"); buttonGroup.add(black); black.setSelected(true); blue = new JRadioButton("Blue"); buttonGroup.add(blue); red = new JRadioButton("Red"); buttonGroup.add(red); btnInventory = new JButton ("Inventory"); if(black.isSelected()) btnInventory.setForeground(Color.BLACK); else if(blue.isSelected()) btnInventory.setForeground(Color.BLUE); else if (red.isSelected()) btnInventory.setForeground(Color.RED); btnInventory.setFont(new Font("Arial",Font.BOLD,14)); btnInventory.setToolTipText("View Inventory"); btnAdd = new JButton ("Add"); if(black.isSelected()) btnAdd.setForeground(Color.BLACK); else if(blue.isSelected()) btnAdd.setForeground(Color.BLUE); else if(red.isSelected()) btnAdd.setForeground(Color.RED); btnAdd.setFont(new Font("Arial",Font.BOLD,14)); btnAdd.setToolTipText("Add item to cart"); btnReturn = new JButton ("Return"); if(black.isSelected()) btnReturn.setForeground(Color.BLACK); else if(blue.isSelected()) btnReturn.setForeground(Color.BLUE); else if(red.isSelected()) btnReturn.setForeground(Color.RED); btnReturn.setFont(new Font("Arial",Font.BOLD,14)); btnReturn.setToolTipText("Remove item from cart"); // Product List model = new DefaultListModel(); lstInventory = new JList(model); lstInventory.setPreferredSize(new Dimension(300,200)); lstInventory.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // Display details txtDetails = new JTextArea(14,30); txtDetails.setFont(new Font("Arial",Font.PLAIN,12)); txtDetails.setEditable(false); scroll = new JScrollPane(txtDetails); scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // Labels JPanel cartL = new JPanel(); JLabel cartLabel = new JLabel ("Cart"); cartL.add(cartLabel = new JLabel("User's Cart")); JPanel colorL = new JPanel(); JLabel colorLabel = new JLabel ("Color"); colorL.add(colorLabel = new JLabel("Choose Font Color")); // Display Cart modelCart = new DefaultListModel(); lstCart = new JList(modelCart); lstCart.setPreferredSize(new Dimension(495,200)); lstCart.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // Register event listeners black.addActionListener(this); blue.addActionListener(this); red.addActionListener(this); btnInventory.addActionListener(this); btnAdd.addActionListener(this); btnReturn.addActionListener(this); lstInventory.addListSelectionListener(this); lstCart.addListSelectionListener(this); // Button Panel JPanel btnpanel = new JPanel(); btnpanel.setLayout(new GridLayout(6,1)); btnpanel.add(btnInventory); btnpanel.add(btnAdd); btnpanel.add(btnReturn); btnpanel.add(colorL); btnpanel.add(black); btnpanel.add(blue); btnpanel.add(red); // Add objects to panel add(btnpanel); add(lstInventory); add(scroll); add(cartL); add(lstCart); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == btnInventory) { model.clear(); ArrayList <String> myList = ctrl.listContents(); for (String s : myList) { model.addElement(s); } } else if (source == btnAdd) { if(lstInventory.isSelectionEmpty()) JOptionPane.showMessageDialog(null, "No Product Selected!"); else { String cartID = lstInventory.getSelectedValue().toString(); String[] fields = cartID.split(","); ctrl.addToCart(fields[0]); modelCart.clear(); ArrayList <String> myCart = ctrl.listCart(); for (String s : myCart) { modelCart.addElement(s); } } } else if (source == btnReturn) { if(lstCart.isSelectionEmpty()) JOptionPane.showMessageDialog(null, "No Product Selected!"); else { String cartID = lstCart.getSelectedValue().toString(); String[] fields = cartID.split(","); ctrl.removeFromCart(fields[0]); modelCart.clear(); ArrayList <String> myCart = ctrl.listCart(); for(String s : myCart) { modelCart.addElement(s); } } } } public void valueChanged(ListSelectionEvent e) { //Read list and get product key String ID = lstInventory.getSelectedValue().toString(); String[] fields = ID.split(","); txtDetails.setText(ctrl.getProductInfo(fields[0])); } }