# More Java Questions



## doomlord289 (Nov 2, 2005)

Hey everyone. I have some more questions about Java. I would have continued my previous post, but I can't make it "un-solved."
Anyway, I'm building a GUI app based off of my TI-89 program Formula Suite 2005. I have the menu bar working fine, but I can't get the dialog boxes I want to use to display right. I've attached pictures of what it looks like now (done in Java, "gui - now.jpg") and what I want it to look like (done in HTML, "gui - after.jpg"). In the after picture, the "Calculate" button should be centered like in the first picture. I have the input working right, I just need to make it look good, like in the HTML picture.
Here's the code that generates the dialog box and calculates the area of a triangle based on the input from the text fields:

```
package Dialogs.Area;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Triangle extends JDialog implements ActionListener
{
	public Triangle(JFrame parent,String title)
	{
		JDialog aboutDialog = new JDialog(parent,title,true);
		setTitle(title);
		Toolkit theKit = parent.getToolkit();
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		Dimension size = theKit.getScreenSize();
		int windowWidth = 300;
		int windowHeight = 200;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		
		//Sets up the message pane
		messagePane = new JPanel();
		String line1 = "Base: ";
		text = new JLabel(line1);
		messagePane.add(text);
		String line2 = "Height: ";
		text1 = new JLabel(line2,JLabel.CENTER);
		base = new JTextField();
		messagePane.add(base,BorderLayout.NORTH);
		messagePane.add(text1,BorderLayout.SOUTH);
		height = new JTextField();
		height.setMinimumSize(new Dimension(50,30));
		messagePane.add(height,BorderLayout.SOUTH);
		getContentPane().add(messagePane);
		
		//Sets up the button pane
		buttonPane = new JPanel();
		button = new JButton("Calculate");
		buttonPane.add(button);
		button.addActionListener(this);
		getContentPane().add(buttonPane,BorderLayout.SOUTH);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	private JPanel messagePane,buttonPane;
	private JLabel text,text1;
	private JButton button;
	private JTextField base,height;
		
	public void actionPerformed(ActionEvent e)
	{
		setVisible(false);
		this.remove(text);
		this.remove(base);
		this.remove(text1);
		this.remove(height);
		this.remove(button);
		this.remove(messagePane);
		this.remove(buttonPane);
		String base_str = base.getText();
		String height_str = height.getText();
		JPanel answerPane = new JPanel();
		double base_double = Double.parseDouble(base_str);
		double height_double = Double.parseDouble(height_str);
		double answer = .5*base_double*height_double;
		JLabel output = new JLabel("Area = "+answer);
		getContentPane().add(output);
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		int windowWidth = 300;
		int windowHeight = 90;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		setVisible(true);
	}
}
```
I believe I need to use some kind of layout manager, but I don't know which one. If someone could post code to generate a dialog box that contains a form that looks like the one in the HTML picture, I would be very grateful.
Also, is there a shorter way to remove the components from the dialog box than what I'm doing now?


----------



## Chicon (Jul 29, 2004)

Hi doomlord289,

Indeed, you need to use a layout manager.
Personally, I think the *GridBagLayout* is the better as it offers more possibilities.
*Here*, you can find the way to implement a gridbag layout; it's a bit tricky at the begin but it is worth to learn how to make it work as it can be used for every frame.
It will help you to solve your problem of alignment in your example.


----------



## doomlord289 (Nov 2, 2005)

Thanks Chicon! That's 9 problems you've helped me solve.
Like you said, the GridBagLayout will be tricky, but I think I can figure it out. I was looking at a Spring layout, but that didn't quite seem to work.

Also, is there a shorter way to remove the components from the dialog box than what I'm doing now?


----------



## Chicon (Jul 29, 2004)

In your coding, you may replace the whole serie of _this.remove()_ by *this.removeAll();* and add the instruction *this.repaint();* just before the last line ( _setVisible(true);_ ).


----------



## doomlord289 (Nov 2, 2005)

I have more questions, so I figured I'd just continue this thread instead of starting a new one.

Anyways, is there any way to have Greek letters appear in my program? I need a delta (the triangle), a rho (the one that looks a lot like a p), pi (the math symbol), a mu (the micro symbol), a theta (the O with a line in it), a lambda (the thing that looks like an upside-down y), an omega (the weird shaped w), and an alpha (the thing that looks like a fish (according to my physics teacher)). A square root symbol and a superscript of 2, 3, and 4 would be nice too.


If you know how to do any of this, please let me know. Thank you.


----------



## Chicon (Jul 29, 2004)

Hi doomlord289,

There's an example on this page.


----------



