1

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each one in separate .java file inside same package.

These two frames represents 2 screens (panels) of application. When Next button in first frame is clicked, it should move to second frame.

When I checked, these two classes are having main method, I think it should be correct way for creating applications. there should be only one main method.

When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?

As I'm beginner, Can somebody suggest me in how to start up with these kind of applications? what are the guidelines that need to be followed? And please help me in finding documentation related to starting up with the development of such applications.

After going through the answers, My comments are:

I used the following code to go to next panel from first panel, but didn't worked.

private void gotoNextPanel(){
//    jPanelFirstScreen.setVisible(false);
      JPanelSecondScreen jpanelSecondScreen= new JPanelSecondScreen();
      jpanelSecondScreen.setVisible(true);
      UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
      upgradeUtilityGUI.removeAll();
      validate();
      repaint();
//      upgradeUtilityGUI.add(jpanelSecondScreen);
            upgradeUtilityGUI.getContentPane().add(jpanelSecondScreen, "card2");
      jpanelSecondScreen.setVisible(true);
      validate();
      repaint();

    }

I'm using netbeans, and 've added two panels to the cardlayout of frame. And when I use the above code to change panels, Nothing is happening, the first panel is still appearing. Can somebody tell me, how to write code for moving from one panel to another when both the panels 've been added to cardlayout of jFrame ?

Jonas
  • 121,568
  • 97
  • 310
  • 388
merlachandra
  • 376
  • 2
  • 17
  • 1) *"After going through the answers, My comments are:"* Not something we will be 'notified' of.. The only way for us to know is for you to add a comment to each answer. I just returned to this to see what the score was after a recent up-vote. ;) 2) For better help sooner, post an [SSCCE](http://sscce.org/). You can see an SSCCE in my linked answer. – Andrew Thompson Apr 09 '12 at 11:55

4 Answers4

4

Use a CardLayout, as shown here (and one frame) as mentioned by others.

Game view High Scores view

When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?

Make the panels public access level and they will be available from other packages.


One problem in that code snippet is implied by the line:

UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();  

It goes out of scope before ever being added to a container. Also, their should be no need to remove anything when adding a new card to the layout, and no need to call repaint().

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • since the panels have been created using netbeans, they are set to private access levels by default and are non-editable. – merlachandra Apr 07 '12 at 10:27
  • 1
    Rubbish they are non-editable. If you cannot figure how to use your IDE, you should either ask questions on that first, or use a simpler code editor. – Andrew Thompson Apr 07 '12 at 10:29
2

If your application is as simple as having only two panels you shouldn't create two JFrames. You should create a JFrame with two JPanel each of them contains the neccessary information for you. If you are ready with your first panel you can call setVisible(false) on it, and call setVisible(true) on the 2nd frame. It is the one of the most easy-to-understand solution.
But, it only depends on you if it is good for you or you would like to use some more detailed solution.

dexametason
  • 1,133
  • 7
  • 16
  • 1
    CardLayout could be used for this purpose this can be found here http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html – krystan honour Apr 07 '12 at 10:08
  • You have right, card layout can help here! But I didn't want to go in details about layouts in my answer as the questioner is new to java GUI development. – dexametason Apr 07 '12 at 10:26
  • 1
    *"I didn't want to go in details about layouts .. as the questioner is new to java GUI development."* That is the ***best time*** to let newbies know how vital they are to GUI development. Prevent bad habits early on. – Andrew Thompson Apr 07 '12 at 11:53
1

What you should do is have a single JFrame for the application, then you add and remove JPanels as you want to move between screens.

Each of your JPanels should basically have the following... 1. A JButton called "Next" 2. A ButtonListener for each button, that tells the JFrame to load panel2, panel3, etc.

As part of the ButtonListener, you basically just want to call something like JFrame.removeAll() to remove the existing panel, then JFrame.add(JPanel) to add the next panel.

By having 1 JFrame, you also only have 1 main() method.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Thanks for your answer. In netbeans, I was selecting Jframe inside "SWING GUI forms" while creating a new class (.java) file. So, I've created two jFrames. Now after all your replies, I found an option to create JPanel form. My problem will be solved by changing the 2nd frame to JPanel... – merlachandra Apr 07 '12 at 10:45
  • Excellent, thats great to hear. If your question is answered, make sure you give a tick to the correct answer, so that others can be helped by your question/answer – wattostudios Apr 07 '12 at 10:47
  • *"then JFrame.add(JPanel) to add the next panel."* Then call `validate()`.. It is more tricky than it seems. That is why `CardLayout` is so handy. All the kinks are ironed out. ;) – Andrew Thompson Apr 07 '12 at 13:54
  • After following the answers, I'm facing some new problems. Since, I'm not able to paste more text in comment, I've edited the Question and added my comments there...Can somebody please look into the edited question and answer my questions? – merlachandra Apr 08 '12 at 17:34
1

Don't use two or more JFrames, nor with separated and compiled Jar files, this is road to the hell, better would be look at CardLayout,

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • *"nor with separated Jar file"* Huh? I typically make 'one Jar per package' to allow faster deployment for applets and JWS, and (with JWS) to allow different packages to have different security levels. – Andrew Thompson Apr 07 '12 at 10:19