0

I tried a lot of layout managers but none could solve my problem:

  • I want the items in a scrollPane to keep their size (preferred or minimum) and not being resized (reduced) to fit the viewport Panel. Since if it is a JTextArea, and if the text area has blank space and it is bigger then the viewport, it would reduce it so the blank text area won't be shown. I want the blank text area to be shown for appearance issues.
  • Im stacking one item after another using BoxLayout, and it seems to me that for text areas the setMinimum method fails.
  • If the text area has blank space, then the scrollbar of the ScrollPane won't appear, instead it only appears it there are no blank space left.

Any solution?

    JScrollPane materialPane = new FScrollPane();
    this.materialPaneView = new TPanel();
    this.materialPaneView.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
    this.materialPaneView.setLayout(new BoxLayout(this.materialPaneView, BoxLayout.Y_AXIS));
    materialPane.setViewportView(materialPaneView);
    materialPane.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
    for(Material mat: this.unit.getMaterial()){
        this.addMaterial(mat);

    }

    centerPanel.add(sectionPane);
    centerPanel.add(exercisePane);
    centerPanel.add(materialPane);
    this.add(upperPanel, BorderLayout.NORTH);
    this.add(centerPanel, BorderLayout.CENTER);

public void addMaterial(Material mat){
    JTextField matName = new JTextField(30);
    JPanel fieldButtonPanel = new TPanel();
    fieldButtonPanel.setLayout(new GridLayout(1,2));
    JPanel fieldPanel = new TPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel deleteMatButtonPanel = new TPanel(new FlowLayout(FlowLayout.RIGHT));
    matName.setText(mat.getName());
    matName.setMaximumSize(new Dimension(FFont.def.getSize()*20, 30));
    fieldPanel.add(matName);
    JButton deleteMat = new JButton("Delete Material");
    deleteMatButtonPanel.add(deleteMat);

    fieldButtonPanel.add(fieldPanel);
    fieldButtonPanel.add(deleteMatButtonPanel);
    fieldButtonPanel.setAlignmentX(LEFT_ALIGNMENT);

    JTextArea matText = new FTextArea(mat.getDesc(), (int)(WIDTH*0.95), (int)(HEIGHT/3.4));
    matText.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.5)));
    /*matText.setMaximumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.4)));*/
    matText.setText(mat.getDesc());
    matText.setAlignmentX(LEFT_ALIGNMENT);
    this.materialPaneView.add(fieldButtonPanel);

    this.materialPaneView.add(matText);
    matName.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            mat.setName(matName.getText());

        }
    });

HEIGHT and WIDTH are constants, and TPanel FScrollPane are my predefined transparent panels. The BoxLayout panel is the viewport of a scrollPane, and still, it would resize the text areas.

hzzhyj
  • 3
  • 4

1 Answers1

1

I am not sure i get what you are asking for so please tell me if i totally missed the point...

As far as i know the Viewport size is controlled by the component inside the JScrollPane and the JScrollPane size wont change no matter what happens to the viewport.

You either want to:

A) Resize the JScrollPane to the same size as it's content.

I would implement listeners to look for the content size change and resize the ScrollPane accordingly but you need to pay attention to resize the whole Hierarchy too.

B) You want to resize the viewport so that it fits in the JScrollPane? Y'know without scrollbars.

I had this problem and fixed it by using a ScrollablePanel component. Check this answer, follow the link to download the .class and use it to use a JPanel that resizes to fit the ScrollPane.

Those arent very detailed answers but i will need more information about what you are trying to do before expanding on it. And your code isnt complete, always share a code that we can CTRL+C/V and readily verify the problem in our end.

BarriaKarl
  • 79
  • 7