## doomlord289 (Nov 2, 2005)

Chicon, the linked source above didn't help like I think you meant it to. My text editor, Notepad2, supports Unicode. However, the MathSymbols code at the bottom did help. I used the Windows Character Map, got the Unicode hex values for the characters I needed, then converted those hex values to decimal and added character variables for each of them.
The values are:

```
public final char pi = 960;
public final char delta = 916;
public final char theta = 952;
public final char omega = 969;
public final char rho = 961;
public final char mu = 956;
public final char lambda = 955;
public final char alpha = 945;
public final char square_root = 8730;
```
I'll post more here as I use them.

For some reason, these won't work with a System.out.println() but they work in a GUI. My thought was DOS couldn't handle Unicode, so it displays a ? instead. Someone please correct me if I'm wrong.
Thank you again.


----------



## Chicon (Jul 29, 2004)

doomlord289 said:


> ... My thought was DOS couldn't handle Unicode, so it displays a ? instead. Someone please correct me if I'm wrong.
> Thank you again.


You're right. MS-DOS can only handle single-byte charsets or _code pages_.

BTW, nice idea to get the Unicode hex values via the Windows Character Map ! :up:


----------



## doomlord289 (Nov 2, 2005)

This question goes back to the first post:
I tried using a GridBagLayout like you said, but using the code posted below, I got the attached screenshot. Can you help me fix this code because nothing I do seems to work right.
Code:

```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Triangle extends JDialog implements ActionListener
{
	public Triangle(JFrame parent,String title)
	{
		JDialog aboutDialog = new JDialog(parent,title,true);
		setTitle(title);
		Toolkit theKit = parent.getToolkit();
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		Dimension size = theKit.getScreenSize();
		int windowWidth = 300;
		int windowHeight = 200;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		
		//Sets up the message pane
		messagePane = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		getContentPane().setLayout(layout);
		
		String line1 = "Base: ";
		text = new JLabel(line1);
		constraints.gridx = 4;
		constraints.gridy = 4;
		constraints.gridheight = 1;
		constraints.gridwidth = 2;
		constraints.weightx = 1;
		constraints.weighty = 2;
		layout.setConstraints(text,constraints);
		messagePane.add(text);
		
		base = new JTextField();
		constraints.gridx = 7;
		constraints.gridy = 4;
		constraints.gridheight = 1;
		constraints.gridwidth = 4;
		constraints.weightx = 1;
		constraints.weighty = 4;
		layout.setConstraints(base,constraints);
		messagePane.add(base);
		
		String line2 = "Height: ";
		text1 = new JLabel(line2);
		constraints.gridx = 4;
		constraints.gridy = 6;
		constraints.gridheight = 1;
		constraints.gridwidth = 2;
		constraints.weightx = 1;
		constraints.weighty = 2;
		layout.setConstraints(text1,constraints);
		messagePane.add(text1);
		
		height = new JTextField();
		constraints.gridx = 7;
		constraints.gridy = 6;
		constraints.gridheight = 1;
		constraints.gridwidth = 4;
		constraints.weightx = 1;
		constraints.weighty = 4;
		layout.setConstraints(height,constraints);
		messagePane.add(height);
		
		getContentPane().add(messagePane);
		
		//Sets up the button pane
		buttonPane = new JPanel();
		button = new JButton("Calculate");
		constraints.gridx = 5;
		constraints.gridy = 10;
		constraints.gridheight = 3;
		constraints.gridwidth = 4;
		layout.setConstraints(button,constraints);
		buttonPane.add(button);
		button.addActionListener(this);
		
		/*constraints.gridx = 4;
		constraints.gridy = 6;
		constraints.gridheight = 1;
		constraints.gridwidth = 2;
		layout.setConstraints(buttonPane,constraints);*/
		getContentPane().add(buttonPane);
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	private JPanel messagePane,buttonPane;
	private JLabel text,text1;
	private JButton button;
	private JTextField base,height;
		
	public void actionPerformed(ActionEvent e)
	{
		setVisible(false);
		this.remove(messagePane);
		this.remove(text);
		this.remove(text1);
		this.remove(base);
		this.remove(height);
		this.remove(buttonPane);
		this.remove(button);
		String base_str = base.getText();
		String height_str = height.getText();
		JPanel answerPane = new JPanel();
		double base_double = Double.parseDouble(base_str);
		double height_double = Double.parseDouble(height_str);
		double answer = .5*base_double*height_double;
		JLabel output = new JLabel("Area = "+answer);
		getContentPane().add(output);
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		int windowWidth = 300;
		int windowHeight = 90;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		setVisible(true);
	}
}
```
Also, the this.removeAll() and this.repaint() didn't work. It just displayed an empty dialog box.

