Results 1 to 11 of 11
  1. #1
    Tonzoffun is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Question Please help :( first time using JPanel..

    I'm trying to make a simple program that allows you to enter numbers into a list of items, and have it return those numbers in an array to the main program for later use, but when the program runs, it doesn't allow time to you to input them before immediately running the next line in the main program.. Is there a way to have a "Submit" button for JPanel?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import java.text.*;
    
    
    public class HealthProgram extends JPanel {
    
        // Values for the fields
        private static int bloodpressureH = 0;
        private static int bloodpressureL = 0;
        private static int bloodsugar = 0;
        private static int weight = 0;
        private static int height = 0;
        private static int bodyfatperc = 0;
        private static int age = 0;
    
        // Labels to identify the fields
        private JLabel bloodpressureHLabel;
        private JLabel bloodpressureLLabel;
        private JLabel bloodsugarLabel;
        private JLabel weightLabel;
        private JLabel heightLabel;
        private JLabel bodyfatpercLabel;
        private JLabel ageLabel;
    	 
        // Strings for the labels
        private static String bloodpressureHString = "Blood Pressure High: ";
        private static String bloodpressureLString = "Blood Pressure Low: ";
        private static String bloodsugarString = "Blood Sugar: ";
        private static String weightString = "Weight: ";
        private static String heightString = "Height: ";
        private static String bodyfatpercString = "Body Fat %: ";
        private static String ageString = "Age: ";
    
        // Fields for data entry
        private JFormattedTextField bloodpressureHField;
        private JFormattedTextField bloodpressureLField;
        private JFormattedTextField bloodsugarField;
        private JFormattedTextField weightField;
        private JFormattedTextField heightField;
        private JFormattedTextField bodyfatpercField;
        private JFormattedTextField ageField;
    
        // Formats to format and parse numbers
        private NumberFormat bloodpressureHFormat;
        private NumberFormat bloodpressureLFormat;
        private NumberFormat bloodsugarFormat;
        private NumberFormat weightFormat;
        private NumberFormat heightFormat;
        private NumberFormat bodyfatpercFormat;
        private NumberFormat ageFormat;
    
        public HealthProgram() {
            super(new BorderLayout());
            setUpFormats();
    
            //Create the labels.
            bloodpressureHLabel = new JLabel(bloodpressureHString);
            bloodpressureLLabel = new JLabel(bloodpressureLString);
            bloodsugarLabel = new JLabel(bloodsugarString);
            weightLabel = new JLabel(weightString);
    	heightLabel = new JLabel(heightString);
    	bodyfatpercLabel = new JLabel(bodyfatpercString);
    	ageLabel = new JLabel(ageString);
    
            //Create the text fields and set them up.
            bloodpressureHField = new JFormattedTextField(bloodpressureHFormat);
            bloodpressureHField.setValue(new Integer(bloodpressureH));
            bloodpressureHField.setColumns(10);
    
            bloodpressureLField = new JFormattedTextField(bloodpressureLFormat);
            bloodpressureLField.setValue(new Integer(bloodpressureL));
            bloodpressureLField.setColumns(10);
    		  
            bloodsugarField = new JFormattedTextField(bloodsugarFormat);
            bloodsugarField.setValue(new Integer(bloodsugar));
            bloodsugarField.setColumns(10);
    
            weightField = new JFormattedTextField(weightFormat);
            weightField.setValue(new Integer(weight));
            weightField.setColumns(10);
    
            heightField = new JFormattedTextField(heightFormat);
            heightField.setValue(new Integer(height));
            heightField.setColumns(10);        
    		  
            bodyfatpercField = new JFormattedTextField(bodyfatpercFormat);
            bodyfatpercField.setValue(new Integer(bodyfatperc));
            bodyfatpercField.setColumns(10);		  
    		  
            ageField = new JFormattedTextField(ageFormat);
            ageField.setValue(new Integer(age));
            ageField.setColumns(10);		  
    		  
    
    	//Tell accessibility tools about label/textfield pairs.
            bloodpressureHLabel.setLabelFor(bloodpressureHField);
            bloodpressureLLabel.setLabelFor(bloodpressureLField);
            bloodsugarLabel.setLabelFor(bloodsugarField);
            weightLabel.setLabelFor(weightField);
    	heightLabel.setLabelFor(heightField);
    	bodyfatpercLabel.setLabelFor(bodyfatpercField);
    	ageLabel.setLabelFor(ageField);
    
            //Lay out the labels in a panel.
            JPanel labelPane = new JPanel(new GridLayout(0,1));
            labelPane.add(bloodpressureHLabel);
            labelPane.add(bloodpressureLLabel);
            labelPane.add(bloodsugarLabel);
            labelPane.add(weightLabel);
            labelPane.add(heightLabel);
    	labelPane.add(bodyfatpercLabel);
    	labelPane.add(ageLabel);
    		  
    
            //Layout the text fields in a panel.
            JPanel fieldPane = new JPanel(new GridLayout(0,1));
            fieldPane.add(bloodpressureHField);
    	fieldPane.add(bloodpressureLField);
    	fieldPane.add(bloodsugarField);
    	fieldPane.add(weightField);
    	fieldPane.add(heightField);
    	fieldPane.add(bodyfatpercField);
    	fieldPane.add(ageField);
    
            //Put the panels in this panel, labels on left,
            //text fields on right.
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            add(labelPane, BorderLayout.CENTER);
            add(fieldPane, BorderLayout.LINE_END);
        }
    
        public static int[] createAndShowGUI(int[] valuesArray) {
            //Create and set up the window.
            JFrame frame = new JFrame("Health Program");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add contents to the window.
            frame.add(new HealthProgram());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
    		 
    	valuesArray[0] = bloodpressureH;
    	valuesArray[1] = bloodpressureL;
    	valuesArray[2] = bloodsugar;
    	valuesArray[3] = weight;
    	valuesArray[4] = height;
    	valuesArray[5] = bodyfatperc;
    	valuesArray[6] = age;
    	 
    		  
    	return valuesArray;
    	}
    
        public static void main(String[] args) {
    		  
    	int[] valuesArray = new int[7]; 
    		  
        	JOptionPane.showMessageDialog(null, "Welcome to the Health Program!",
    	"Health Program", JOptionPane.INFORMATION_MESSAGE);    
    		  
            createAndShowGUI(valuesArray);
    		  
    	JOptionPane.showMessageDialog(null, "Blood Pressure is: " + valuesArray[0] + " / " + valuesArray[1],
    	"Blood Pressure", JOptionPane.INFORMATION_MESSAGE);    
    		  
            }
    	 
        //Create and set up number formats. These objects also
        //parse numbers input by user.
        private void setUpFormats() {
            bloodpressureHFormat = NumberFormat.getNumberInstance();
    	bloodpressureLFormat = NumberFormat.getNumberInstance();
    	bloodsugarFormat = NumberFormat.getNumberInstance();
    	weightFormat = NumberFormat.getNumberInstance();
    	heightFormat = NumberFormat.getNumberInstance();
    	bodyfatpercFormat = NumberFormat.getNumberInstance();
    	ageFormat = NumberFormat.getNumberInstance();
        }
    }
    Thoughts?? Much appreciated!!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Tonzoffun View Post
    Is there a way to have a "Submit" button for JPanel?
    Have you looked at a Swing tutorial?

    You are using JLabels, JPanels and JFormattedTextFields. Did it not occur to you to see if there was a JButton as well?

  3. #3
    Tonzoffun is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Yes, I realize there's a JButton, but how would I make the program wait until all values are entered, or until the Submit button is pressed for it to move to the next line in the main program?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If you read a Swing tutorial or went to the Java API for the JButton class and followed the "How to use buttons" link you will find plenty of information of how to do it.

  5. #5
    Tonzoffun is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    okay thanks, well I'll read into the JButton option.. but excluding that part from the problem.. any other solutions that would force the program to wait until values are entered before proceeding?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    So you cannot be bothered and just want someone to provide you an easier solution. Well expanding your knowledge and learning about JButtons is the correct solution.

  7. #7
    Tonzoffun is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    I'm not sure why u insist on insulting me like I'm some 13 year old kid.. I am doing research on it on my own trying to figure out more about JPanel since I haven't used java in years.. ever considered this was one way of me doing research, not just looking for the easy way out.. oh great genius of wise vast knowledge, I can clearly see you've never run into anything you don't know how to do, you were born with the ability to program and solve differential equations.. get real, everyone learns at one point, and forums is a branch of knowledge for me as well as web research of my own.. every little bit of knowledge gained adds up

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If I am insulting you then you need to grow a thicker skin. As to the advice I have provided all your questions will be answered if you bothered to read the tutorial but it appears to me that you are reluctant to do that and are simply after someone to spoonfeed you the solution. On the other hand if you want me to provide further information on how to use JButtons then why should I do that? Do you want me to waste my time typing up a bunch of example code and provide a full explanation of what it is doing when it is already out there on the net in numerous tutorials?

  9. #9
    Tonzoffun is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Why bother have this forum about java then.. since obviously there's no need for this site at all since everything anyone needs to know is elsewhere.. It was a simple question, I don't see why you have to get all riled up over it, I was simply asking for advice not a slap in the face and told to go elsewhere.. And 2 seconds worth of "here's a link to what I mean" would have sufficed

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    The point of this forum is for people to seek advice. My advice to you was read the tutorial. What is the point of you continuing to whine about it?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Tonzoffun View Post
    "here's a link to what I mean" would have sufficed
    Oh, I see I supposed to do you friggin research as well.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  3. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  4. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  5. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 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
  •