Java Homework... Too many windows (GUI)

manicmike

Honorable
Mar 3, 2012
26
0
10,580
My program works except for one problem. I run it, it pops up a GUI and allows me to enter text as required... but if I look at my task bar on the bottom of the screen, there are 2 more instances of the GUI. When I close one instance of the GUI, it closes all other instances. The program outputs correctly, but I don't want 3 identical GUIs

thank you for any help, and I apologize for doing a bad job of explaining, but if you copy/paste and run the code you'll see what I mean

Code:
import java.util.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class MyList < T extends Comparable> extends JFrame
{
	static MyList<Integer> listInt = new MyList<Integer>();
	static MyList<String> listString = new MyList<String>();
	
	
	private JTextField num1 = new JTextField(10);
	private JTextField num2 = new JTextField(10);
	private JTextField num3 = new JTextField(10);
	private JTextField str1 = new JTextField(10);
	private JTextField str2 = new JTextField(10);
	private JTextField str3 = new JTextField(10);
	private JLabel numMessage = new JLabel("Please enter \n3 integers");
	private JLabel strMessagePt1 = new JLabel("Please enter");
	private JLabel strMessagePt2 = new JLabel("3 one-word strings");
	private JButton runNum = new JButton("Run Number Function");
	private JButton runStr = new JButton("Run String Function");
	private JPanel panel1 = new JPanel();
	private JPanel panel2 = new JPanel();
	private JPanel panel3 = new JPanel();
	private JPanel panel4 = new JPanel();
	private JPanel panel5 = new JPanel();
	private JPanel panel6 = new JPanel();
	private JPanel blank1 = new JPanel();
	private JPanel blank2 = new JPanel();
	private JPanel blank3 = new JPanel();
	private final int WINDOW_WIDTH = 500; 				// Window width
	private final int WINDOW_HEIGHT = 300; 				// Window height
	
	ArrayList<T> newlist = new ArrayList<T>(); 

	public MyList()
	{
		// Set window parameters
		setTitle("MyList Modified");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setLayout(new GridLayout(3,3));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		panel1.add(numMessage);
		panel2.add(num1);
		panel2.add(num2);
		panel2.add(num3);
		runNum.addActionListener(new numListener());
		panel3.add(runNum);
		
		panel4.add(strMessagePt1);
		panel4.add(strMessagePt2);
		panel5.add(str1);
		panel5.add(str2);
		panel5.add(str3);
		runStr.addActionListener(new strListener());
		panel6.add(runStr);
		
		add(panel1);
		add(blank1);
		add(panel4);
		add(panel2);
		add(blank2);
		add(panel5);
		add(panel3);
		add(blank3);
		add(panel6);
		
		setVisible(true);
	}

	public void add(T thing)
	{
		newlist.add(thing);
	}

	public T getSmallest()  
	{  
		T smallest = (T) newlist.get(0);  
		
		for(int i=1; i<newlist.size(); i++)  
		{
			if(((Comparable<T>) newlist.get(i)).compareTo(smallest) <0)  
				smallest = (T) newlist.get(i);  
		}  
		return smallest;   
	}
	
	public T getLargest()  
	{  
		T largest = (T) newlist.get(0); 
		
		for(int i=1; i<newlist.size(); i++)  
		{  
			if(((Comparable<T>) newlist.get(i)).compareTo(largest) > 0)  
				largest = (T) newlist.get(i);  
		}  
		return largest;   
	}
	
	private class numListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Comparable number1 = Integer.parseInt(num1.getText());
			Comparable number2 = Integer.parseInt(num2.getText());
			Comparable number3 = Integer.parseInt(num3.getText());
			listInt.add((Integer) number1);
			listInt.add((Integer) number2);
			listInt.add((Integer) number3);
			Comparable max = listInt.getLargest();
			Comparable min = listInt.getSmallest();
			JOptionPane.showMessageDialog(null, "You entered "+number1+", "+
					number2+" & "+number3+"\n Largest Number: "+max+
					"\nSmallest Number: "+min);
		}
	}
	
	private class strListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Comparable string1 = str1.getText();
			Comparable string2 = str2.getText();
			Comparable string3 = str3.getText();
			listString.add((String) string1);
			listString.add((String) string2);
			listString.add((String) string3);
			Comparable max = listString.getLargest();
			Comparable min = listString.getSmallest();
			JOptionPane.showMessageDialog(null, "You entered "+string1+", "+
					string2+" & "+string3+"\n Largest String: "+max+
					"\nSmallest String: "+min);
		}
	}

	public static void main(String[] args)
	{
		MyList myListIntAndStr = new MyList();
	}
}
 

Scott_D_Bowen

Honorable
Nov 28, 2012
50
0
10,590
Look at: public static void main(String[] args)

Then look at your constructor...
Then read up on the Java API for JFrame & its setDefaultCloseOperation() method.
- http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29

Actually that was a really good example.
- I did not see it on the first two passes, I had to read it three times (I'm executing it in my head, I don't have NetBeans installed atm).

Every time you make a new object using: new MyList<Integer>();, new MyList<String>();, new MyList();, etc. the constructor is called.

You have the static constant JFrame.EXIT_ON_CLOSE hard coded in your constructor, instead of accepting a parameter and your class extends JFrame.

I would suggest overloading your constructor to accept an additional parameter (e.g. int setDefaultCloseOperation), that if set to one of the JFrame.CONSTANTS passes that to the call for the first two static calls.

Basically, you don't want JFrame.EXIT_ON_CLOSE for all three, and it's hard coded into your non-overloaded constructor.
- This is easy to fix, as you can migrate the code into a better constructor very easily, and have the constructor with parameter just call it's overloaded self, passing the hard coded JFrame.EXIT_ON_CLOSE for the window that you want to close the app.