I have more special characters if anyone's interested:

```
public final char degree = 176;
public final char to_the_second = 178;
public final char to_the_third = 179;
```


----------



## Chicon (Jul 29, 2004)

Hi doomlord289,

In your *actionPerformed* method, you may replace the whole serie of *this.remove* by the following command :

*this.getContentPane().removeAll();*

I've tested it and it works; the *repaint* method is no more necessary as the container has been removed.


----------



## doomlord289 (Nov 2, 2005)

but what about the GridBagLayout? That's what I really need help on.


----------



## Chicon (Jul 29, 2004)

doomlord289 said:


> but what about the GridBagLayout? That's what I really need help on.


Sorry, I missed the GBL point. I'm working on it.


----------



## Chicon (Jul 29, 2004)

Here's something more simple :


```
[SIZE=2]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Triangle extends JDialog implements ActionListener
{
	public Triangle(JFrame parent,String title)
	{
		JDialog aboutDialog = new JDialog(parent,title,true);
		setTitle(title);
		Toolkit theKit = parent.getToolkit();
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		Dimension size = theKit.getScreenSize();
		int windowWidth = 300;
		int windowHeight = 200;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		
		//Sets up the message pane
		messagePane = new JPanel();
		GridBagLayout layout = new GridBagLayout();
                messagePane.setLayout(layout);
		GridBagConstraints constraints = new GridBagConstraints();
                constraints.insets = new Insets(2, 2, 2, 2);
		
		
		String line1 = "Base: ";
		text = new JLabel(line1);
                constraints.ipadx = 0;
                constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		messagePane.add(text, constraints);
		
		base = new JTextField();
                constraints.ipadx = 80;
 		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 3;
		messagePane.add(base, constraints);
		
		String line2 = "Height: ";
		text1 = new JLabel(line2);
                constraints.ipadx = 0;
                constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		messagePane.add(text1, constraints);
		
		height = new JTextField();
                constraints.ipadx = 80;
		constraints.gridx = 1;
		constraints.gridy = 1;
		constraints.gridheight = 1;
		constraints.gridwidth = 3;
		messagePane.add(height, constraints);
		
				
		//Sets up the button pane
		buttonPane = new JPanel();
		button = new JButton("Calculate");
                constraints.ipadx = 0;
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		buttonPane.add(button);
                messagePane.add(button, constraints);
		button.addActionListener(this);
		getContentPane().add(messagePane);
				
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	private JPanel messagePane,buttonPane;
	private JLabel text,text1;
	private JButton button;
	private JTextField base,height;
		
	public void actionPerformed(ActionEvent e)
	{
		setVisible(false);
		this.getContentPane().removeAll();
		String base_str = base.getText();
		String height_str = height.getText();
		JPanel answerPane = new JPanel();
		double base_double = Double.parseDouble(base_str);
		double height_double = Double.parseDouble(height_str);
		double answer = .5*base_double*height_double;
		JLabel output = new JLabel("Area = "+answer);
		getContentPane().add(output);
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		int windowWidth = 300;
		int windowHeight = 90;
		setBounds(center.x-windowWidth/2,center.y-windowHeight/2,windowWidth,windowHeight);
		setVisible(true);
	}
}
[/SIZE]
```


----------



## doomlord289 (Nov 2, 2005)

Beautiful! Thank you so much!  :up:


----------



## Chicon (Jul 29, 2004)

doomlord289 said:


> Beautiful! Thank you so much!  :up:


You're welcome !


----------



## doomlord289 (Nov 2, 2005)

I got another question (don't worry, it's simple):
What version of Java should I use to compile this with? I currently compile for version 1.3. Is it likely anyone uses a JRE older than that that supports Swing components? If so, would you recommend compiling for an earlier version than 1.3?


----------



## Chicon (Jul 29, 2004)

Indeed, it's a difficult question : to make sure a majority of people will be able to run a Java program, it's better to develop it for an older version of runtime. But it has a price : the program may be less performant, the developer may miss opportunities to use improved features and so on ...

Personally, I do not develop anymore for a JRE which version is prior to 1.4.2 as a lot of improvements have been brought.

Just have a look to the new features brought to the following JRE :
JRE 1.3
JRE 1.4.2
JRE 1.5.0


----------



## doomlord289 (Nov 2, 2005)

I think I'll take your advice and develop for 1.4.2 and later. I don't think I've ever seen 1.3 anywhere. Even the Windows 95 computers we use in my Computer Science class at school have 1.4.2. Thank you again for your help.


----------

