Results 1 to 3 of 3
  1. #1
    akahafe is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Image display for individual radio button

    Hi I have the following code that currently displays an image when a radio button is pressed. Could someone kindly show me how a seperate image can be created when any other button is pressed? Meaning, if there are 6 radio buttons then there would be 6 image panels. Thank you in adavnace
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class PlaceFound extends JPanel
                                 implements ActionListener {
        static String Place1String = "b";
        static String Place2String = "a";
        static String Place3String = "c";
        static String Place4String = "d";
        static String Place5String = "e";
       
            private JLabel lblHeader;
            private JButton btnNext;
            private JButton btnBack;
            private ImagePanel pnImg1, pnImg2, pnImg3, pnImg4, pnImg5, pnImg6, pnImg7, pnImg8, pnImg9, pnImg10;
            private JPanel pn1, pn2, pn3, pn4, pn5, pn6, pn7, pn8, pn9, pn10;
     
        JLabel picture;
       
     
        public PlaceFound() {
            super(new BorderLayout());
     
            JRadioButton Place1Button = new JRadioButton("Select");   
            Place1Button.setActionCommand(Place1String);
            Place1Button.setSelected(true);
     
            JRadioButton Place2Button = new JRadioButton("Select");
        
            Place2Button.setActionCommand(Place2String);
     
            JRadioButton Place3Button = new JRadioButton("Select");    
            Place3Button.setActionCommand(Place3String);
     
            JRadioButton Place4Button = new JRadioButton("Select");     
            Place4Button.setActionCommand(Place4String);
           
            JRadioButton Place5Button = new JRadioButton("Select");
            Place5Button.setActionCommand(Place5String);
     
            ButtonGroup group = new ButtonGroup();
            group.add(Place1Button);
            group.add(Place2Button);
            group.add(Place3Button);
            group.add(Place4Button);
            group.add(Place5Button);
           
           
            Place1Button.addActionListener(this);
            Place2Button.addActionListener(this);
            Place3Button.addActionListener(this);
            Place4Button.addActionListener(this);
            Place5Button.addActionListener(this);
     
            picture = new JLabel(createImageIcon(""
                                                 + Place1String
                                                 + ".jpg"));
     
            picture.setPreferredSize(new Dimension(177, 122));
     
     
            //Put the radio buttons in a column in a panel.
            JPanel radioPanel = new JPanel (new GridLayout(3,5,50, 20));
            radioPanel.add(Place1Button);
            radioPanel.add(Place2Button);
            radioPanel.add(Place3Button);
            radioPanel.add(Place4Button);
            radioPanel.add(Place5Button);
          
     
            add(radioPanel, BorderLayout.LINE_START);
            add(picture, BorderLayout.CENTER);
            setBorder(BorderFactory.createEmptyBorder(100,20,20,20));
        }
     
        /** Listens to the radio buttons. */
        public void actionPerformed(ActionEvent e) {
            picture.setIcon(createImageIcon(""
                                            + e.getActionCommand()
                                            + ".jpg"));
        }
     
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = PlaceFound.class.getResource(path);
            if (imgURL != null){
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
     
        private static void createAndShowGUI() {
           
     
            JFrame frame = new JFrame("Location search options");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JComponent newContentPane = new PlaceFound();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this appliPlace2ion's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Last edited by Norm; 12-13-2011 at 04:04 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image display for individual radio button

    how a seperate image can be created
    Can you explain how you want to "create" an image?
    What is it that you are trying to do?
    Are you trying to create the filename of an image?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Image display for individual radio button

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Radio Button
    By S-NESH in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:03 PM
  2. Replies: 0
    Last Post: 12-30-2010, 03:55 AM
  3. Customized Radio Button
    By Javified in forum AWT / Swing
    Replies: 6
    Last Post: 12-13-2010, 07:06 AM
  4. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •