1

I want to use 2 panel in the same frame. But button is not working? How do i do that? I want to put couple of buttons in one panel and other panel will do some other stuff.

public class TestingPage extends JFrame {

    JFrame frame=new JFrame();
    JPanel panel01;
    JPanel panel02;
    JButton bttn1;


    /**
     * @param args
     */
    public TestingPage(){
        super("Test");
        setBounds(700,700,650,500);
        setVisible(true);
        setLayout(new BorderLayout());
        Container cont=frame.getContentPane();

        panel01=new JPanel();
        panel02=new JPanel();
        cont.add(panel01,BorderLayout.EAST);
        cont.add(panel02,BorderLayout.WEST);

    //setLayout(new BorderLayout());

    bttn1=new JButton("Button");
    bttn1.setBounds(77, 75, 100,26);
    add(bttn1);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
      Runnable guiCreator= new Runnable(){
      @Override
      public void run  (){
          TestingPage page=new TestingPage();  
      }
     };
    javax.swing.SwingUtilities.invokeLater(guiCreator); 
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Asus HP
  • 51
  • 2
  • 2
  • 4

1 Answers1

3

You have three problems...

  • You're not adding the button to any of the panels, but to the frame itself
  • You're calling setVisible before you've finished creating the UI. This is well know common problem. If you need to add content to the frame after it is set visible, you will need to call revalidate to ensure that the layout is updated
  • You're class extends JFrame, but you create another JFrame and use it's content pane to add your components to, but make your TestingPage visible...

enter image description here

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestingPage extends JFrame {

//    JFrame frame = new JFrame();
    JPanel panel01;
    JPanel panel02;
    JButton bttn1;

    public TestingPage() {
        super("Test");
        setBounds(700, 700, 650, 500);
        setLayout(new BorderLayout());
        Container cont = getContentPane();

        panel01 = new JPanel();
        panel02 = new JPanel();
        cont.add(panel01, BorderLayout.EAST);
        cont.add(panel02, BorderLayout.WEST);

        //setLayout(new BorderLayout());

        bttn1 = new JButton("Button");
        panel01.add(bttn1);
        setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Runnable guiCreator = new Runnable() {
            @Override
            public void run() {
                TestingPage page = new TestingPage();
            }
        };
        javax.swing.SwingUtilities.invokeLater(guiCreator);
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks. One more thing- how do I fixed the position of any button? – Asus HP May 11 '13 at 02:32
  • Generally, I wouldn't. The problem is, Java is a cross platform language, meaning that it is likely when its run on another platform, it will not look exactly the same (differences in font sizes and DPI), meaning that any absolute positioning/sizing of components will not be right. Instead, take advantage of the layout managers... – MadProgrammer May 11 '13 at 02:36
  • Take a look at [this](http://stackoverflow.com/questions/14290020/dynamically-growing-jpanel-with-boxlayout-on-a-null-layout/14290582#14290582) and [this](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601). They both make use of custom layout managers to take the place of `null` layouts. They take advantage of the in built work flow and APIs of Swing making easier to keep up to date with the changes the API might ask of you... – MadProgrammer May 11 '13 at 02:42