Closed Thread
Results 1 to 6 of 6

Thread: cant understand some part of this code ..~

  1. #1
    R3.RyozKidz Guest

    cant understand some part of this code ..~

    Code:
    import java.awt.GridLayout;
    import java.awt.Container;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    
    public class GridLayoutFrame extends JFrame implements ActionListener
    {
        private JButton buttons[];
        private final String names[] = { "one" , "two" , "three" , "four" , "five" , "six"};
        private boolean toggle = true;
        private Container container;
        private GridLayout gridLayout1;
        private GridLayout gridLayout2;
        
        public GridLayoutFrame()
        {
            super("GridLayout Demo");
            gridLayout1 = new GridLayout(2 , 3 , 5 , 5);
            gridLayout2 = new GridLayout(3 , 2 );
            container = getContentPane();
            setLayout(gridLayout1);
            buttons = new JButton[names.length];
            
            for(int count = 0 ; count < names.length ; count++)
            {
                buttons[count] = new JButton(names[count]);
                buttons[count].addActionListener(this);
                add(buttons[count]);
            }
        }
        
        public void actionPerformed(ActionEvent event)
        {
            if(toggle)
            {
                container.setLayout(gridLayout2);
            }
            else
            {
                container.setLayout(gridLayout1);
            }
            
            toggle = !toggle;
            container.validate();
        }
    }

    (1) the usage of container = getContentPane();
    (2) the "this" at buttons[count].addActionListener(this) is refer to ..?

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: cant understand some part of this code ..~

    You give the instruction to add an actionlistener. Now you must tell it where the actionlistener is. By using "this" here you tell Java it can find the actionlistener in the class you're currently in. So here you tell Java it can find the actionlistener in"GridLayoutFrame". Java can find the actionperformed method itself from there.

  4. #3
    denarced is offline Programmer
    Join Date
    Jul 2008
    Location
    Joensuu, Finland
    Posts
    182
    Rep Power
    0

    Re: cant understand some part of this code ..~

    Quote Originally Posted by R3.RyozKidz View Post
    (1) the usage of container = getContentPane();
    You don't add graphical stuff directly on JFrame in Java. You use root panes.
    Check out these:
    How to Use Root Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  5. #4
    R3.RyozKidz Guest

    Re: cant understand some part of this code ..~

    actually the "this" will ask java to find an appropriate "stuff" to match with the current condition ..?

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: cant understand some part of this code ..~

    this is a reference to the class/object you're using it in.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  7. #6
    R3.RyozKidz Guest

    Re: cant understand some part of this code ..~

    so in this case , the "this" im passing is the reference to an object of interface ( ActionListener ) ... ??

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. YASM Can anyone help me understand this asm code (It's short)
    By Renato_Motta in forum Assembly
    Replies: 9
    Last Post: 02-15-2011, 04:30 PM
  2. Need to understand the part of the code
    By saumya in forum C and C++
    Replies: 5
    Last Post: 11-21-2010, 07:37 AM
  3. Replies: 0
    Last Post: 07-26-2010, 03:10 PM
  4. I dont understand this parser code
    By Salrandin in forum C and C++
    Replies: 3
    Last Post: 10-23-2007, 09:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts