-1

I have created a JFrame Form for Base and created JPanel for Page Replacement Purposes. I have created MenuBar with Login Button. I have created LoginPage on JPanel.

Now I want to link my LoginPage to Login Button on Base Frame. Please Help me. I have Tried a code. When I click Login Button nothing happens.

  • Login Page Name : LoginPage.java
  • Panel for Replacement : panelWelcomePanel
  • Login Button Name : menuLogin

Code:

private void menuLoginActionPerformed(java.awt.event.ActionEvent evt) {                                          
        LoginPage login = new LoginPage();
        panelWelcomePanel.removeAll();
        validate();
        panelWelcomePanel.add(login);
        validate();
    } 

Login Page Code : I am using NetBeans.

public class LoginPage extends javax.swing.JPanel {

    /**
     * Creates new form LoginPage
     */
    public LoginPage() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        labeluName = new javax.swing.JLabel();
        labeluPass = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        loginBackground = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(800, 500));
        setMinimumSize(new java.awt.Dimension(800, 500));
        setLayout(null);

        labeluName.setFont(new java.awt.Font("Serif", 3, 24)); // NOI18N
        labeluName.setForeground(new java.awt.Color(204, 204, 255));
        labeluName.setText("UserName :");
        add(labeluName);
        labeluName.setBounds(250, 200, 120, 40);

        labeluPass.setFont(new java.awt.Font("Serif", 3, 24)); // NOI18N
        labeluPass.setForeground(new java.awt.Color(204, 204, 255));
        labeluPass.setText("Password :");
        add(labeluPass);
        labeluPass.setBounds(250, 260, 120, 50);
        add(jTextField1);
        jTextField1.setBounds(390, 200, 200, 30);
        add(jPasswordField1);
        jPasswordField1.setBounds(390, 270, 200, 30);

        btnLogin.setForeground(new java.awt.Color(51, 51, 51));
        btnLogin.setText("Login");
        add(btnLogin);
        btnLogin.setBounds(280, 390, 100, 40);

        btnReset.setForeground(new java.awt.Color(51, 51, 51));
        btnReset.setText("Reset");
        add(btnReset);
        btnReset.setBounds(450, 390, 100, 40);

        loginBackground.setIcon(new javax.swing.ImageIcon("C:\\Users\\boparai\\Desktop\\LOGIN.jpg")); // NOI18N
        loginBackground.setMaximumSize(new java.awt.Dimension(800, 500));
        loginBackground.setMinimumSize(new java.awt.Dimension(800, 500));
        loginBackground.setPreferredSize(new java.awt.Dimension(800, 500));
        add(loginBackground);
        loginBackground.setBounds(0, 0, 800, 500);
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnReset;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel labeluName;
    private javax.swing.JLabel labeluPass;
    private javax.swing.JLabel loginBackground;
    // End of variables declaration                   
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
user2804097
  • 35
  • 1
  • 1
  • 6
  • also you should call `revalidate()` instead of `validate()` and I'm not sure about this but maybe you should call that `revalidate()` at the very last, and not between the `remove(...)` and `add(...)` methods, if someone could correct me if wrong or tell me if right, would appreciate it too though – Frakcool Jul 28 '14 at 05:34
  • same result with revalidate() – user2804097 Jul 28 '14 at 05:36
  • I have posted full LoginPage code. – user2804097 Jul 28 '14 at 05:38
  • at first view I see you're using `null layout`, if you're a starter in swing it's ok for learning purposes, but if this application is for commercial purpose or if you want to improve your skills I suggest you to use [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) instead of `null layout`, that said, maybe you might want to use [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) to change between `JPanels` instead of removing and adding them manualy. – Frakcool Jul 28 '14 at 05:45
  • 1
    Also a `Card Layout` will allow you to don't use multiple `JFrames`, you might ask, "why shouldn't I use multiple `JFrames`?", well the answer is [right here](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice), please be sure to check all links I provide. Also [this one](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) about overriding `get preferred | maximum | minimum size` instead of using `set preferred | maximum | minimum size`. – Frakcool Jul 28 '14 at 05:47
  • i have changed Layouts....No Change in result. – user2804097 Jul 28 '14 at 05:53
  • 1
    please show how you changed Layouts, and for better help sooner please provide an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the issue so we can copy, paste and test your code easier, faster and better to help you in that way :) – Frakcool Jul 28 '14 at 05:55
  • @Frakcool *"null layout, if you're a starter in swing it's ok for learning purposes"* Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. - Or to put that another way - .. – Andrew Thompson Jul 28 '14 at 07:23
  • @Frakcool cont. response .. 'learning' null layouts is learning ***bad** habits.* – Andrew Thompson Jul 28 '14 at 07:24
  • 1
    @AndrewThompson I know what you mean, but learning components and layouts is sometimes a headache for beginners that's why I said that (I used to do that). But you're right about everything else, I'll avoid saying that in the future adding part of what I learned from your comment. :) – Frakcool Jul 28 '14 at 12:26

1 Answers1

2
  1. Don't use a null layout. Always use a proper layout manager.
  2. As has been already suggested, consider using a CardLayout
  3. While adding/removing child components from a parent which is already visible, invoke parent#revalidate() and parent#repaint() (in that order) after the code which removes/adds the children
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maneesh
  • 1,692
  • 1
  • 14
  • 18
  • *"Don't use a null layout. Always use a proper layout manager."* +1 (the rest is also good advice, but I was 'sold' on that alone). :) – Andrew Thompson Jul 28 '14 at 07:21