Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java Frame

  • 08-01-2007 02:07PM
    #1
    Closed Accounts Posts: 488 ✭✭


    ey,

    I have a java problem, I know its probably simple but bear with me.

    I have a Jframe that I want to divide up into different panels.

    example:
    public MainFrame(){

    super("The Text System " );
    setSize(900,700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }
    The panels contain labels, buttons etc.
    example of a panel:
    public DetailPanel(){
    super();
    setPreferredSize(new Dimension(1,0));
    setVisible(true);
    //this.setBorder(new TitledBorder("Details"));

    dtxtarea.setLineWrap(true);
    dtxtarea.setWrapStyleWord(true);

    add(rptDetail);
    add(dScroll);
    add(save);
    }
    I want to keep the panels separte and just call them in the Jframe to display them.
    ie
    in the main frame do this
    DetailPanel detailsP= new DetailPanel();
    to display the panel
    I am going wrong some where.
    Any help would be great.


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    You need something like the following:

    public class MyFrame extends JFrame{


    MyFrame (){

    JPanel myPanel=new JPanel();
    //ad stuff to panel

    getContentPane().add(myPanel, BorderLayout.CENTER);

    setVisable(true);
    }
    }
    You may want to look at different Layout managers to arrange the panels nicely in the Frame also.
    The default layout for a JFrame content pane is BorderLayout so you can add panels to
    BorderLayout.CENTER
    BorderLayout.NORTH
    BorderLayout.SOUTH
    BorderLayout.EAST
    BorderLayout.WEST


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    I want to keep the panels separte and just call them in the Jframe to display them.

    Nothing stopping you creating a separate class (.java file) for each JPanel
    public class DetailPanel1 extends JPanel{
         public DetailPanel1() {
             init();
         }
    
         private void init() {
            // Setup your panel here
         }
    }
    

    The in the main class do exactly as your example

    DetailPanel1 panel1 = new DetailPanel1();


  • Closed Accounts Posts: 488 ✭✭watsgone


    Thank you kindly


Advertisement