Results 1 to 4 of 4
  1. #1
    Nych is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Help on using JRadioButton with images

    i have a class with 8 radio button and each radio button has a specific image shown when selected. now my problem is, how am i gonna call the image of the selected radio button to another window? any help will do.

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You've posted limited information to give much help...for example: define 'call the image'...if you read in the image and want to use it in multiple locations, keep a reference to that Image object, URL, or some reference to the image, which can then be used elsewhere

  3. #3
    Nych is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;

    public class ChooseToken extends JPanel implements ActionListener
    {

    private static final long serialVersionUID = 5694133531666971797L;

    static String barrowString = "Barrow";
    static String bootString = "Boot";
    static String carString = "Car";
    static String dogString = "Dog";
    static String hatString = "Hat";
    static String ironString = "Iron";
    static String shipString = "Ship";
    static String thimbleString = "Thimble";


    private static Font iF = new Font("SansSerif", Font.BOLD, 10);

    Border blackline = BorderFactory.createLineBorder(Color.black);
    ButtonGroup group = new ButtonGroup();
    JLabel picture;
    JLabel space = new JLabel("");
    static JRadioButton barrowButton, bootButton;
    static Image barrowImage, bootImage, carImage;
    ImageIcon icon1, icon2, icon3;
    static JLabel label;

    public ChooseToken()
    {
    super(new BorderLayout());

    barrowButton = new JRadioButton(barrowString, false);
    barrowButton.setMnemonic(KeyEvent.VK_B);
    barrowButton.setActionCommand(barrowString);
    barrowButton.setFont(iF);

    bootButton = new JRadioButton(bootString, false);
    bootButton.setMnemonic(KeyEvent.VK_O);
    bootButton.setActionCommand(bootString);
    bootButton.setFont(iF);

    JRadioButton carButton = new JRadioButton(carString);
    carButton.setMnemonic(KeyEvent.VK_C);
    carButton.setActionCommand(carString);
    carButton.setFont(iF);

    JRadioButton dogButton = new JRadioButton(dogString);
    dogButton.setMnemonic(KeyEvent.VK_D);
    dogButton.setActionCommand(dogString);
    dogButton.setFont(iF);

    JRadioButton hatButton = new JRadioButton(hatString);
    hatButton.setMnemonic(KeyEvent.VK_H);
    hatButton.setActionCommand(hatString);
    hatButton.setFont(iF);

    JRadioButton ironButton = new JRadioButton(ironString);
    ironButton.setMnemonic(KeyEvent.VK_I);
    ironButton.setActionCommand(ironString);
    ironButton.setFont(iF);

    JRadioButton shipButton = new JRadioButton(shipString);
    shipButton.setMnemonic(KeyEvent.VK_S);
    shipButton.setActionCommand(shipString);
    shipButton.setFont(iF);

    JRadioButton thimbleButton = new JRadioButton(thimbleString);
    thimbleButton.setMnemonic(KeyEvent.VK_T);
    thimbleButton.setActionCommand(thimbleString);
    thimbleButton.setFont(iF);

    group.add(barrowButton);
    group.add(bootButton);
    group.add(carButton);
    group.add(dogButton);
    group.add(hatButton);
    group.add(ironButton);
    group.add(shipButton);
    group.add(thimbleButton);

    barrowButton.addActionListener(this);
    bootButton.addActionListener(this);
    carButton.addActionListener(this);
    dogButton.addActionListener(this);
    hatButton.addActionListener(this);
    ironButton.addActionListener(this);
    shipButton.addActionListener(this);
    thimbleButton.addActionListener(this);

    picture = new JLabel(createImageIcon("/MonopolyToken/qm.jpg"));
    picture.setPreferredSize(new Dimension(50, 50));

    JPanel radioPanel = new JPanel(new GridLayout(4, 2));
    radioPanel.add(barrowButton);
    radioPanel.add(bootButton);
    radioPanel.add(carButton);
    radioPanel.add(dogButton);
    radioPanel.add(hatButton);
    radioPanel.add(ironButton);
    radioPanel.add(shipButton);
    radioPanel.add(thimbleButton);

    JPanel panP = new JPanel();
    panP.setLayout(new GridLayout(0, 1));
    panP.add(picture, BorderLayout.CENTER);

    JPanel panP2 = new JPanel();
    panP2.setBorder(new TitledBorder(blackline,"Choose Token:"));
    panP2.add(panP, BorderLayout.SOUTH);

    add(radioPanel, BorderLayout.NORTH);
    add(panP2, BorderLayout.SOUTH);
    setVisible(true);

    try
    {
    barrowImage = ImageIO.read(new File("C:\\Project Nych\\iAcademy\\SoftEng\\JavaMonopoly\\src\\Monopo lyToken\\Barrow.jpg"));
    bootImage = ImageIO.read(new File("C:\\Project Nych\\iAcademy\\SoftEng\\JavaMonopoly\\src\\Monopo lyToken\\Boot.jpg"));
    carImage = ImageIO.read(new File("C:\\Project Nych\\iAcademy\\SoftEng\\JavaMonopoly\\src\\Monopo lyToken\\Car.jpg"));

    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    if(barrowButton.isSelected())
    {
    icon1 = new ImageIcon(barrowImage);
    label = new JLabel(icon1);
    }
    else if(bootButton.isSelected())
    {
    icon2 = new ImageIcon(bootImage);
    label = new JLabel(icon2);
    }
    else
    {
    icon3 = new ImageIcon(carImage);
    label = new JLabel(icon3);
    }



    }

    public void actionPerformed(ActionEvent e)
    {
    picture.setIcon(createImageIcon("/MonopolyToken/"+ e.getActionCommand()+ ".jpg"));
    }

    protected static ImageIcon createImageIcon(String path)
    {
    java.net.URL imgURL = ChooseToken.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("Select Token!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JComponent newContentPane = new ChooseToken();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    createAndShowGUI();
    }
    }
    );
    }
    }





    -this is my code. what i want to do is send the selected JRadioButton image to another window. the code is for jradiobutton with image only. hope u could kindly help me.

  4. #4
    Nych is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    package JavaMonopoly;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;

    public class ChooseToken extends JPanel implements ActionListener
    {

    private static final long serialVersionUID = 5694133531666971797L;

    static String barrowString = "Barrow";
    static String bootString = "Boot";
    static String carString = "Car";
    static String dogString = "Dog";
    static String hatString = "Hat";
    static String ironString = "Iron";
    static String shipString = "Ship";
    static String thimbleString = "Thimble";


    private static Font iF = new Font("SansSerif", Font.BOLD, 10);

    Border blackline = BorderFactory.createLineBorder(Color.black);
    ButtonGroup group = new ButtonGroup();
    JLabel picture;
    JLabel space = new JLabel("");
    static JRadioButton barrowButton, bootButton;
    static JLabel label;

    public ChooseToken()
    {
    super(new BorderLayout());

    barrowButton = new JRadioButton(barrowString, false);
    barrowButton.setMnemonic(KeyEvent.VK_B);
    barrowButton.setActionCommand(barrowString);
    barrowButton.setFont(iF);

    bootButton = new JRadioButton(bootString, false);
    bootButton.setMnemonic(KeyEvent.VK_O);
    bootButton.setActionCommand(bootString);
    bootButton.setFont(iF);

    JRadioButton carButton = new JRadioButton(carString);
    carButton.setMnemonic(KeyEvent.VK_C);
    carButton.setActionCommand(carString);
    carButton.setFont(iF);

    JRadioButton dogButton = new JRadioButton(dogString);
    dogButton.setMnemonic(KeyEvent.VK_D);
    dogButton.setActionCommand(dogString);
    dogButton.setFont(iF);

    JRadioButton hatButton = new JRadioButton(hatString);
    hatButton.setMnemonic(KeyEvent.VK_H);
    hatButton.setActionCommand(hatString);
    hatButton.setFont(iF);

    JRadioButton ironButton = new JRadioButton(ironString);
    ironButton.setMnemonic(KeyEvent.VK_I);
    ironButton.setActionCommand(ironString);
    ironButton.setFont(iF);

    JRadioButton shipButton = new JRadioButton(shipString);
    shipButton.setMnemonic(KeyEvent.VK_S);
    shipButton.setActionCommand(shipString);
    shipButton.setFont(iF);

    JRadioButton thimbleButton = new JRadioButton(thimbleString);
    thimbleButton.setMnemonic(KeyEvent.VK_T);
    thimbleButton.setActionCommand(thimbleString);
    thimbleButton.setFont(iF);

    group.add(barrowButton);
    group.add(bootButton);
    group.add(carButton);
    group.add(dogButton);
    group.add(hatButton);
    group.add(ironButton);
    group.add(shipButton);
    group.add(thimbleButton);

    barrowButton.addActionListener(this);
    bootButton.addActionListener(this);
    carButton.addActionListener(this);
    dogButton.addActionListener(this);
    hatButton.addActionListener(this);
    ironButton.addActionListener(this);
    shipButton.addActionListener(this);
    thimbleButton.addActionListener(this);

    picture = new JLabel(createImageIcon("/MonopolyToken/qm.jpg"));
    picture.setPreferredSize(new Dimension(50, 50));

    JPanel radioPanel = new JPanel(new GridLayout(4, 2));
    radioPanel.add(barrowButton);
    radioPanel.add(bootButton);
    radioPanel.add(carButton);
    radioPanel.add(dogButton);
    radioPanel.add(hatButton);
    radioPanel.add(ironButton);
    radioPanel.add(shipButton);
    radioPanel.add(thimbleButton);

    JPanel panP = new JPanel();
    panP.setLayout(new GridLayout(0, 1));
    panP.add(picture, BorderLayout.CENTER);

    JPanel panP2 = new JPanel();
    panP2.setBorder(new TitledBorder(blackline,"Choose Token:"));
    panP2.add(panP, BorderLayout.SOUTH);

    add(radioPanel, BorderLayout.NORTH);
    add(panP2, BorderLayout.SOUTH);
    setVisible(true);


    }

    public void actionPerformed(ActionEvent e)
    {
    picture.setIcon(createImageIcon("/MonopolyToken/"+ e.getActionCommand()+ ".jpg"));
    }

    protected static ImageIcon createImageIcon(String path)
    {
    java.net.URL imgURL = ChooseToken.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("Select Token!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JComponent newContentPane = new ChooseToken();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    createAndShowGUI();
    }
    }
    );
    }
    }



    -sorry i mispost the code. this is the code that i was using.

Similar Threads

  1. JRadioButton
    By mcajavaprogramer in forum AWT / Swing
    Replies: 1
    Last Post: 11-05-2010, 01:31 PM
  2. JRadioButton help (see pasted code)
    By ocarabal in forum New To Java
    Replies: 4
    Last Post: 06-02-2010, 01:56 AM
  3. JRadioButton Event Handling Help???
    By CS313e in forum AWT / Swing
    Replies: 3
    Last Post: 03-27-2010, 05:35 PM
  4. BackGround JRadioButton
    By antonello12 in forum AWT / Swing
    Replies: 1
    Last Post: 10-10-2009, 06:33 AM
  5. Regarding JTree with JCheckBox and JRadioButton
    By partha_adt in forum AWT / Swing
    Replies: 0
    Last Post: 11-11-2008, 10:03 AM

Posting Permissions

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