new to java

whats the best way to go about this?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2173 Views - Last Post: 14 December 2009 - 04:47 PM Rate Topic: -----

#1 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

new to java

Posted 09 December 2009 - 10:59 AM

I wont lie I'm a total newb to Java and programming in general. So I will appologise right off the bat.

Now I have a project where I need to make a swimming pool calculator. All I really need to do is enter in length, width, average depth and create a button to do the calculation. I guess im confused on what the best thing to use for this. Do I use a frame? or panel? I guess it could be an applet too. Should I just use awt package or should I use swing? I guess it's all prefrence maybe?

this really shouldn't be hard to do but i'm having a hard time getting started. I'm not asking for code here but advise. I can create a basic frame but I get confused on adding in my components. I know I'll need lables, textfields,and a button. It will have to use actionListener for the event of clicking the button.

It's when I sit down to write the code is when I get all messed up so to speak. Do all three things (applet, frame, and panel) need to use containers? Again I'm sorry because I kinda of understand how to do this but just lack the ability to putt it together. Again I'm not asking for the code I really do want to write this myself and make it work. But I have been searching and reading and am just having a hard time finding anything that will show me just how to putt the whole thing together.

Thanks in advance for any guidance you can give me!

Is This A Good Question/Topic? 0
  • +

Replies To: new to java

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: new to java

Posted 09 December 2009 - 11:46 AM

1) Do NOT use awt. AWT (Abstract Windowing Technique) was a package that was Java's first stab at GUI. Not only is it inefficient, it contains components that are OS specific. It is deprecated (no longer supported). Use Swing, but don't get too attached. In the next major Java release we are hoping for a new saviour known as Nimbus, so swing will soon be deprecated (hopefully).

2) If you make an applet, you will have to embed it in an HTML and run it in a browser. I recommend just using Swing to make a desktop application.

3) As a basic outline of the classes you will need, it will be as follows:

-Your main class (SomethingWindow) should extend JFrame
-You should have your GUI components in JPanels.
-You can set the Layouts of JPanels
-You can add to Layouts or JPanels
-You will use JLabel, JButton, JTextField
-You will need to make a private inner class to listen for the button press that implements ActionListener

I recommend looking up a tutorial or getting a book. Getting into GUI programming can be a tough concept to grasp (layouts and Panels will drive you mad, trust me). Maybe once I'm done this semester and I have some more time to breathe I'll write a nice Intro to GUI tutorial. Hope that helps though
Was This Post Helpful? 1
  • +
  • -

#3 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 09 December 2009 - 01:23 PM

View PostTriggaMike, on 9 Dec, 2009 - 10:46 AM, said:

1) Do NOT use awt. AWT (Abstract Windowing Technique) was a package that was Java's first stab at GUI. Not only is it inefficient, it contains components that are OS specific. It is deprecated (no longer supported). Use Swing, but don't get too attached. In the next major Java release we are hoping for a new saviour known as Nimbus, so swing will soon be deprecated (hopefully).

2) If you make an applet, you will have to embed it in an HTML and run it in a browser. I recommend just using Swing to make a desktop application.

3) As a basic outline of the classes you will need, it will be as follows:

-Your main class (SomethingWindow) should extend JFrame
-You should have your GUI components in JPanels.
-You can set the Layouts of JPanels
-You can add to Layouts or JPanels
-You will use JLabel, JButton, JTextField
-You will need to make a private inner class to listen for the button press that implements ActionListener

I recommend looking up a tutorial or getting a book. Getting into GUI programming can be a tough concept to grasp (layouts and Panels will drive you mad, trust me). Maybe once I'm done this semester and I have some more time to breathe I'll write a nice Intro to GUI tutorial. Hope that helps though


thanks for the reply, yea I'm sure I'll get the hang of it but it just takes time like anything. I feel a bit like a drowning fish at the moment. this isn't much but here is the basic code I have so far. First I'm just trying to get the frame created and the lables in there. I have the lables in there "sort of" I'll worry about figureing out how to do the orrientation and all that once I get the comonents added. Then I'll get the functionality of the button and so forth done as well but for now I'm just trying to get the frame and components added and looking correct for now.

import javax.swing.*;

