|
Re: closing a window
|
Posted: Nov 30, 2002 5:31 AM
|
|
Syd,
Without spending a lot of time going through your code, have you tried using the remove() method in place of add() where you opened the help window? I also am new to Java and had a similar instance where I wanted to remove a JLabel and JText box. I used the remove() method where I had used the add() method previously. Don't forget to addActionListener to your button and I found that if (source == button) could not be nested within another if (source == however you open your help window).
Hope that makes sense and is applicable to your situation. If not, send me the snippet of code that builds your "help window".
Derrell Jenkins > I am knew at JAVA > I have set up a menu > I open a help window from the menu > I have added a button to the help window and on pressing > it I want to exit only that window not the whole program > > I have added my program below > > > import javax.swing.*; > import java.awt.*; > import javax.swing.JScrollPane; > import javax.swing.JFrame; > import java.awt.event.*; > import javax.swing.border.*; > import java.io.*; > import java.lang.reflect.*; > import java.util.*; > > public class ChooseTestArray extends JFrame { > public static final JCheckBox[] checkboxes = new > JCheckBox[] > { > new JCheckBox("Contact between hot & cold tubes"), > new JCheckBox("Radial and circumferential > ntial conduction"), > new JCheckBox("Hot pin radiating to cooler > ooler enclosure"), > new JCheckBox("Failure(burst) of hot pressurized > rized tube"), > new JCheckBox("Non-uniform PT Deformation Outside > tside Surface Temps Used"), > new JCheckBox("Acceleration of stagnant water, > ater, K=1,3,5"), > new JCheckBox("Area change DP with reversible > sible losses,without, and losses removed"), > new JCheckBox("Water falling from upper pipes to > es to lower pipes"), > new JCheckBox("Countercurrent vertical flooding > flooding limit"), > new JCheckBox("Countercurrent steam-water flow in an > in an upright 90 elbow"), > new JCheckBox("Edwards Blowdown Experiment"), > new JCheckBox("Numerical Methods Test: Pure Single > ingle Phase Water Hammer"), > new JCheckBox("Single Phase Water Hammer Valve Slam > Slam Case for 7-Sisters Water Rig"), > new JCheckBox("Elastic pipe deformation: Water > Water Hammer"), > new JCheckBox("TEE-junction Noncondensable Standard > ndard Test Problems"), > new JCheckBox("Stern CHF tests - case 1776 - 3 > 1776 - 3 percent crept pressure-tube - two phase tests"), > new JCheckBox("28-element bundle oxidized in > dized in superheated steam conditions"), > new JCheckBox("7-element bundle oxidized in > ed in superheated steam conditions"), > new JCheckBox("LASH Test #520, double-injection, > tion, double-break, refill"), > new JCheckBox("Diakoptic solution test - LASH Test > Test #520, double-injection, double-break, refill"), > new JCheckBox("RD-14 BO3 Steam Generator Blowdown > wdown TEST B8522"), > new JCheckBox("MAPLE-X10 Single-pin experiment > iment #393"), > new JCheckBox("Test Tank filling from bottom"), > new JCheckBox("Outsurge of a pressurized tank"), > new JCheckBox("CWIT 1097-Single Channel (bottom) > ttom) Feeder Refill"), > new JCheckBox("RD-14M IDEALIZATION - DARLINGTON ECI, > ECI, TRANS. TEST B9401"), > new JCheckBox("RD-14 Test B8603, 7 mm Inlet Header > eader Break,pumps on, ECI, no surge tank"), > new JCheckBox("RD-14M Test B9001 - 7mm break, no > k, no ECI, 4MW/pass, 27 l/s, 4.5 MPa"), > new JCheckBox("POINT LEPREAU CANDU6 - 5% RIH BREAK > BREAK w/o ecc - CASE 5"), > new JCheckBox("NRU135 Idealization- no > alization- no Non-condensables- PCS, NECC and PWS , > kinetics") > }; > > > public ChooseTestArray(String title){ > /*********************************Add a > menu*****************************/ > JMenuBar mb = new JMenuBar(); > this.setJMenuBar(mb); > > JMenu fileMenu = new JMenu("File"); > mb.add(fileMenu); > > JMenuItem printItem = new JMenuItem("Print..."); > JMenuItem cleanItem = new JMenuItem("Clean..."); > //JMenuItem closeItem = new JMenuItem("Close"); > fileMenu.add(printItem); > fileMenu.add(cleanItem); > //fileMenu.add(closeItem); > > fileMenu.addSeparator(); > //print all the eps files of each test > printItem.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > new FileOpen("C:\\AcceptSuite\\print.bat"); > System.exit(0); > } > }); > //clean all the files generated in each test > cleanItem.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > new FileOpen("C:\\AcceptSuite\\clean.bat"); > System.exit(0); > } > }); > > JMenu helpMenu = new JMenu("Help"); > mb.add(helpMenu); > > JMenuItem contentsItem = new JMenuItem("Contents"); > JMenuItem searchItem = new JMenuItem("Search"); > helpMenu.add(contentsItem); > helpMenu.add(searchItem); > > helpMenu.addSeparator(); > //open readme.txt > contentsItem.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > /***********************Readme File > Window********************************/ > final JFrame f = new JFrame("Readme File"); > JPanel bP = new JPanel(); > > f.getContentPane().add(bP,BorderLayout.SOUTH); > bP.setBackground(Color.white); > JButton cl = new JButton("CLOSE WINDOW"); > bP.add(cl); > > //f.setDefaultCloseOperation(3); > /****************************close help > window********************/ > cl.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > //f.setDefaultCloseOperation(3); > f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CL > SE); > f.addWindowListener(new WindowAdapter() { > public void windowClosed(WindowEvent e) { > System.out.println("CLOSED"); > e.getWindow().dispose();} > }); > } > }); > JTextArea read = new JTextArea(10,10); > read.setFont(new Font("Times New Roman", Font.BOLD, > , 14)); > read.setForeground(Color.black); > read.setBackground(Color.white); > read.setText("Use of the Acceptance Suite\n" + > > " Saying YES activated all the check boxes and pressing > start will run\n " + > " the full acceptance suite\n" + > > " If you said NO you are now free to activate any number > of test you wish\n " + > " Then press start to begin the runs\n" + > > "Print under the file menu heading will print out the > graphs produced by the gnu\n" + > "scripts contained in each sub-directory after CATHENA has > completed\n" + > "Clean under the file menu heading will delete all files > produced by CATHENA.\n" + > > "If problems occur contact Syd Jones"); > > read.setTabSize(4); > f.getContentPane().add(read, "Center"); > f.setSize(500, 300); > f.setVisible(true); > > } > }); > JMenuItem aboutItem = new JMenuItem("About Acceptance > e Suite..."); > helpMenu.add(aboutItem); > > /********************************* checkBox > Panel*****************************/ > JPanel checkPanel = new JPanel(); > Border bdr = BorderFactory.createEtchedBorder(); > > > checkPanel.setBorder(BorderFactory.createTitledBorder(bdr, > CATHENA Acceptance Suite", > TitledBorder.TOP, > TitledBorder.CENTER)); > > checkPanel.setLayout(new BoxLayout(checkPanel, > BoxLayout.Y_AXIS)); > > > this.getContentPane().add(checkPanel,"Center"); > JScrollPane p = new JScrollPane(checkPanel); > this.getContentPane().add(p, BorderLayout.CENTER); > > for(int i=0; i < checkboxes.length; ++i) { > checkPanel.add(checkboxes);} > > /******** Main Button panel to start or cancel run > *****************************/ > JPanel buttonPanel = new JPanel(); > this.getContentPane().add(buttonPanel,BorderLayout.SOUTH); > > > JButton strt = new JButton("START"); > buttonPanel.add(strt); > JButton cnl = new JButton("CANCEL"); > buttonPanel.add(cnl); > rootPane.setDefaultButton(strt); > > strt.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > System.out.println("Start Run"); > int[]index={}; > index = (int[])goodArrayGrow(index); > int j = 0; > > for(int i=0; i < checkboxes.length; ++i){ > if (checkboxes.isSelected()){ > System.out.println("True"+i); > index[j] = i; > System.out.println(j); > index = (int[])goodArrayGrow(index); > j++; > } > } > FileArray(index); > System.exit(0); > } > }); > cnl.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent evt) { > System.out.println("CANCEL pressed"); > System.exit(0); > } > }); > > /******************** Text Panel > ********************************************/ > > JPanel textPanel = new JPanel(); > textPanel.setForeground(Color.black); > > textPanel.setBorder(BorderFactory.createTitledBorder(bdr," > nstructions", > TitledBorder.TOP, > TitledBorder.CENTER)); > textPanel.setBackground(Color.white); > this.getContentPane().add(textPanel,BorderLayout.WEST); > JTextArea ta = new JTextArea(10,15); > ta.setFont(new Font("Times New Roman", Font.BOLD, > D, 14)); > ta.setForeground(Color.black); > ta.setBackground(Color.white); > textPanel.add(ta); > ta.setText(""+"Check the Readme file under \n the Help > lp menu for " + > ""+"instructions\n on how to use this program"); > ta.setTabSize(4); > /****************** Run total suite ? > ******************************************/ > > > JFrame nf = new JFrame("CATHENA Acceptance > ptance Suite"); > JOptionPane pane = new JOptionPane("Would you like to > o run the entire Suite?:", > JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION,nu > l); > JDialog d = pane.createDialog(nf, "CATHENA > CATHENA Acceptance Suite"); > d.setVisible(true); > > Integer returnValue = (Integer)pane.getValue(); > System.out.println("Return value is " + > is " + returnValue); > > if (returnValue.intValue() == > ) == JOptionPane.YES_OPTION){ > for(int i=0; i < checkboxes.length; ++i) > checkboxes.setSelected(true);} > > > > } > > public void FileArray (int [] index){ > FileOpen run = new FileOpen(); > for (int j=0; j < index.length-1; j++) > System.out.println("FileArray" + index[j]); > String Path = ("C:\\AcceptanceTestSuite"); > String[]fp = new String[]{ > (Path + "\\test1\\condss.bat"), > (Path + "\\test2\\condrc.bat"), > (Path + "\\test3\\rad.bat"), > (Path + "\\test4\\fcd1d.bat"), > (Path + "\\test5\\s14r2.bat"), > (Path + "\\test6\\momnt.bat"), > (Path + "\\test7\\arch.bat"), > (Path + "\\test8\\massc.bat"), > (Path + "\\test9\\flood.bat"), > (Path + "\\test10\\el90t11.bat"), > (Path + "\\test11\\edbldexp.bat"), > (Path + "\\test12\\instant_slam.bat"), > (Path + "\\test13\\fc02.bat"), > (Path + "\\test14\\rubpipe.bat"), > (Path + "\\test15\\teejunc.bat"), > (Path + "\\test16\\c1776.bat"), > (Path + "\\test17\\cs281.bat"), > (Path + "\\test18\\cs77.bat"), > (Path + "\\test19\\lash.bat"), > (Path + "\\test20\\diakop.bat"), > (Path + "\\test21\\sgen.bat"), > (Path + "\\test22\\maple.bat"), > (Path + "\\test23\\tankfil.bat"), > (Path + "\\test24\\gentnk.bat"), > (Path + "\\test25\\cwit.bat"), > (Path + "\\test26\\b0113.bat"), > (Path + "\\test27\\b8603.bat"), > (Path + "\\test28\\b9001.bat"), > (Path + "\\test29\\ptlpr.bat"), > (Path + "\\test30\\NRU.bat"), > }; > for (int j=0; j < index.length-1; j++){ > System.out.println(fp[index[j]]); > new FileOpen(fp[index[j]]);} > > } > > /****************************Increase Array size by > one***********************/ > static Object goodArrayGrow(Object a) > { > Class cl = a.getClass(); > if (!cl.isArray()) return null; > Class componentType = cl.getComponentType(); > int length = Array.getLength(a); > int newLength = length * 11 / 10 + 1; > > Object newArray = > Array = Array.newInstance(componentType, > newLength); > System.arraycopy(a, 0, newArray, 0, length); > return newArray; > } > /****************************MAIN > METHOD*************************************/ > > public static void main(String[] args) { > JFrame f = new ChooseTestArray("CATHENA Acceptance > ce Suite"); > f.setBounds(40, 50, 800, 600); > f.setVisible(true); > //f.addWindowListener(new WindowAdapter() { > // public void windowClosing(WindowEvent e) { > // System.exit(0); > // } > //}); > > } > > } > /************************Run each tests BAT > File*******************************/ > class FileOpen { > public FileOpen(){} > public FileOpen(String numRows) { > > try { > String line; > Process p = Runtime.getRuntime().exec(numRows); > BufferedReader input = new BufferedReader(new > ew InputStreamReader(p.getInputStream())); > while ((line = input.readLine()) != null) { > System.out.println(line); > } > input.close(); > } > catch (Exception err) { > err.printStackTrace(); > } > } > } > /********************************************************** > *****************/
|
|