I'm back to start again with GBL. Application start-up layout is as desired. However as soon as I draw upon "main page view panel" with paintComponent() page view panel does overlay thumbnail panel on the left.

Code for main panel to group up 3 main components

public class RottrMainPanel extends JPanel{

    private PdfManager pdfManager;

    public RottrMainPanel(){
        super();
        pdfManager = new PdfManager();
        setPanel();
        setBackground(Color.WHITE);
    }

    private void setPanel(){
        Dimension dim = new Dimensions().getMaxDimension();
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridx = 0;
        gbc.gridy = 0;
        RottrToolBar rottrToolBar = new RottrToolBar(pdfManager);
        add(rottrToolBar, gbc);

        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.ipadx = 185;
        gbc.gridx = 0;
        gbc.gridy = 1;
        RottrThumbnailsPanel rottrThumbnailsPanel = new RottrThumbnailsPanel(pdfManager);
        JScrollPane jspThumbnails = new JScrollPane(rottrThumbnailsPanel,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(jspThumbnails, gbc);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        RottrPageViewPanel rottrPageViewPanel = new RottrPageViewPanel(pdfManager);
        JScrollPane jspPageView = new JScrollPane(rottrPageViewPanel,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(jspPageView, gbc);


        dim = new Dimension((int)dim.getWidth()-10, (int)dim.getHeight()-10);
        setPreferredSize(dim);
        setMaximumSize(dim);
    }
}


and drawing on main view panel ( I do not think issue is here)

public void paintComponent(Graphics g) {
        //super.paintComponent(g);
        if (isDrawingAllowed) {
            g.drawImage(img, 0, 0, this);
        }
    }

    public void fileToOpen() {
        isDrawingAllowed = true;
        Pdf2Image pdf2Image = new Pdf2Image();
        img = pdf2Image.getPage(1, pdfManager.getPdfFile().toString());
        repaint();
    }
Attachments gbl_overlap.png 38.2 KB

peter_budo,

As per code snippet, components are arranged in following layout

x:       0       1
y |-----------|------------|
0 |ToolBox | Thumb    |
   |---------- |------------|
1 |             |Rottr        |
   |------------------------|

Nope
gbc.gridx = 0;
gbc.gridy = 0;
RottrToolBar rottrToolBar = new RottrToolBar(pdfManager);

gbc.gridx = 0;
gbc.gridy = 1;
RottrThumbnailsPanel rottrThumbnailsPanel = new RottrThumbnailsPanel(pdfManager);

gbc.gridx = 1;
gbc.gridy = 1;
RottrPageViewPanel rottrPageViewPanel = new RottrPageViewPanel(pdfManager);

Reset gridwidth for second row.

OK, and how I'm gone achieve width of that cell to be specific size?

You have to set width of a component which was placed into that cell. Do not use GridBagLayout to construct top level gui layout for your application. Use BorderLayout, GroupLayout, or BoxLayout.

A sample for review.

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

public class P1 extends JFrame {
   public P1() {
      Tool a1=new Tool();
      Thumb a2=new Thumb(); 
      Rott a3=new Rott();
      GridBagLayout g=new GridBagLayout();
      GridBagConstraints gc=new GridBagConstraints();
      
      gc.gridwidth=gc.REMAINDER;
      gc.fill=gc.HORIZONTAL;
      gc.anchor=gc.NORTHWEST;
 
      getContentPane().setLayout(g);
      getContentPane().add(a1,gc);
      gc.gridheight=gc.REMAINDER; 
      gc.gridwidth=gc.RELATIVE;      

      gc.fill=gc.VERTICAL;
      gc.anchor=gc.NORTHWEST;

      gc.weighty=1.0;
      getContentPane().add(a2,gc);
      gc.gridwidth=gc.REMAINDER;
      gc.fill=gc.BOTH;
      gc.anchor=gc.NORTHWEST;
      getContentPane().add(a3,gc);
   
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      pack();
      setVisible(true);
   }
   public static void main(String []args) {
         new P1();
   }
}
class Tool extends JPanel{
   public Tool()    {
       setBackground(Color.RED);
       for(int i=0;i<=10;i++)
           add(new JButton("OK"));
     }
}
class Thumb extends JPanel{
   public Thumb()    {
       setBackground(Color.YELLOW);
for(int i=0;i<=2;i++)
           add(new JButton("OK"));
     }
}
class Rott extends JPanel{
   public Rott()    {
       setBackground(Color.BLUE);
for(int i=0;i<=3;i++)
           add(new JButton("OK"));
     }
}

It was original frame display mode that was playing havoc with my layout. Setting frame size based on Toolkit.getDefaultToolkit().getScreenSize(); doesn't put frame in max mod.
This was enforced by

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
setMaximizedBounds(e.getMaximumWindowBounds());
setSize(e.getMaximumWindowBounds().width,e.getMaximumWindowBounds().height);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Now frame layout is what I wanted from the start. Unfortunately resize have to be disabled as any change will mess up again. I'm not sure why this does happens....

This article has been dead for over six months. Start a new discussion instead.