public class SwimmingPoolCalc {

	
	public static void main(String[] args) 
	{
		JFrame frame = new JFrame("Swimming Pool Calculator");
		frame.setVisible(true);
		frame.setSize(400,200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label1 = new JLabel("Enter Legth of Pool");
		JLabel label2 = new JLabel("Enter width of Pool");
		JLabel label3 = new JLabel("Enter average volume of Pool");
		JPanel panel = new JPanel();
		
		frame.add(panel);
		panel.add(label1);
		panel.add(label2);
		panel.add(label3);
		
	//public JTextField()
		
	
			
	}


Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: new to java

Posted 09 December 2009 - 01:44 PM

Don't listen to him completely, you still need awt. Import both. Without awt you can't have actionlisteners, right?

Also, does it need to be like a punch-in-the-numbers-in-the-application calculator like MS's built in one or do you just want a dialog to pop up and prompt you for input and do the calculations internally? For something that small you could probably just do the second option, as you don't really need all that UI for just a simple thing like that. In the case of the second option, just do JOptionPane.

This post has been edited by NeoTifa: 09 December 2009 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 09 December 2009 - 01:50 PM

View PostNeoTifa, on 9 Dec, 2009 - 12:44 PM, said:

Don't listen to him completely, you still need awt. Import both. Without awt you can't have actionlisteners, right?

Also, does it need to be like a punch-in-the-numbers-in-the-application calculator like MS's built in one or do you just want a dialog to pop up and prompt you for input and do the calculations internally? For something that small you could probably just do the second option, as you don't really need all that UI for just a simple thing like that. In the case of the second option, just do JOptionPane.



No I would like it to be an app that comes up and has 3 buttons 1, enter legth of pool. 2 enter width, 3 enter volume.
Then have a button that says' calculate volume and a text field to display the volume. I don't really want to use JOptionPane.

Fairly simple it seems. Just trying to get the layout done and running into walls. Seems like everytime I find another place to show you how to do it it's diffent then the last.

and yea I will need to import and take from the awt package I'm sure. But I'd still like to use swing for the main stuff so I"m looking to use JFrame,JTextField, JButton ect.

Thanks.

This post has been edited by gec5741: 09 December 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: new to java

Posted 09 December 2009 - 01:53 PM

Yea, your main package will be swing, but for those buttons to do anything you'd need awt.

So, you want a text field at the top and a total of 4 buttons?
Was This Post Helpful? 0
  • +
  • -

#7 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 09 December 2009 - 02:34 PM

View PostNeoTifa, on 9 Dec, 2009 - 12:53 PM, said:

Yea, your main package will be swing, but for those buttons to do anything you'd need awt.

So, you want a text field at the top and a total of 4 buttons?



Im sorry I totally miss typed!!! I meant 3 lables with 3 textfields and 1 button that says "calculate volume" on it. And one last text field to display the output. Hope that made more sense.
Was This Post Helpful? 0
  • +
  • -

#8 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: new to java

Posted 09 December 2009 - 04:44 PM

Have a look at http://java.sun.com/...olts/index.html You will find some excellent introductory stuff here, including samples of GUI development. Good luck, and happy coding!!!!!
Was This Post Helpful? 0
  • +
  • -

#9 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 10 December 2009 - 07:51 AM

View Postsam.adams61, on 9 Dec, 2009 - 03:44 PM, said:

Have a look at http://java.sun.com/...olts/index.html You will find some excellent introductory stuff here, including samples of GUI development. Good luck, and happy coding!!!!!



Thank you I will take a look!!!
Was This Post Helpful? 0
  • +
  • -

#10 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: new to java

Posted 10 December 2009 - 08:06 AM

View PostTriggaMike, on 9 Dec, 2009 - 05:46 PM, said:

1) Do NOT use awt. AWT (Abstract Windowing Technique) was a package that was Java's first stab at GUI. Not only is it inefficient, it contains components that are OS specific. It is deprecated (no longer supported). Use Swing, but don't get too attached. In the next major Java release we are hoping for a new saviour known as Nimbus, so swing will soon be deprecated (hopefully).


I'd be curious to know where you got the idea that AWT was not to be used, or that it was deprecated. Also, as far as I'm aware, nimbus is a Swing L&F released as part of 6u10, so it's not replacing Swing, but it is an alternative to Metal...
Was This Post Helpful? 0
  • +
  • -

#11 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 10 December 2009 - 01:46 PM

View Postgec5741, on 10 Dec, 2009 - 06:51 AM, said:

View Postsam.adams61, on 9 Dec, 2009 - 03:44 PM, said:

Have a look at http://java.sun.com/...olts/index.html You will find some excellent introductory stuff here, including samples of GUI development. Good luck, and happy coding!!!!!



Thank you I will take a look!!!



What's the best layout to use for this? I was playing with GridBagLayout but seems kinda of complicated. I'd like to be able to position my lables to the left followed by the appropriate txtfield and so on.

thanks.
Was This Post Helpful? 0
  • +
  • -

#12 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 10 December 2009 - 02:18 PM

View Postgec5741, on 10 Dec, 2009 - 12:46 PM, said:

View Postgec5741, on 10 Dec, 2009 - 06:51 AM, said:

View Postsam.adams61, on 9 Dec, 2009 - 03:44 PM, said:

Have a look at http://java.sun.com/...olts/index.html You will find some excellent introductory stuff here, including samples of GUI development. Good luck, and happy coding!!!!!



Thank you I will take a look!!!



What's the best layout to use for this? I was playing with GridBagLayout but seems kinda of complicated. I'd like to be able to position my lables to the left followed by the appropriate txtfield and so on.

thanks.



Here's what I've come up with so far minus the implimatation of action listener or the code to do the math.

sort of what I want but I would like to pretty it up. Not sure if gridbaglayout is the best way to do this. I'd kind of like all my test with the lables to start at the same point to the left then still have all my textfields line up as well on the right of the lables. I'd also like to add a larger font lable with the name of the app along the top middle maybe. Any input would be appreciated. I'm learning at least! :)

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;

public class SwimmCalc {
public static void main(String[] args){
	
	JFrame frame = new JFrame("Swimming Pool Calculator");
	frame.setVisible(true);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(450,250);
	JPanel panel = new JPanel(new GridBagLayout());
	frame.getContentPane().add(panel, BorderLayout.WEST);	
	GridBagConstraints c = new GridBagConstraints();
	//JLabel titel = new JLabel("Swimming Pool Calculator");
	//c.gridx = 0;
	//c.gridy = 0;
	//panel.add(titel, c);
	JLabel lLabel = new JLabel("Enter in the legth of the pool");
	c.gridx = 0;
	c.gridy = 1;
	panel.add(lLabel, c);
	c.insets = new Insets(10,10,10,10);
	JLabel wLabel = new JLabel("Enter in the width of the pool");
	c.gridx = 0;
	c.gridy = 2;
	panel.add(wLabel, c);
	JLabel dLabel = new JLabel("Enter in the average depth of the pool");
	c.gridx = 0;
	c.gridy = 3;
	panel.add(dLabel, c);
	JTextField lTextField = new JTextField(10);
	c.gridx = 2;
	c.gridy = 1;
	panel.add(lTextField, c);
	JTextField wTextField = new JTextField(10);
	c.gridx = 2;
	c.gridy = 2;
	panel.add(wTextField, c);
	JTextField dTextField = new JTextField(10);
	c.gridx = 2;
	c.gridy = 3;
	panel.add(dTextField, c);
	JButton calc = new JButton("Calculate Volume");
	c.gridx = 0;
	c.gridy = 4;
	panel.add(calc, c);
	JTextField total = new JTextField(10);
	c.gridx = 2;
	c.gridy = 4;
	panel.add(total, c);
	
	
	}
}

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new to java

Posted 10 December 2009 - 08:19 PM

View PostTriggaMike, on 9 Dec, 2009 - 10:46 AM, said:

1) Do NOT use awt. AWT (Abstract Windowing Technique) was a package that was Java's first stab at GUI. Not only is it inefficient, it contains components that are OS specific. It is deprecated (no longer supported). Use Swing, but don't get too attached. In the next major Java release we are hoping for a new saviour known as Nimbus, so swing will soon be deprecated (hopefully).

