Results 1 to 16 of 16
  1. #1
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default Please help a noob :)

    I am currently working on a lab for a java class i am in, and I am supposed to make a basic GUI with that will have text boxes, radio buttons, and combo boxes that are used to accept and calculate data on the click of a button.

    My current problem is trying to take the input of the combobox when the calculate volume button is clicked.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class getWetPools extends JFrame
    {
    	private static final int WIDTH = 350;
    	private static final int HEIGHT = 400;
    	
    	private JLabel PlengthL, PwidthL, PdepthL, Pmodel, Plft, Pwft, Pdft;
    	private JTextField PlengthTF, PwidthTF, PdepthTF, PvolumeTF;
    	private JButton PcalculateBtn, PexitBtn;
    	
    	//Handlers
    	private PcalcBtnHandler PcbHandler;
    	private PexitBtnHandler PebHandler;
    	
    	public getWetPools()
    	{
    		//-----Pool Tab------------------------------------------------------------------------
    		Pmodel = new JLabel("Select Pool Model:");
    		PlengthL = new JLabel("Enter the Pool's Length:");
    		PwidthL = new JLabel("Enter the Pool's Width:");
    		PdepthL = new JLabel("Enter the Pool's Depth:");
    		Plft = new JLabel("ft.");
    		Pwft = new JLabel("ft.");
    		Pdft = new JLabel("ft.");
    		
    		PlengthTF = new JTextField(10);
    		PwidthTF = new JTextField(10);
    		PdepthTF = new JTextField(10);
    		PvolumeTF = new JTextField(25);
    		PvolumeTF.setEditable(false);
    		
    		String [] models = { "RX-45-O73", "RX34-O67", "QuickSet-347", "QuickSet-547" };
    	    JComboBox PmodelSelect = new JComboBox(models);
    	    PmodelSelect.setEditable(false);
    		
    		PcalculateBtn = new JButton("Calculate Volume");
    		PcbHandler = new PcalcBtnHandler();
    		PcalculateBtn.addActionListener(PcbHandler);
    		PexitBtn = new JButton("Exit Program");
    		PebHandler = new PexitBtnHandler();
    		PexitBtn.addActionListener(PebHandler);
    		
    		JFrame frame = new JFrame("Get Wet Pools & Spas");
    	    JTabbedPane tabby = new JTabbedPane(  );
    	    JPanel pool = new JPanel(  );
    	    
    	    pool.add(Pmodel);
    	    pool.add(PmodelSelect);
    	    pool.add(PlengthL);
    	    pool.add(PlengthTF); pool.add(Plft);
    	    pool.add(PwidthL);
    	    pool.add(PwidthTF); pool.add(Pwft);
    	    pool.add(PdepthL);
    	    pool.add(PdepthTF); pool.add(Pdft);
    	    pool.add(PcalculateBtn);
    	    pool.add(PexitBtn);
    	    pool.add(PvolumeTF);
    	    
    	    PcalculateBtn.setMnemonic('C');
    	    PexitBtn.setMnemonic('x');
    	    
    	    tabby.addTab("Pools", pool);
    	    
    	    frame.getContentPane(  ).add(tabby);
    	    frame.setSize(WIDTH, HEIGHT);
    	    frame.setVisible(true);
    	    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);	
    	}
    	
    	private class PcalcBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double Pwidth, Plength, Pdepth, Pvolume;
    		
    			Plength = Double.parseDouble(PlengthTF.getText());
    			Pwidth = Double.parseDouble(PwidthTF.getText());
    			Pdepth = Double.parseDouble(PdepthTF.getText());
    			Pvolume = Plength * Pwidth * Pdepth;
    			
    			PvolumeTF.setText("Model's volume is: " + Pvolume);
    		}
    		
    		
    	}
    	
    	public class PexitBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		getWetPools getWet = new getWetPools();
    	}
    }
    I would like for the output to contain the model also,for example...

    "Model RX34-O67's volume is 7392.00"

    To anyone that has a solution for me thanks a million.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    No, I can do the formatting, I just dont kno how to make the JComboBox do anything but look pritty. When the PcalculateBtn is pushed i need it to read and display the model that was selected from the combobox, Im not sure how to make it check to see witch item was selected and how to set it to a varible for output.

    Edit: I will try to explain it like this.

    In this method the action for the PcalculateBtn JButton is defined, and it sets the value of soem varibles to the text that was typed into the TextFields. And what i need is for it to also do the same thing with the item that was selected in the JComboBox.

    Java Code:
    private class PcalcBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double Pwidth, Plength, Pdepth, Pvolume;
    		
    			Plength = Double.parseDouble(PlengthTF.getText());
    			Pwidth = Double.parseDouble(PwidthTF.getText());
    			Pdepth = Double.parseDouble(PdepthTF.getText());
    			Pvolume = Plength * Pwidth * Pdepth;
    			
    			PvolumeTF.setText("Model's volume is: " + Pvolume);
    		}
    		
    		
    	}
    Last edited by Bays; 06-16-2008 at 09:47 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all define the JComboBox in class level. If not it's wired to use in the result text box you used.

  5. #5
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    Ok. And I did an edit to help better explain my problem.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This may you looking,

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class getWetPools extends JFrame
    {
    	private static final int WIDTH = 350;
    	private static final int HEIGHT = 400;
    	
    	private JLabel PlengthL, PwidthL, PdepthL, Pmodel, Plft, Pwft, Pdft;
    	private JTextField PlengthTF, PwidthTF, PdepthTF, PvolumeTF;
    	private JButton PcalculateBtn, PexitBtn;
            private JComboBox PmodelSelect;
    	
    	//Handlers
    	private PcalcBtnHandler PcbHandler;
    	private PexitBtnHandler PebHandler;
    	
    	public getWetPools()
    	{
    		//-----Pool Tab------------------------------------------------------------------------
    		Pmodel = new JLabel("Select Pool Model:");
    		PlengthL = new JLabel("Enter the Pool's Length:");
    		PwidthL = new JLabel("Enter the Pool's Width:");
    		PdepthL = new JLabel("Enter the Pool's Depth:");
    		Plft = new JLabel("ft.");
    		Pwft = new JLabel("ft.");
    		Pdft = new JLabel("ft.");
    		
    		PlengthTF = new JTextField(10);
    		PwidthTF = new JTextField(10);
    		PdepthTF = new JTextField(10);
    		PvolumeTF = new JTextField(25);
    		PvolumeTF.setEditable(false);
    		
    		String [] models = { "RX-45-O73", "RX34-O67", "QuickSet-347", "QuickSet-547" };
    	    PmodelSelect = new JComboBox(models);
    	    PmodelSelect.setEditable(false);
    		
    		PcalculateBtn = new JButton("Calculate Volume");
    		PcbHandler = new PcalcBtnHandler();
    		PcalculateBtn.addActionListener(PcbHandler);
    		PexitBtn = new JButton("Exit Program");
    		PebHandler = new PexitBtnHandler();
    		PexitBtn.addActionListener(PebHandler);
    		
    		JFrame frame = new JFrame("Get Wet Pools & Spas");
    	    JTabbedPane tabby = new JTabbedPane(  );
    	    JPanel pool = new JPanel(  );
    	    
    	    pool.add(Pmodel);
    	    pool.add(PmodelSelect);
    	    pool.add(PlengthL);
    	    pool.add(PlengthTF); pool.add(Plft);
    	    pool.add(PwidthL);
    	    pool.add(PwidthTF); pool.add(Pwft);
    	    pool.add(PdepthL);
    	    pool.add(PdepthTF); pool.add(Pdft);
    	    pool.add(PcalculateBtn);
    	    pool.add(PexitBtn);
    	    pool.add(PvolumeTF);
    	    
    	    PcalculateBtn.setMnemonic('C');
    	    PexitBtn.setMnemonic('x');
    	    
    	    tabby.addTab("Pools", pool);
    	    
    	    frame.getContentPane(  ).add(tabby);
    	    frame.setSize(WIDTH, HEIGHT);
    	    frame.setVisible(true);
    	    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);	
    	}
    	
    	private class PcalcBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double Pwidth, Plength, Pdepth, Pvolume;
    		
    			Plength = Double.parseDouble(PlengthTF.getText());
    			Pwidth = Double.parseDouble(PwidthTF.getText());
    			Pdepth = Double.parseDouble(PdepthTF.getText());
    			Pvolume = Plength * Pwidth * Pdepth;
    			
    			PvolumeTF.setText(PmodelSelect.getSelectedItem().toString()
                                    + "Model's volume is: " + Pvolume);
    		}
    		
    		
    	}
    	
    	public class PexitBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		getWetPools getWet = new getWetPools();
    	}
    }

  7. #7
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    OMG your a life saver, that is exactly correct. Thanks a million.

    I have another Questions, and will let you take a shot at it.

    I am also required to make the output be added to a outside file, and then I will have to code a tab that will read the file. Can you kinda direct me in a path that explains how to use read and write features in java. Or posibly just show me how i would write this output to a file, and i will be able to figure out the read.

    Thanks again. +rep

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually to write a file, you need to have a buffered the stream, basically you have to use a BufferedWriter. Try and see.

  9. #9
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    BufferedWriter and an example.

    Hope that helps,
    freedom exists in the world of ideas

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    ok, this is working, however each time the program is ran, or each time the button is pusshed it clears the file and writes the line. I dont want it to clear the file, i want it to write the information on a new line. Here is what i got right now.

    Java Code:
    	private class PcalcBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double Pwidth, Plength, Pdepth, Pvolume;
    			String volumeOutput;
    		
    			Plength = Double.parseDouble(PlengthTF.getText());
    			Pwidth = Double.parseDouble(PwidthTF.getText());
    			Pdepth = Double.parseDouble(PdepthTF.getText());
    			Pvolume = Plength * Pwidth * Pdepth;
    			
    			volumeOutput = ("Model " + PmodelSelect.getSelectedItem().toString() + "'s volume is: " + Pvolume);
    			PvolumeTF.setText(volumeOutput);	
    	        
    	        BufferedWriter bufferedWriter = null;
    	        
    	        try {
    	            
    	            //Construct the BufferedWriter object
    	            bufferedWriter = new BufferedWriter(new FileWriter("info.dat"));
    	            
    	            //Start writing to the output stream
    	            bufferedWriter.write(volumeOutput);
    	            bufferedWriter.newLine();
    	            
    	        } catch (FileNotFoundException ex) {
    	            ex.printStackTrace();
    	        } catch (IOException ex) {
    	            ex.printStackTrace();
    	        } finally {
    	            //Close the BufferedWriter
    	            try {
    	                if (bufferedWriter != null) {
    	                    bufferedWriter.flush();
    	                    bufferedWriter.close();
    	                }
    	            } catch (IOException ex) {
    	                ex.printStackTrace();
    	            }
    	        }
    
    		}

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Lots of errors in this class.

    1. private access modifier is not allowed.

    2. If this is the full code of the class, PlengthTF, PwidthTF and few other variables are not defined.

    3. Required packages are not imported.

    Here is a much simpler example. See what happened there. Basically what you have to do is, append data to the file.

    Java Code:
        public static void main(String[] args) {
            try {
                FileWriter fw = new FileWriter("files/AppendData.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("Java");
                bw.close();
            }
            catch(Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }

  13. #13
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default no

    no that was not the full code. Here it is so far.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    
    public class getWetPools extends JFrame
    {
    	private static final int WIDTH = 350;
    	private static final int HEIGHT = 400;
    	
    	private JLabel PlengthL, PwidthL, PdepthL, Pmodel, Plft, Pwft, Pdft, pInfo, sShape, 
    						sWidthL, sLengthL, sDepthL, sVolumeL, Slft, Swft, Sdft;
    	private JTextField PlengthTF, PwidthTF, PdepthTF, PvolumeTF, 
    						sLengthTF, sWidthTF, sDepthTF, sVolumeTF;
    	private JTextArea pMInfo;
    	private JButton PcalculateBtn, PexitBtn, sCalcBtn, sExitBtn;
    	String modelInfo;
    	
    	String [] models = { "RX-45-O73", "RX34-O67", "QuickSet-347", "QuickSet-547" };
        JComboBox PmodelSelect = new JComboBox(models);
    	
    	//Handlers
    	private PcalcBtnHandler PcbHandler;
    	private PexitBtnHandler PebHandler;
    	
    	
    	public getWetPools()
    	{
    		//-----Pool Define--------------------------------------------------------------------
    		Pmodel = new JLabel("Select Pool Model:");
    		PlengthL = new JLabel("Enter the Pool's Length:");
    		PwidthL = new JLabel("Enter the Pool's Width:");
    		PdepthL = new JLabel("Enter the Pool's Depth:");
    		Plft = new JLabel("ft.");
    		Pwft = new JLabel("ft.");
    		Pdft = new JLabel("ft.");
    		
    		PlengthTF = new JTextField(10);
    		PwidthTF = new JTextField(10);
    		PdepthTF = new JTextField(10);
    		PvolumeTF = new JTextField(25);
    		PvolumeTF.setEditable(false);
    		PmodelSelect.setEditable(true);
    		
    		PcalculateBtn = new JButton("Calculate Volume");
    		PcbHandler = new PcalcBtnHandler();
    		PcalculateBtn.addActionListener(PcbHandler);
    		PexitBtn = new JButton("Exit Program");
    		PebHandler = new PexitBtnHandler();
    		PexitBtn.addActionListener(PebHandler);
    		
    		//-----pInfo Define--------------------------------------------------------------------
    		pInfo = new JLabel("Current Model Information:");
    		
    		pMInfo = new JTextArea(6, 25);
    		pMInfo.setEditable(false);
    		
    		//-----Spas Define---------------------------------------------------------------------
    		sShape = new JLabel("Select Spa Shape:");
    		sLengthL = new JLabel("Enter Spa's Length:");
    		sWidthL = new JLabel("Enter Spa's Width:");
    		sDepthL = new JLabel("Enter Spa's Depth:");
    		Slft = new JLabel("ft.");
    		Swft = new JLabel("ft.");
    		Sdft = new JLabel("ft.");
    		
    		sLengthTF = new JTextField(10);
    		sWidthTF = new JTextField(10);
    		sDepthTF = new JTextField(10);
    		sVolumeTF = new JTextField(25);
    		
    
    		
    		
    		//-----Tabs Created--------------------------------------------------------------------
    		JFrame frame = new JFrame("Get Wet Pools & Spas");
    	    JTabbedPane tabby = new JTabbedPane(  );
    	    
    	    //-----Pool Tab------------------------------------------------------------------------
    	    JPanel pool = new JPanel(  );
    	    
    	    pool.add(Pmodel);
    	    pool.add(PmodelSelect);
    	    pool.add(PlengthL);
    	    pool.add(PlengthTF); pool.add(Plft);
    	    pool.add(PwidthL);
    	    pool.add(PwidthTF); pool.add(Pwft);
    	    pool.add(PdepthL);
    	    pool.add(PdepthTF); pool.add(Pdft);
    	    pool.add(PcalculateBtn);
    	    pool.add(PexitBtn);
    	    pool.add(PvolumeTF);
    	    
    	    PcalculateBtn.setMnemonic(KeyEvent.VK_C);
    	    PexitBtn.setMnemonic(KeyEvent.VK_X);
    	    
    	    //-----Pool Info tab---------------------------------------------------------------------
    	    JPanel poolInfo = new JPanel(  );
    	    
    	    poolInfo.add(pInfo);
    	    poolInfo.add(pMInfo);
    	    
    	    try {
    		       BufferedReader br = new BufferedReader(new FileReader("models.txt"));
    		       while ((modelInfo = br.readLine()) != null) { // while loop begins here
    		    	   pMInfo.setText(modelInfo);
    		       } // end while 
    		     } // end try
    		     catch (IOException ex) {
    		       System.err.println("Error: " + ex);
    		     }
    		     
    		//-----Pool Info tab---------------------------------------------------------------------
    			    JPanel spas = new JPanel(  );
    			    
    			    spas.add(sShape);
    			    
    	    
    	    //---------------------------------------------------------------------------------------
    	    
    	    tabby.addTab("Pools", pool);
    	    tabby.addTab("Pool Info", poolInfo);
    	    tabby.addTab("Spas", spas);
    	    
    	    frame.getContentPane(  ).add(tabby);
    	    frame.setSize(WIDTH, HEIGHT);
    	    frame.setVisible(true);
    	    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	    
    	}
    	
    	//-----Pool Buttons--------------------------------------------------------------------------
    	private class PcalcBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double Pwidth, Plength, Pdepth, Pvolume;
    			String volumeOutput;
    		
    			Plength = Double.parseDouble(PlengthTF.getText());
    			Pwidth = Double.parseDouble(PwidthTF.getText());
    			Pdepth = Double.parseDouble(PdepthTF.getText());
    			Pvolume = Plength * Pwidth * Pdepth;
    			
    			volumeOutput = ("Model " + PmodelSelect.getSelectedItem().toString() + "'s volume is: " + Pvolume);
    			PvolumeTF.setText(volumeOutput);	
    	        
    	        BufferedWriter bufferedWriter = null;
    	        
    	        try {
    	            
    	            //Construct the BufferedWriter object
    	            bufferedWriter = new BufferedWriter(new FileWriter("models.txt"));
    	            
    	            //Start output stream
    	            bufferedWriter.write(volumeOutput);
    	            bufferedWriter.newLine();
    	            
    	        } catch (FileNotFoundException ex) {
    	            ex.printStackTrace();
    	        } catch (IOException ex) {
    	            ex.printStackTrace();
    	        } finally {	
    	            //Close the BufferedWriter
    	            try {
    	                if (bufferedWriter != null) {
    	                	bufferedWriter.flush();
    	                    bufferedWriter.close();
    	                }
    	            } catch (IOException ex) {
    	                ex.printStackTrace();
    	            }
    	        }
    
    		}
    	}
    
    	public class PexitBtnHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		getWetPools getWet = new getWetPools();
    	}
    }

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  15. #15
    Bays is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    Yes that worked, seems like the bufferedWriter.newLine(); was what was causeing it....

    How would i make it linebreak and print to a new line everytime it writes something?

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 06:10 PM
  2. Noob question- easy
    By mattonitto in forum New To Java
    Replies: 7
    Last Post: 06-13-2008, 01:26 AM
  3. [noob]Problem with TOS .bat
    By fred33 in forum Advanced Java
    Replies: 0
    Last Post: 03-19-2008, 03:04 PM
  4. Ah! Help a Java Noob
    By Snejana in forum New To Java
    Replies: 4
    Last Post: 01-24-2008, 04:52 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
  •