-3

I want to adjust the size of my scrollbar to the size of my canvas window to be the same width as the image window.

This is what I have so far :

https://i.stack.imgur.com/5Xpxf.png

Any help will be appreciated.

I tried setPreferedsize(), I tried setVisibleAmount() but none is working.

     JScrollBar SB = new JScrollBar();


        SB.setMinimum(1);
        SB.setMaximum(Cavalieri_counting_2.img.getNSlices() + 9);
        SB.setValue(img.getCurrentSlice());
        SB.setOrientation(Adjustable.HORIZONTAL);
        //SB.setVisibleAmount(SB.getMaximum());;

        //SB.setOrientation(JScrollBar.HORIZONTAL);
        GridBagConstraints gbc_scrollBar = new GridBagConstraints();
        //gbc_scrollBar.insets = new Insets(0, 0, 5, 0);
        gbc_scrollBar.gridwidth = 20;
        gbc_scrollBar.gridx = 0;
        gbc_scrollBar.gridy = 18;
        View_Panel.add(SB, gbc_scrollBar);
        SB.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
wrise
  • 1
  • 1
  • 1
    (1-) Why are you using a JScrollBar??? The suggestion in your last question (https://stackoverflow.com/questions/54337684/is-there-a-way-to-update-the-display-window-when-the-user-loads-a-new-image) was to use a JScrollPane. – camickr Jan 25 '19 at 03:33
  • 2
    `gbc_scrollBar.gridwidth = 20;` and `gbc_scrollBar.gridy = 18;` - where did you get those numbers from? You can't just randomly chose 18 and 20 as numbers You can only use those values if you actually have 20 other components added to the panel. Read the Swing tutorial on [How to Use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for the basics and working examples. I would also read the section on `How to Use Text Areas` for a working example of using a JScrollPane. And the section on `How to Use Scroll Panes` explains how a scroll pane works. – camickr Jan 25 '19 at 03:38
  • 1
    And you still haven't posted a proper [mcve] as was suggested in your last question. – camickr Jan 25 '19 at 03:44

1 Answers1

0

Firstly - GridBag layout does not work by setting pixel coordinates but instead is constructed proportional to other objects on the screen. See this link.

In particular it is important to understand this:

In the GridBagLayout is that there’s no way to specify the size of the grid. There doesn’t have to be. The grid size is determined implicitly by the constraints of all the objects;

An example can be seen in this image. Each 'location' on the screen is actually a relative grid position.

enter image description here

Secondly - Your question is oddly worded but as I understand you want to change the length of the scroll bar so it covers more of the screen. In order to change the properties of a scroll bar it is important to change ScrollBar.setPreferedSize(). See JScrollBars.

This is the correct method to change. If setPreferedSize() is not working then you have clearly not posted all the code. Trying checking you are not overriding it somewhere in your program (using setMaximumSize() or by setting the size again somewhere else accidentally).

If you are not overriding it then you potentially are encountering an error with your layout manager. In your other question you seem to be manually setting your row and column sizes. This could be overriding the size of your components:

GridBagLayout gbl_ViewPane = new GridBagLayout();
gbl_ViewPane.columnWidths = new int[]{0, 0, 0, 0, 0, 222, 0};
gbl_ViewPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_ViewPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_ViewPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
ViewPane.setLayout(gbl_ViewPane);

Alternatively your layout manager may just be not implemented correctly or you may not be pack()ing your scrollbar properly. If you test your code in a more isolated manner you will find that it may be a bug in your code causing the error.

Also it is important to note it is bad practice to set size manually.

JFreeman
  • 974
  • 1
  • 10
  • 26