Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using a button to change the interface

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using a button to change the interface

    The idea is there are 5 buttons on the top of the screen. Every time you press one it removes the old menu and produces a new one. To me, my code looks fine. The problem is when i click on "Add CD" nothing happens, but then if i manually resize the window (moving my mouse to the edge of the window and changing the size of it) the new menu pops up... Can anyone give me advice on how to fix this problem... Thanks, the whole runnable code is below

    package ui;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
     
     
     
    public class mainInterface extends JFrame implements ActionListener
    {
     
        // Variables declaration - do not modify
        private JButton jButton1;
        private JButton jButton2;
        private JButton jButton3;
        private JButton jButton4;
        private JButton jButton5;
        private JButton jButton6;
        private JPanel jPanel1;
     
        private JTextField text1;
        private JTextField text2;
        private JTextField text3;
        private JTextField text4;
        private JTextField text5;
        private JTextField text6;
        private JTextField text7;
        private JTextField text8;
     
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        private JLabel label6;
        private JLabel label7;
        private JLabel label8;
     
        private JPanel bottomPanel;
        private JPanel topPanel;               //  declaring panels
        private JPanel holdAll;
     
        private JPanel one;
        private JPanel two;
        private JPanel three;
        private JPanel four;
     
    //----------------------------------------------------------
     
    //----------------------------------------------------------
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    mainInterface myApplication = new mainInterface();
     
                    myApplication.setLocation(100, 100);                  
     
                    myApplication.setSize(700, 400);                       
     
                    myApplication.setTitle("Kevin's Jukebox");
     
                    myApplication.setVisible(true);
     
     
                }
            });
        }
    //----------------------------------------------------------
     
    //----------------------------------------------------------
        /** Creates new form mainInterface */
        public mainInterface()
        {
     
            jButton1 = new JButton("Add CD");
            jButton2 = new JButton("Add Video");
            jButton3 = new JButton("Total Play Time");
            jButton4 = new JButton("Create Playlist");
            jButton5 = new JButton("Show Library");
            jButton6 = new JButton("Quit");
     
            topPanel = new JPanel();
            holdAll  = new JPanel();
            bottomPanel = new JPanel();
     
            one = new JPanel();
            two = new JPanel();
            three = new JPanel();
            four = new JPanel();
     
            text1 = new JTextField(15);
            label1 = new JLabel("Title: ");
            text2 = new JTextField(15);
            label2 = new JLabel("Artist: ");
            text3 = new JTextField(15);
            label3 = new JLabel("Length: ");
            text4 = new JTextField(15);
            label4 = new JLabel("Num of Tracks: ");
            label5 = new JLabel("Welcome to Kevins Jukebox");
     
            int flag = 0;
            drawApp(flag);
     
            jButton1.addActionListener(this);
            jButton2.addActionListener(this);
            jButton3.addActionListener(this);
            jButton4.addActionListener(this);                           
            jButton5.addActionListener(this);
            jButton6.addActionListener(this);
     
     
     
     
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     
        }
    //----------------------------------------------------------
     
    //----------------------------------------------------------
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == jButton1)                              
            {
     
                int flag = 1;
                drawApp(flag);
            }
        }
    //----------------------------------------------------------
     
    //----------------------------------------------------------
        public void drawApp(int flag)
        {
            topPanel.setLayout(new FlowLayout());
                topPanel.add(jButton1);
                topPanel.add(jButton2);
                topPanel.add(jButton3);
                topPanel.add(jButton4);
                topPanel.add(jButton5);
                topPanel.add(jButton6);
     
            bottomPanel.add(label5);
     
            holdAll.setLayout(new BorderLayout());
                holdAll.add(topPanel, BorderLayout.NORTH);
                holdAll.add(bottomPanel, BorderLayout.CENTER);
     
            if (flag == 0)
                bottomPanel.add(label5);
            else
                bottomPanel.remove(label5);
     
            if (flag == 1)
            {
     
                one.add(label1);
                one.add(text1);
                bottomPanel.add(one);
                two.add(label2);
                two.add(text2);
                bottomPanel.add(two);
                three.add(label3);
                three.add(text3);
                bottomPanel.add(three);
                four.add(label4);
                four.add(text4);
                bottomPanel.add(four);
     
                bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
            }
            getContentPane().add(holdAll, BorderLayout.CENTER); // places everything on the frame
     
     
        }
    //----------------------------------------------------------
     
    }


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a button to change the interface

    I found a solution... adding this.validate(); after the UI is updated... no idea why this is though... maybe someone could explain if they have any idea

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using a button to change the interface

    You cannot dynamically add components after a container has been realized and expect them to display..if you do add components, you must revalidate so the LayoutManager knows to modify the GUI.

Similar Threads

  1. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  2. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  3. Interface problem please help!!
    By joff1403 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:09 AM
  4. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
  5. how to using button to change linechart
    By NARs in forum AWT / Java Swing
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM