Sign in with
Sign up | Sign in
Your question

Java Homework... Too many windows (GUI)

Last response: in Applications
Share
October 16, 2012 9:18:52 PM

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

  1. import java.util.*;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.*;
  6.  
  7.  
  8. public class MyList < T extends Comparable> extends JFrame
  9. {
  10. static MyList<Integer> listInt = new MyList<Integer>();
  11. static MyList<String> listString = new MyList<String>();
  12.  
  13.  
  14. private JTextField num1 = new JTextField(10);
  15. private JTextField num2 = new JTextField(10);
  16. private JTextField num3 = new JTextField(10);
  17. private JTextField str1 = new JTextField(10);
  18. private JTextField str2 = new JTextField(10);
  19. private JTextField str3 = new JTextField(10);
  20. private JLabel numMessage = new JLabel("Please enter \n3 integers");
  21. private JLabel strMessagePt1 = new JLabel("Please enter");
  22. private JLabel strMessagePt2 = new JLabel("3 one-word strings");
  23. private JButton runNum = new JButton("Run Number Function");
  24. private JButton runStr = new JButton("Run String Function");
  25. private JPanel panel1 = new JPanel();
  26. private JPanel panel2 = new JPanel();
  27. private JPanel panel3 = new JPanel();
  28. private JPanel panel4 = new JPanel();
  29. private JPanel panel5 = new JPanel();
  30. private JPanel panel6 = new JPanel();
  31. private JPanel blank1 = new JPanel();
  32. private JPanel blank2 = new JPanel();
  33. private JPanel blank3 = new JPanel();
  34. private final int WINDOW_WIDTH = 500; // Window width
  35. private final int WINDOW_HEIGHT = 300; // Window height
  36.  
  37. ArrayList<T> newlist = new ArrayList<T>();
  38.  
  39. public MyList()
  40. {
  41. // Set window parameters
  42. setTitle("MyList Modified");
  43. setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  44. setLayout(new GridLayout(3,3));
  45. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.  
  47. panel1.add(numMessage);
  48. panel2.add(num1);
  49. panel2.add(num2);
  50. panel2.add(num3);
  51. runNum.addActionListener(new numListener());
  52. panel3.add(runNum);
  53.  
  54. panel4.add(strMessagePt1);
  55. panel4.add(strMessagePt2);
  56. panel5.add(str1);
  57. panel5.add(str2);
  58. panel5.add(str3);
  59. runStr.addActionListener(new strListener());
  60. panel6.add(runStr);
  61.  
  62. add(panel1);
  63. add(blank1);
  64. add(panel4);
  65. add(panel2);
  66. add(blank2);
  67. add(panel5);
  68. add(panel3);
  69. add(blank3);
  70. add(panel6);
  71.  
  72. setVisible(true);
  73. }
  74.  
  75. public void add(T thing)
  76. {
  77. newlist.add(thing);
  78. }
  79.  
  80. public T getSmallest()
  81. {
  82. T smallest = (T) newlist.get(0);
  83.  
  84. for(int i=1; i<newlist.size(); i++)
  85. {
  86. if(((Comparable<T>) newlist.get(i)).compareTo(smallest) <0)
  87. smallest = (T) newlist.get(i);
  88. }
  89. return smallest;
  90. }
  91.  
  92. public T getLargest()
  93. {
  94. T largest = (T) newlist.get(0);
  95.  
  96. for(int i=1; i<newlist.size(); i++)
  97. {
  98. if(((Comparable<T>) newlist.get(i)).compareTo(largest) > 0)
  99. largest = (T) newlist.get(i);
  100. }
  101. return largest;
  102. }
  103.  
  104. private class numListener implements ActionListener
  105. {
  106. public void actionPerformed(ActionEvent e)
  107. {
  108. Comparable number1 = Integer.parseInt(num1.getText());
  109. Comparable number2 = Integer.parseInt(num2.getText());
  110. Comparable number3 = Integer.parseInt(num3.getText());
  111. listInt.add((Integer) number1);
  112. listInt.add((Integer) number2);
  113. listInt.add((Integer) number3);
  114. Comparable max = listInt.getLargest();
  115. Comparable min = listInt.getSmallest();
  116. JOptionPane.showMessageDialog(null, "You entered "+number1+", "+
  117. number2+" & "+number3+"\n Largest Number: "+max+
  118. "\nSmallest Number: "+min);
  119. }
  120. }
  121.  
  122. private class strListener implements ActionListener
  123. {
  124. public void actionPerformed(ActionEvent e)
  125. {
  126. Comparable string1 = str1.getText();
  127. Comparable string2 = str2.getText();
  128. Comparable string3 = str3.getText();
  129. listString.add((String) string1);
  130. listString.add((String) string2);
  131. listString.add((String) string3);
  132. Comparable max = listString.getLargest();
  133. Comparable min = listString.getSmallest();
  134. JOptionPane.showMessageDialog(null, "You entered "+string1+", "+
  135. string2+" & "+string3+"\n Largest String: "+max+
  136. "\nSmallest String: "+min);
  137. }
  138. }
  139.  
  140. public static void main(String[] args)
  141. {
  142. MyList myListIntAndStr = new MyList();
  143. }
  144. }
January 18, 2013 1:51:31 PM

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/JF...

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.


m
0
l
!