- I am opening a
JInternal
Frame by getting its name from JTextField and ** then creates its object at runtime**, the problem is that if I write 10 times different Internal Frame name in textBox and then click the button it opens the newJInternal
Frame everytime. - Now i want whenever a new
JInternal
Frame is open, the previous JInternalFrame should be close automatically. - I know it's pretty easy to do that but my case is difficult because of i create its object at run time, how can i do it.
My code behine the Button is following
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { String st = TextField.getText().toString(); // in TextField i enter the JInternal Frame Name String clazzname = "practice."+st; // practice is the package name try { JInternalFrame obj1 = (JInternalFrame) Class.forName( clazzname ).newInstance(); obj1.setVisible(true); jPanel1.add(obj1); // now in there i want that whenever i click the button , it check either is there any Jinternal frame is open already or not if yes then close the previously open JFrame } catch(Exception e) { System.out.println("error "+e); } }

- 3,123
- 4
- 22
- 45

- 33
- 6
1 Answers
I know it's pretty easy to do that but my case is difficult because i creats its object at run time, how can i do it.
There's nothing magical about runtime that makes this any different from how you'd normally close it. The secret is in having a reference to the JInternalFrame readily available. A solution is to use a JInternalFrame field, a non-static instance variable, to hold the reference and not use a local variable as you're currently doing. The key here is to understand that references are what matter, much more so than variables. If you need a reference variable that persists when the method ends, then the variable cannot be declared within the method but should be on class scale.
Something like:
public class MyGui {
// instance field to hold reference to currently displayed JInternalFrame
private JInternalFrame currentInternalFrame = null;
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
if (currentInternalFrame != null) {
currentInternalFrame.dispose(); // clear current one
}
String st = TextField.getText().toString(); // in TextField i enter the JInternal Frame Name
String clazzname = "practice."+st; // practice is the package name
try {
// JInternalFrame obj1 = (JInternalFrame) Class.forName( clazzname ).newInstance();
currentInternalFrame = (JInternalFrame) Class.forName( clazzname ).newInstance();
currentInternalFrame.setVisible(true);
jPanel1.add(currentInternalFrame);
} catch(Exception e) {
System.out.println("error "+e);
}
}
}
Note that this code has not been tested and is not here for a copy-and paste solution but to give you a general idea.
Another unrelated issue is on program design: users don't usually like windows opening and closing, and perhaps a better program structure for your user is to swap JPanel views via a CardLayout (please read the CardLayout Tutorial for more on this).

- 283,665
- 25
- 256
- 373
-
i am new to java it will be great if you can explain with the help of code – shaun Tait May 22 '18 at 21:11
-
@shaunTait: example given. The code has not been tested and is not there for a copy-and paste solution but to give you a general idea. The key here is to understand that references are what matter, much more so than variables. If you need a reference variable that persists when the method ends, then the variable cannot be declared within the method but should be on class scale. – Hovercraft Full Of Eels May 22 '18 at 21:15
-
@shaunTait: that comment does not give me information that allows me to help. You need to fill in the blanks. Also note that Java does not have "global" variables. – Hovercraft Full Of Eels May 22 '18 at 21:16
-
@shaunTait: understand that statements like "does not work" or "gives me an error" are not helpful to us since they don't tell us exactly what is wrong, information that your compiler and JVM are giving you and that you'll want to share with us. – Hovercraft Full Of Eels May 22 '18 at 21:17
-
i understand next time i will post my question properly. One more thing like my problem, i do hope you can understand what is my level, it will be very nice if you recommend me some thing, link or website to overcome the tiny problems like the one which i post. So next time i don't have to post such kind of stupid question. – shaun Tait May 22 '18 at 21:23
-
@shaunTait: your post wasn't bad, and better than most early posts, but do check out the [ask] section and Jon Skeet's blog post [Asking the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). He should know as he's the [top member of the StackOverflow site](https://stackoverflow.blog/2018/01/15/thanks-million-jon-skeet/). – Hovercraft Full Of Eels May 22 '18 at 21:27
-
thanks that's very kind of you one thing you forget to tell, what kind of reading material i shoud read, since i am new to java, guide me a little, because there is so much in internet, it confuses me that where to start what should i read first, what should be my path in order to be a programmer like you. & if there is any blog related to that then it will be great . – shaun Tait May 22 '18 at 21:31
-
@shaunTait: sorry but this site is for answering specific question and mentoring is far out of what we can or should do. Keep reading, keep studying, keep coding. If you want to learn fastest, take a course taught by a good instructor. – Hovercraft Full Of Eels May 22 '18 at 21:34