0

So basically I want to run this but replace ‘name’ with a Int variable

JFrame name = new JFrame();

Thank you it isn’t multiple jFrame it is one JFrame that looks different depending on the variables sent

Callum
  • 1
  • 5
  • 1
    Ah, if I understand correctly, no – MadProgrammer Aug 23 '18 at 07:44
  • Do you want to use a dynamic variable name? Please elaborate your question – fgamess Aug 23 '18 at 07:46
  • Possible duplicate of [Assigning variables with dynamic names in Java](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – Turamarth Aug 23 '18 at 07:47
  • it Will start as 0 then increase by 1 each time but it needs to be the name off JFrame – Callum Aug 23 '18 at 07:48
  • You shouldn't even need more than a single `JFrame`, so you're definitely chasing after unicorns here. – Kayaman Aug 23 '18 at 07:52
  • Please elaborate your question – Prakhar Nigam Aug 23 '18 at 07:54
  • Basically the name needs to change each time so at first the name would be 1 then it will add 1 so the next time it runs the name will be 2 so each time the jframe has a different name that is why the name needs to be a variable – Callum Aug 23 '18 at 07:58
  • And the reason for different names is that it is a universal JFrame so it is all based on variables and if I dispose of the previous one if the have the same name it will close both of them – Callum Aug 23 '18 at 07:59
  • 1
    @Callum - can you explain *why* you need multiple `JFrame`s? There is likely a better solution. – achAmháin Aug 23 '18 at 08:00
  • Basically the file reads some variables and depending on the variables shows certain buttons and labels on that jframe then once you press a button it sends you to a different class depending on the variables and that new main class file sends different variables and that JFrame repeats that process but the problem is after pressing the button the jframe opens a new jframe and disposes of the old one but if they have the same name it disposes of both off them so really it is one jframe being reused each time that is why i need a variable for the name of the JFrame – Callum Aug 23 '18 at 08:07
  • So if you can dispose of the correct jframe when both have the same name that would be the better solution – Callum Aug 23 '18 at 08:08
  • Closing a `JFrame` and opening a new one is unnecessary. You should most likely be replacing its `ContentPane`. I've commented on your game project before, but I'd like to reiterate that your design is flawed, which is why you constantly run into these situations where you try to do things that simply aren't possible, or if possible, very clumsy and brittle. – Kayaman Aug 23 '18 at 08:46
  • Ok how to I close its current ContentPane to add a new one – Callum Aug 23 '18 at 08:47
  • You don't close a contentpane. You use `JFrame.setContentPane()` to replace the contents in the frame. – Kayaman Aug 23 '18 at 08:48
  • I know it is flawed that is why I have decided to redesign everything that is what I am doing – Callum Aug 23 '18 at 08:50
  • Oh ok so I just add it to the JFrame and it will replace it – Callum Aug 23 '18 at 08:51
  • You should also do some reading https://docs.oracle.com/javase/tutorial/uiswing/components/index.html – Kayaman Aug 23 '18 at 08:57
  • Ok I will check it out – Callum Aug 23 '18 at 09:42

1 Answers1

0

You cant give dynamic variable name like that in Java. But possibility is to create a dynamic datastructure like the array list and save all you JFrames in that list.

ArrayList: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

in that List you can save Object with an Integer as index to find them later.

Donatic
  • 323
  • 1
  • 13