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 4 of 4

Thread: set specific location for radio buttons

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question set specific location for radio buttons

    Hi..
    I would like to know how to set a radio button to a specific point on the panel correctly..
    So what i'm trying to do is to load an image as background image (a simple map), and put radio buttons on it..
    The result would roughly looks like the image i attached.. (i give a red line border because it's quite hard to see the radio buttons)
    map.jpg

    This is what i've done so far..
    (mostly taken from internet)

    The first code is to load an image into the panel..
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    import java.applet.Applet;
     
    /**
     * This class demonstrates how to load an Image from an external file
     */
    public class LoadImageApp extends Component
    {
     
        BufferedImage img;
     
        static radioButtonGenerator radButtonGen = new radioButtonGenerator();
     
        public void paint(Graphics g)
        {
     
        	g.drawImage(img, 0, 0, 757, 392, this); // draw the image
        }
     
        public LoadImageApp()
        {
           try
           {
               img = ImageIO.read(new File("Singapore.png"));
           }
           catch (IOException e)
           {
           }
     
        }
     
        public Dimension getPreferredSize()
        {
            if (img == null)
            {
                 return new Dimension(100,100);
            }
            else
            {
               //return new Dimension(img.getWidth(null), img.getHeight(null));
            	//return new Dimension(800, 600);
            	return new Dimension(757, 392);
            }
        }
     
        public static void main(String[] args)
        {
     
            JFrame f = new JFrame("Load Image Sample");
            radButtonGen.init();
     
            f.addWindowListener(new WindowAdapter()
            {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
            });
     
            f.add(new LoadImageApp());
            f.add("Center", radButtonGen);
            f.pack();
            f.setVisible(true);
        }
    }
    and the second code is to create radio buttons on the location i prefer..
    this code is generated by a program called javagui, where i just need to drag and drop the radio button and generate the code out of it..
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;
     
    public class radioButtonGenerator extends Applet
    {
        ButtonGroup cbg;
        JRadioButton radio_1;
        JRadioButton radio_2;
        JRadioButton radio_3;
        JRadioButton radio_4;
     
        public void init()
        {
            frameTestLayout customLayout = new frameTestLayout();
     
            setFont(new Font("Helvetica", Font.PLAIN, 12));
            setLayout(customLayout);
     
            cbg = new ButtonGroup();
            radio_1 = new JRadioButton("radio_1", false);
            cbg.add(radio_1);
            add(radio_1);
     
            radio_2 = new JRadioButton("radio_2", false);
            cbg.add(radio_2);
            add(radio_2);
     
            radio_3 = new JRadioButton("radio_3", false);
            cbg.add(radio_3);
            add(radio_3);
     
            radio_4 = new JRadioButton("radio_4", false);
            cbg.add(radio_4);
            add(radio_4);
     
            setSize(getPreferredSize());
     
        }
     
        /*
        public static void main(String args[])
        {
            radioButtonGenerator applet = new radioButtonGenerator();
            Frame window = new Frame("frameTest");
     
            window.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
     
            applet.init();
            window.add("Center", applet);
            window.pack();
            window.setVisible(true);
        }
        */
    }
     
    class frameTestLayout implements LayoutManager {
     
        public frameTestLayout()
        {
     
        }
     
        public void addLayoutComponent(String name, Component comp)
        {
     
        }
     
        public void removeLayoutComponent(Component comp)
        {
     
        }
     
     
        public Dimension preferredLayoutSize(Container parent)
        {
            Dimension dim = new Dimension(0, 0);
     
            Insets insets = parent.getInsets();
            //dim.width = 320 + insets.left + insets.right;
            //dim.height = 240 + insets.top + insets.bottom;
            dim.width = 757 + insets.left + insets.right;
            dim.height = 392 + insets.top + insets.bottom;
     
            return dim;
        }
     
     
        public Dimension minimumLayoutSize(Container parent)
        {
            Dimension dim = new Dimension(0, 0);
            return dim;
        }
     
     
        public void layoutContainer(Container parent)
        {
            Insets insets = parent.getInsets();
     
            Component c;
            c = parent.getComponent(0);
            if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+0,16,16);}
     
            c = parent.getComponent(1);
            if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+208,16,16);}
     
            c = parent.getComponent(2);
            if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+0,16,16);}
     
            c = parent.getComponent(3);
            if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+216,16,16);}
     
        }
    }

    but it give me this result instead..
    result.jpg

    i wonder what is wrong with the code?
    thank you..
    Last edited by nosxlimit; June 17th, 2012 at 05:22 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: set specific location for radio buttons

    You want some advice? Ditch the gui builder. That frameTestLayout (which should be FrameTestLayout) seems a little wonky. Why don't you just use a null layout and absolute positioning?

    It's almost always a better idea to use a layout manager, but in your case a null layout might indeed be the way to go.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: set specific location for radio buttons

    Quote Originally Posted by KevinWorkman View Post
    You want some advice? Ditch the gui builder. That frameTestLayout (which should be FrameTestLayout) seems a little wonky. Why don't you just use a null layout and absolute positioning?

    It's almost always a better idea to use a layout manager, but in your case a null layout might indeed be the way to go.
    thanks for the reply..

    i was thinking the same too, by not using the gui builder..
    but i don't know how to set the radiobutton onto a specific location..
    something like based on coordinates, for example, like a map for tourists that has points indicating tourists attraction locations..
    any advice or link for reference, maybe?

    thank you..

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: set specific location for radio buttons

    The API is your best friend: Java Platform SE 6

    Specifically, consult the JRadioButton class API for useful methods. You're looking for methods that set the bounds or the position of the component.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Opening Documents in specific location using JButtons
    By twolohan in forum AWT / Java Swing
    Replies: 2
    Last Post: January 5th, 2012, 10:51 AM
  2. output to a specific location in html code
    By aketr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2011, 12:44 PM
  3. Help with radio buttons and results.
    By Bewitched1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 04:01 PM
  4. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 09:28 AM
  5. [SOLVED] Using html Radio Buttons with Servlets
    By oss_2008 in forum Java Servlet
    Replies: 2
    Last Post: June 25th, 2009, 06:39 AM