4

So I am making a Text-Based RPG Applet in Java. I am using SWING panels, with text fields and buttons on it. I am programming it in Netbeans, and I am using the GUI editor. I have crafted the HTML page and it opens in the browser fine. When I click a button to switch panels (basically set the first the non-visible), the new panel doesn't load. I tried using repaint() and validate() but it just doesn't work..... Any help?

package applettest;

import javax.swing.UIManager;

public class NewApplet extends java.applet.Applet {

    startScreen ss;
    registerScreen rs;
    charactercreationScreen ccs;

    @Override
    public void init() {

        try {
            //This sets the look and feel to NIMBUS.
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    initComponents();
                    startup();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    public void startup() {
        showCharacterCreationScreenSTART();
        showRegisterScreenSTART();
        showStartScreenSTART();
    }

    public void showStartScreen() {
        setSize(410, 350);
        ss = new applettest.startScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
    }

    public void showStartScreenSTART() {
        setSize(410, 350);
        rs.setVisible(false);
        ss = new applettest.startScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        repaint();
        validate();
    }

    public void showRegisterScreen() {
        repaint();
        validate();
        ss.setVisible(false);
        setSize(400, 350);
        rs = new applettest.registerScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        repaint();
        validate();
    }

    public void showRegisterScreenSTART() {
        repaint();
        validate();
        ccs.setVisible(false);
        setSize(400, 350);
        rs = new applettest.registerScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        repaint();
        validate();
    }

    public void showCharacterCreationScreen() {
        rs.setVisible(false);
        setSize(400, 350);
        ccs = new applettest.charactercreationScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        repaint();
        validate();

    }

    public void showCharacterCreationScreenSTART() {
        setSize(400, 350);
        ccs = new applettest.charactercreationScreen(this);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
                addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));

    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Jay
  • 143
  • 2
  • 11
  • 3
    repaint works fine, but you've got a bug in your program -- where? who the heck knows since we don't see any code! – Hovercraft Full Of Eels Aug 23 '11 at 02:23
  • The code shown doesn't show much related to your problem -- except that you are using NetBeans to create your Swing GUI code -- something I recommend you don't do as it can shield you from having to learn Swing and thereby interfere with your understanding what you're doing and not doing. Better to go through the Swing tutorials and learn Swing right. – Hovercraft Full Of Eels Aug 23 '11 at 02:36
  • In each method you can see I use repaint() and validate. Let me try and edit some more, ill see if I can edit in a few minutes. – Jay Aug 23 '11 at 02:38
  • I see a ton of nearly impossible to decipher Matisse-generated code. I see methods where you may be adding component(s) to this GUI but don't seem to be removing all components. But mainly I see a debugging nightmare. – Hovercraft Full Of Eels Aug 23 '11 at 02:45
  • Rule #1 for Netbeans GUI designer: do not use `GroupLayout`. – lhballoti Aug 23 '11 at 02:49

2 Answers2

2

Sounds like you should be using CardLayout.

See also:

Community
  • 1
  • 1
mre
  • 43,520
  • 33
  • 120
  • 170
  • Code is there as requested... I don't really know how to add all the panels at once.. – Jay Aug 23 '11 at 02:30
  • 1
    @Justin, If you want any sort of robustness in your Swing application, I suggest you move away from NetBeans and actually do the coding yourself. Automating this process via a GUI builder will only hurt you. – mre Aug 23 '11 at 02:32
  • I know this, but this is a start for me. I just needed something to generate code so I don't have to type out like 213497084721384 lines just for 1 panel. – Jay Aug 23 '11 at 02:34
  • 1
    1+ for suggesting CardLayout ;) – Hovercraft Full Of Eels Aug 23 '11 at 02:38
  • 3
    @Justin: while NetBeans generates 100000 lines, we can write it in a few easy to understand lines, and you can too. I strongly urge you to move away from NetBeans-generated code. Use it as an IDE, fine as it's an excellent one, but don't let its drag-and-drop utility hinder your education. – Hovercraft Full Of Eels Aug 23 '11 at 02:40
  • Alright, I will try to do that. No one has ever explained that to. So yeah, I guess I will just use CardLayout! – Jay Aug 23 '11 at 02:45
2

I tried using repaint() and validate() but it just doesn't work

FYI, when using Swing it should be:

panel.revalidate();
panel.repaint(); // sometimes needed

although that code is generally used when adding/removing individual components from a panel. If you are swapping entire panels then CardLayout is the way to go.

Also, you should be extending JApplet, NOT Applet.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks a ton! But when I extend JApplet, I get errors! `java.lang.reflect.InvocationTargetException at java.awt.EventQueue.invokeAndWait(EventQueue.java:1242) at applettest.NewApplet.init(NewApplet.java:22) at sun.applet.AppletPanel.run(AppletPanel.java:434) at java.lang.Thread.run(Thread.java:722)` – Jay Aug 23 '11 at 03:58