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: Problem In JFrame Window

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Problem In JFrame Window

    Hi All,

    /* I had created a JFrame with Jpanel having five JButtons on JPanel and their layout is set by choosing layout from menubar using of Actionlistener but problem is that effect is show after minize the window not instantly after selecting..*/

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    class LayoutManagersShow implements ActionListener
    {
    JFrame jf;
    JMenuBar jb;
    JMenu jm1,jm2,jm3;
    JMenuItem m1,m2,m3,m4,m6,m7;
    JMenuItem First,Next,Last,Previous,Desire;
    JPanel jp;
    JButton b1,b2,b3,b4,b5;
    String str1;

    LayoutManagersShow()
    {
    jf=new JFrame("LayOut Appearence");
    jb=new JMenuBar();
    jm1=new JMenu("Layout");
    jm2=new JMenu("Quit");
    jm3=new JMenu("CardLayout");
    jb.add(jm1);
    jb.add(jm2);
    jf.setJMenuBar(jb);
    m1=new JMenuItem("FlowLayout");
    m2=new JMenuItem("GridLayout");
    m3=new JMenuItem("BorderLayout");

    m4=new JMenuItem("CardLayout");
    m6=new JMenuItem("Exit");
    jm1.add(m1);
    jm1.add(m2);
    jm1.add(m3);
    jm1.addSeparator();
    jm1.add(m4);

    jm2.add(m6);
    First=new JMenuItem("First");
    Next=new JMenuItem("Next");
    Last=new JMenuItem("Last");
    Previous=new JMenuItem("Previous");
    Desire=new JMenuItem("Desire");
    jf.setLayout(new FlowLayout());
    jf.setVisible(true);
    jf.setSize(400,400);

    jp=new JPanel(new FlowLayout());
    b1=new JButton("FirstButton");
    b2=new JButton("SecondButton");
    b3=new JButton("ThirdButton");
    b4=new JButton("FourthButton");
    b5=new JButton("FifthButton");
    jp.add(b1);
    jp.add(b2);
    jp.add(b3);
    jp.add(b4);
    jp.add(b5);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    jp.setBackground(new Color((float)Math.random(),(float)Math.random(),(f loat)Math.random()));
    jp.setPreferredSize(new Dimension(250,250));
    jp.setVisible(true);
    jf.add(jp);
    m1.addActionListener(this);
    m2.addActionListener(this);
    m3.addActionListener(this);
    m4.addActionListener(this);
    m6.addActionListener(this);
    First.addActionListener(this);
    Next.addActionListener(this);
    Last.addActionListener(this);
    Desire.addActionListener(this);
    Previous.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {

    if(e.getSource()==m1)
    {
    jp.setLayout(new FlowLayout());
    }
    if(e.getSource()==m2)
    {
    jp.setLayout(new GridLayout(3,2));
    }
    if(e.getSource()==m3)
    {
    jp.setLayout(new BorderLayout());
    jp.add(b1,"North");
    jp.add(b2,"South");
    jp.add(b3,"East");
    jp.add(b4,"West");
    jp.add(b5,"Center");

    }
    if(e.getSource()==m4)
    {
    jb.add(jm3);
    jm3.add(First);
    jm3.add(Next);
    jm3.add(Previous);
    jm3.add(Desire);
    jm3.add(Last);
    CardLayout c=new CardLayout(20,20);
    jp.setLayout(c);
    jp.add(b1,"t1");
    jp.add(b2,"t2");
    jp.add(b3,"t3");
    jp.add(b4,"t4");
    jp.add(b5,"t5");
    if(e.getSource()==First)
    {
    c.first(jp);

    }
    if(e.getSource()==Next){
    c.next(jp);
    }
    if(e.getSource()==Last){
    c.last(jp);

    }
    if(e.getSource()==Previous){
    c.previous(jp);
    }
    if(e.getSource()==Desire){
    c.show(jp,"t3");
    }
    }
    if(e.getSource()==m6)
    {
    System.exit(0);
    }
    }

    public static void main(String...s)
    {
    LayoutManagersShow lms=new LayoutManagersShow();
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem In JFrame Window

    Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.

    Too much code posted improperly. Please fix it using the instructions in the link I've provided.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem In JFrame Window

    Quote Originally Posted by Suryanshi View Post
    I had created a JFrame with Jpanel having five JButtons on JPanel and their layout is set by choosing layout from menubar using of Actionlistener but problem is that effect is show after minize the window not instantly after selecting..
    First, always use the good practice to invoke aFrame.setVisible(true) only after all GUI creation/setup.
    Second, when you use JFrame always configure the "default close operation" (see setDefaultCloseOperation).
    Third, when a layout (either adding/removing components or changing layout manager) is changed into a container, the layout must be revalidated. This is generally done invoking a validate() on the container. In your case jp.validate();

    Other note: for what I see, the handling of First, Next, etc... is not appropriate into the if(e.getSource()==m4). Those handlings are distinct from the action of the m4.

    And final note: please give "good" names to variables ..... not m1, m2, jb, etc... which are a bit ugly.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Single JFrame for Dinamic Contents Window
    By Malsasa in forum AWT / Java Swing
    Replies: 5
    Last Post: February 5th, 2013, 11:06 AM
  2. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  3. Replies: 2
    Last Post: October 31st, 2011, 09:19 AM
  4. Setting JFrame to be only selectable window
    By aussiemcgr in forum AWT / Java Swing
    Replies: 3
    Last Post: July 22nd, 2010, 12:43 PM
  5. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM