Hie everyone,

I am trying to develop a GUI through hard coding.
I encountered a challenge:

I created a Jframe and onto this I added a JPanel. I assigned thge flow layout manager of the frame to null(because i want to do the positioning of my components manually)

The program is running perfect except when i click the maximise button of the frame, the JPanel does not change its size.
With Netbeans IDE - i found that the propert is Horizontal Resizable propert. I have tried to look for the method but in vain.

NB. I am trying to develop identical things from hard coding and in drag and drop environment. so with drag and drop its easy but in hard coding how do I work it out?

Here is the code if it is helpful:

public class JPanelDemo {

    JFrame frame;
    JPanel panel;

    JPanelDemo(){
    createFrame();
    }

    public void createFrame(){
        //create the frame

    frame=new JFrame("GUI Sample");
    frame.setSize(new Dimension(750,300));
    frame.setMaximumSize(new Dimension(2147483647, 2147483647));
    frame.setVisible(true);
    //create the panel

    panel= new JPanel();
    panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.yellow, Color.black));
    panel.setAlignmentX((float) 0.5);
    panel.setAlignmentY((float) 0.5);
    panel.setDoubleBuffered(true);
    panel.setFocusable(true);
    panel.setCursor(Cursor.getDefaultCursor());
    panel.setMaximumSize(new Dimension(32767, 32767));
    panel.setSize(new Dimension(732, 146));
    panel.size();
    frame.setLayout(null);
    frame.add(panel);

    }

public static void main(String [] args){

JPanelDemo jpd= new JPanelDemo();
}
}

This is the downside of not using a layout manager!
Try adding a listener for the resize events and resetting the necessary bounds in the event handler.

Hie James,

Thanks for headsup. I have resorted to BorderLayout manager. Now I have assigned my JPanel to the NORHT region of my JFrame. However, the Panel is not taking the width that i have assigned. Is there a way of resizing it?

Here is the code if it Helps:

public class JPanelDemo {

    JFrame frame;
    JPanel panel;
    GridBagConstraints cons = new GridBagConstraints();

    JPanelDemo() {
        createFrame();
    }

    public void createFrame() {
        //create the frame

        frame = new JFrame("GUI Sample");
        frame.setSize(new Dimension(750, 300));
        frame.setMaximumSize(new Dimension(2147483647, 2147483647));
        frame.setVisible(true);
    //create the panel

        panel = new JPanel();
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.yellow, Color.black));
        panel.setCursor(Cursor.getDefaultCursor());
        panel.setMaximumSize(new Dimension(32767, 32767));
        panel.setSize(new Dimension(732, 146));

        BorderLayout layout= new BorderLayout();
        layout.setHgap(3);
        layout.setVgap(10);

        frame.setLayout(layout);
        frame.add(panel,BorderLayout.NORTH);
      frame.add(new JButton("button"));
    }

    public static void main(String[] args) {

        JPanelDemo jpd = new JPanelDemo();
    }
}

setSize rarely has any effect with a layout manager. setMinimumSize and setPreferredSize are recognised by most layout managers unless they conflict with the layout manager's own rules.
The simple layout managers are really just for simple cases; if you know GridBagLayout then you can use that to force almost any kind of fitting you want.

Hie James,

Thanks for the ideas. I used setPreferredSize()and revalidate() they worked perfect. I will still try to figure out how to make it with GridBagLayout like you said.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.