Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JRadioButton Won't Respond

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JRadioButton Won't Respond

    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]));
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: JRadioButton Won't Respond

    Use printlns to help determine where things go unexpected

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JRadioButton Won't Respond

    The problem I'm having is that I'm not quite sure where things are going unexpectedly to put printlns around them.

  4. #4
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: JRadioButton Won't Respond

    Your Radio buttons are responding, but you have not programmed them to do anything. The only programming you have of them is in the constructor, which gets called once and never again. You need to move the programming of them into the ActionListener Method.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: JRadioButton Won't Respond

    Quote Originally Posted by SynergY View Post
    The problem I'm having is that I'm not quite sure where things are going unexpectedly to put printlns around them.
    So start at the beginning. You know where the program should start. You know what it should do first. You know what it should do next. Add printlns to verify that things are happening in the right order. Eventually something unexpected will happen, or nothing will happen when it should, ...something unexpected. This will help you determine what IS happening, so you can change things to happen a different way

Similar Threads

  1. Servlet to respond many requests
    By CarlosM87 in forum Java Servlet
    Replies: 3
    Last Post: January 31st, 2012, 04:18 AM
  2. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM
  3. Get Box to Respond to JScrollPane
    By bgroenks96 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 25th, 2011, 10:18 AM
  4. [SOLVED] Platform game player doesn't respond to keyboard
    By jesamjasam in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 09:44 AM
  5. Buttons don't work on using JRadioButton
    By neo_2010 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 11th, 2009, 11:37 AM