You can give to AWT all the bad things you want but not inefficient
If Swing took soo much time to impose itself it is because it is a lot less efficient and consume a lot more resource than AWT
Until JRE 1.4 the rule was "unless you do not really need performance stay away from Swing" which has been fortunatly completly redesigned in 1.4.
Actually most of the Swing components still rely on their AWT counter part to achieve what they have to do so you will always have more overhead using Swing than AWT directly

This post has been edited by pbl: 10 December 2009 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: new to java

Posted 10 December 2009 - 08:31 PM

Adding to what Pbl said, when an object is created, in addition to invoking its constructors, all the constructors of its superclasses are invoked as well. And since most of the Swing components are the 5th or 6th child class of Object, you will have about 4-5 additional constructors being invoked. When using AWT, you will find that its components are usually the 2nd, 3rd or 4th child classes of Object, so there are fewer constructors being invoked. In short, the more parent classes a given class has, the more overhead is required to create an instance of that class.

This post has been edited by macosxnerd101: 10 December 2009 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#15 gec5741  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 09-December 09

Re: new to java

Posted 10 December 2009 - 09:13 PM

View Postmacosxnerd101, on 10 Dec, 2009 - 07:31 PM, said:

Adding to what Pbl said, when an object is created, in addition to invoking its constructors, all the constructors of its superclasses are invoked as well. And since most of the Swing components are the 5th or 6th child class of Object, you will have about 4-5 additional constructors being invoked. When using AWT, you will find that its components are usually the 2nd, 3rd or 4th child classes of Object, so there are fewer constructors being invoked. In short, the more parent classes a given class has, the more overhead is required to create an instance of that class.



Well now that I have my gui made I need to figure out how to make it functional. I'm pretty sure I understand how to make the ActionListener and actionperformed. really all the action needs to do is multiply length*width*depth. But what I'm not sure is how to actually get the data the user input into my text fields and do the multiplication.

I"m guessing I need to first declare 4 variables all doubles being double length, width, depth, volume;

Now how do I actuall get and use the data the user inputs into the text fields? Will that need to be parsed into doube?

I thought this would be easier then it's turing out to be.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2