views:

21

answers:

2

Hi all,

I'm creating a small crypto app for the desktop using java.

I'm using JFrames (import javax.swing.JFrame) with Oracle JDeveloper 11g under Linux.

I want to have a "welcome" form/frame where users can choose their encryption method, and then on choosing the method, I want to dynamically create the appropriate form for the chosen encryption method and also destroy/free/dispose() of the welcome form. When the user has finished their encrypting, they should close the frame/form (either by clicking on the x at the top right - or using the Exit button or by any method) and the welcome frame should be dynamically recreated and appear.

I've tried various things - btnEncode_actionPerformed(ActionEvent e) then this.dispose() - and I've fiddled with this_windowClosed(WindowEvent e) and dispose(), but nothing seems to work.

Even a workaround using setVisibl(true/false) would be acceptable at this stage - this has been wrecking my head all day. It's very easy to do in Delphi!

TIA and rgs,

Paul...

A: 

something like this usually does the trick: (Note I haven't tested this)

public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
    this.dispose();
    SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}

where btnContinue is the Continue button on your welcome form and JFrameAppropriateWindow is the next frame you would like to show depending on the user's choice. Args are any arguments you need to pass.

When you are ready, you can simply dispose the current frame and relaunch an instance of WelcomeMsg

npinti
A: 

I put together this simple example for creating and displaying a panel depending on a user choice.

public class Window extends JFrame {

public Window() {
    this.setLayout(new BorderLayout());
    JComboBox encryptionCombobox = new JComboBox();
    encryptionCombobox.addItem("foo");
    encryptionCombobox.addItem("bar");
    //...
    encryptionCombobox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // find choices and the correct panel
            JPanel formPanel = new JPanel();
            formPanel.setOpaque(true);
            formPanel.setBackground(Color.RED);
            //...
            Window.this.add(formPanel, BorderLayout.CENTER);
            Window.this.validate();
            Window.this.repaint();
        }
    });
    add(encryptionCombobox, BorderLayout.NORTH);
}

public static void main(String[] args) {
    new Window().setVisible(true);
}
}

When I come to think about it, you should probably use a CardLayout instead, which allows you to switch between different panels (cards).

Avall