Results 1 to 5 of 5
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Java Radio Buttons

    I recently completed a project within netbeans with the help from this forum, thanks for the help.

    I am now just looking into the work that the ide does behind the scenes so I have tried to put the gui that I had in netbeans together from scratch.

    At this moment in time I ham havin 2 problems

    1. Radio buttons are not showing on the panels
    2.The graphic the I want in the top left, I have not managed to work out how to put it into the gui, I have put a text label in position to mark where I want it but the ide made this very easy however I am learning that it is not quite so easy when doing the ide work.

    I would be grateful if someone could just point me in the right direction as far as the radio buttons and the graphic, I have googled quite a few java sites but have not come up with any answers

    Thanks in advance for any help

    Here is the commented code, also attached zipped.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.imageio.*;
    import java.io.*;
    import java.text.*;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.util.*;
    import javax.swing.ButtonGroup;
    
    
    
    // Note the add panels code, this order is important if stacking panels and other components
    
    public class BmiCalc{
    
    	public static void main(String[] args) {
    
    		JFrame frame = new JFrame("Body Mass Index Calculator by David Ashby http://www.ictfd.com");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		// center on screen start
    
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    			Dimension size = frame.getSize();
    			screenSize.height = screenSize.height/6;
    			screenSize.width = screenSize.width/6;
    
    			int y = screenSize.height - size.height;
    			int x = screenSize.width - size.width;
    			frame.setLocation(x, y);
    
    
    		// center on screen end
    
    		[COLOR="DarkGreen"]// Radio Buttons Start ****radio buttons not showing ****[/COLOR]
    
    					JRadioButton cmdMetric   = new JRadioButton("Metric"  , true);
    					JRadioButton cmdImperial = new JRadioButton("Imperial"   , false);
    
    					ButtonGroup measurement = new ButtonGroup();
    					measurement.add(cmdMetric);
    					measurement.add(cmdImperial);
    
    					JPanel radioPanel = new JPanel();
    					radioPanel.setBackground(new java.awt.Color(255, 0, 0));
    					radioPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
    					radioPanel.setLayout(new GridLayout(3, 1));
    					radioPanel.add(cmdMetric);
    					radioPanel.add(cmdImperial);
    
    					radioPanel.setBorder(BorderFactory.createTitledBorder(
    		            BorderFactory.createEtchedBorder(), "Measurements"));
    
    [COLOR="rgb(0, 100, 0)"]		// Radio buttons End
    [/COLOR]
    		// start of icon code for frame icon
    
    				Image icon = Toolkit.getDefaultToolkit().getImage("icon.gif");
    				Label lblicon = new Label("icon.",Label.LEFT);
    				frame.add(lblicon);
    				frame.setIconImage(icon);
    
    		// end of icon code
    
    		// Start of panels code
    
    		JPanel panel = new JPanel();
    		panel.setLayout(null);
    
    		JPanel jp = new JPanel(); // JPanel ******* This is the main Panel used for the background within the frame *******
    		jp.setBackground(new java.awt.Color(204, 204, 255));
    		jp.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
    		jp.setBounds(5, 5, 675, 455); // JPanel indent (first 2 numbers) and dimensions (Last 2 numbers)
    
    		JPanel jp1 = new JPanel(); // JPanel ******* This is the top panel for the measurements and conversions *******
    		jp1.setBackground(new java.awt.Color(204, 204, 255));
    		jp1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    		jp1.setBounds(20, 150, 480, 104); // JPanel indent (first 2 numbers) and dimensions (Last 2 numbers)
    
    		JPanel jp2 = new JPanel(); // JPanel ******* This is the 2nd panel for the bmi output *******
    		jp2.setBackground(new java.awt.Color(204, 204, 255));
    		jp2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    		jp2.setBounds(20, 270, 480, 67); // JPanel indent (first 2 numbers) and size (Last 2 numbers)
    
    		// End of panels code
    
    		// Start of buttons code
    
    			JButton cmdHelp = new JButton("Help");
    			JButton cmdNfile = new JButton("New File");
    			JButton cmdFirstRec = new JButton("First Record");
    			JButton cmdSearchRec = new JButton("Search Records");
    			JButton cmdUpdate = new JButton("Update Records");
    			JButton cmdNrecord = new JButton("Next Record");
    			JButton cmdClose = new JButton("Close Application");
    			JButton cmdConvert = new JButton("Convert");
    			JButton cmdCalculate = new JButton("Calculate");
    			JButton cmdAddrecord = new JButton("Add Record");
    			JButton cmdReset = new JButton("Reset Form");
    
    
    			cmdHelp.setBounds(590,10,80,30);
    			cmdNfile.setBounds(525, 160, 130, 25); // 1st number how far from left
    			cmdFirstRec.setBounds(525, 190, 130, 25); // 2nd number how far down
    			cmdSearchRec.setBounds(525, 275, 130, 25); // 3rd number button length
    			cmdUpdate.setBounds(525, 310, 130, 25); // 4th number button width
    			cmdNrecord.setBounds(180, 350, 130, 25);
    			cmdClose.setBounds(540, 420, 133, 25);
    			cmdConvert.setBounds(30,220,100,25);
    			cmdCalculate.setBounds(30,290,100,25);
    			cmdAddrecord.setBounds(20, 350, 130, 25);
    			cmdReset.setBounds(370,350,130,25);
    
    
    
    			panel.add(cmdHelp);
    			panel.add(cmdNfile); // buttons
    			panel.add(cmdFirstRec);
    			panel.add(cmdSearchRec);
    			panel.add(cmdUpdate);
    			panel.add(cmdNrecord);
    			panel.add(cmdClose);
    			panel.add(cmdConvert);
    			panel.add(cmdCalculate);
    
    			panel.add(cmdAddrecord);
    			panel.add(cmdReset);
    
    
    		// End of buttons code
    
    		// Cursor Change Start ** changes to hand on buttons **
    
    			Cursor c1 = cmdReset.getCursor();
    			cmdReset.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c2 = cmdAddrecord.getCursor();
    			cmdAddrecord.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c3 = cmdHelp.getCursor();
    			cmdHelp.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c4 = cmdCalculate.getCursor();
    			cmdCalculate.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    
    			Cursor c5 = cmdConvert.getCursor();
    			cmdConvert.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c6 = cmdClose.getCursor();
    			cmdClose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c7 = cmdSearchRec.getCursor();
    			cmdSearchRec.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c8 = cmdFirstRec.getCursor();
    			cmdFirstRec.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    
    			Cursor c9 = cmdNfile.getCursor();
    			cmdNfile.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c10 = cmdUpdate.getCursor();
    			cmdUpdate.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			Cursor c11 = cmdNrecord.getCursor();
    			cmdNrecord.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    
    
    		// Cursor change End
    
    
    
    	// Start of Labels code
    
    		JLabel image1 = new JLabel("icon1.gif"); // Label for the logo - image dimensions 146 x 131px
    		JLabel lblMbrship = new JLabel("Mbrship No."); // Label
    		JLabel lblJdate = new JLabel("Join Date"); // Label
    		JLabel lblAge = new JLabel("Age"); // Label
    		JLabel lblGender = new JLabel("Gender"); // Label
    		JLabel lblHeight = new JLabel("Height"); // Label
    		JLabel lblWeight = new JLabel("Weight"); // Label
    		JLabel lblBmi = new JLabel("Bmi"); // Label
    		JLabel lblResults = new JLabel("Results"); // Label
    
    		image1.setBounds(10, 5, 100, 50); // Label for the logo
    		lblMbrship.setBounds(20, 100, 100, 50); // Label
    		lblJdate.setBounds(200, 100, 100, 50); // Label
    		lblAge.setBounds(370, 100, 100, 50); // Label
    		lblGender.setBounds(440, 100, 100, 50); // Label
    		lblHeight.setBounds(160, 155, 100, 50); // 3rd and 4th number dimensions of field
    		lblWeight.setBounds(320, 155, 100, 50); // 1st number in , 2nd number down
    		lblBmi.setBounds(180, 290, 100, 25);
    		lblResults.setBounds(320, 290, 100, 25);
    
    		panel.add(image1); // Label for the logo
    		panel.add(lblMbrship); // Labels as per naming convention
    		panel.add(lblJdate);
    		panel.add(lblAge);
    		panel.add(lblGender);
    		panel.add(lblHeight);
    		panel.add(lblWeight);
    		panel.add(lblBmi);
    		panel.add(lblResults);
    
    
    	// End of Labels code
    
    	// start of Graphic code for frame icon
    
    
    
    	// End of graphic top left
    
    
    	// Start of Texfields
    
    		JTextField txtMembership = new JTextField(20); // textfields
    		JTextField txtJdate = new JTextField(20);
    		JTextField txtAge = new JTextField(20);
    		JTextField txtGender = new JTextField(20);
    		JTextField txtHeight = new JTextField(20);
    		JTextField txtWeight = new JTextField(20);
    		JTextField txtCheight = new JTextField(20);
    		JTextField txtCweight = new JTextField(20);
    		JTextField txtBmi = new JTextField(20);
    		JTextField txtResults = new JTextField(20);
    
    		txtMembership.setBounds(90, 115, 100, 25);  // TextFields for main panel
    		txtJdate.setBounds(260, 115, 100, 25);
    		txtAge.setBounds(400, 115, 30, 25);
    		txtGender.setBounds(485, 115, 20, 25);
    		txtHeight.setBounds(210, 165, 100, 25); // Textfield for 2nd panel
    		txtWeight.setBounds(370, 165, 100, 25);
    		txtCheight.setBounds(210, 195, 100, 25); // Textfields for conversions
    		txtCweight.setBounds(370, 195, 100, 25);
    		txtBmi.setBounds(210, 290, 100, 25); // Textfields for bmi and output
    		txtResults.setBounds(370, 290, 100, 25);
    
    		panel.add(txtMembership);
    		panel.add(txtJdate); // Textfields
    		panel.add(txtAge);
    		panel.add(txtGender);
    		panel.add(txtHeight);
    		panel.add(txtWeight);
    		panel.add(txtCheight);
    		panel.add(txtCweight);
    		panel.add(txtBmi);
    		panel.add(txtResults);
    
    
    	// End of textfields
    
    	// add Frames and Panels Start
    
    		panel.add(jp1);// top panel
    		panel.add(jp2);// bottom panel
    		panel.add(jp); // background JPanel
    		frame.add(panel);
    		frame.setSize(700,500);
    		frame.setVisible(true);
    
    	// add Frames and Panels End
    
    
    
    	}
    }
    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Two problems that I see immediately:

    1) You create a radioPanel but never add it to the GUI.
    2) You are using the null layout rather than using layout managers.

    edit: even worse (sorry to criticize, but I think that this is what you desired when posting the code), this isn't even a real class but instead a large static main method. You really need to create honest classes here if you want to do this right, and divide portions of the GUI into their own separate classes.
    Last edited by Fubarable; 09-20-2009 at 11:07 PM.

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    I realise the way that I had put the code together was incorrect as far as a working application but I thought I would be able to put the gui together have all the components in place then carve it up and format it correctly as I have still not been able to grasp java this would be a way to do it.

    I will start to write the code correctly but I still wonder about the original questions regarding the radio buttons and the graphic to go on the gui itself as the icon is already working.

    Thanks in advance

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The answer regarding your radiobuttons is in my original post. Regarding the graphic, you have to read in the Image, usually with ImageIO.read(...), create an ImageIcon, and place the ImageIcon into a JLabel. The JLabel tutorial will show you how.

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

Similar Threads

  1. [SOLVED] Radio Buttons
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 03:44 PM
  2. Deselect Radio Buttons
    By linux1man in forum AWT / Swing
    Replies: 7
    Last Post: 03-21-2009, 04:01 AM
  3. How to use Radio Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:26 PM
  4. radio buttons and paint
    By gtraylo in forum Java Applets
    Replies: 1
    Last Post: 04-19-2008, 12:43 PM
  5. property for radio buttons
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-03-2007, 08:39 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
  •