Limit JScrollPane to only show part of its content - java

I have a JScrollPane that contains a JPanel of size 5000x5000 pixels. I want to limit the JScrollPane to only be able to show a subpart of this JPanel.
For example the rectangle defined by the two points (X,Y):
(500,500) (upper left corner)
(3000,3000) (lower right corner).
I have tried
myJScrollPane.getHorizontalScrollBar().setMinimum(500);
and
myJScrollPane.getHorizontalScrollBar().setMaximum(3000);
But it doesn't work. The setMinimum function only sets the position of the viewport on the scrollpane, setMaximum doesn't do anything.

Change your JPanel instead to have size (2500, 2500) and render desired fragment of the image. Use translate() of the Graphics in paintComponent() for this.

Related

JFrame & JPanel sizing issue

I am attempting to code a JFrame containing a JPanel. Within the JPanel is an array of JTextField's. So, my GUI looks like:-
I am not using a layout manager, and have set this to null for the JFrame and the JPanel. I am sizing these components by hand.
You can see that the right hand portion of the JPanel is chopped off, even though I have used the same sizing as the containing JFrame.
The code appears as below:-
I have calculated the required width of the JPanel by multiplying the number of columns in the JTextField array by the width of the JTextField. Aside from that would need to be added the width of each gap between the JTextFields (there would be (columnNumber - 1) of them), as well as the two border gaps.
I have done this, yet the right hand side border gap is chopped off, as you can see from the diagram.
If I add some random amount to the panelWidth, then you can see the right hand gap there, but my question is what am I missing here? This ought to work surely, if the JFrame side and the JPanel size are identical, which they are as I have also printed them both out, and the print outs give the same number.
Jeremy
I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing.
The GridLayout allows you to specify a horizontal/vertical gap between each component and allows you to control the size of the grid.
Then you can wrap the panel using a GridLayout in a panel that respects the size of the grid.
For example you could do:
JPanel grid = new JPanel( new GridLayout(...) );
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(grid, new GridBagConstraints());
frame.add(wrapper, BorderLayout.CENTER);
If you pack the frame, the grid panel will be displayed at is preferred size.
If you resize the frame the grid panel will remain centered in the wrapper panel.
The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the CENTER of a border layout, the flow layout in the PAGE_END, pack the top level container (for non-cropped, 'right size') & the job is done.
It might end up looking something like this:

How do I get the bounds of a JPanel centered in another JPanel?

I have a main panel with a border layout containing another panel in the center.
Because I want to paint inside the centered panel I have to get the bounds of it.
panel_main.add(panel, BorderLayout.CENTER);
I wanted to save the bounds in a rectangle like this:
Rectangle bounding = new Rectangle
(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight());
But every argument seems to be 0. So how do I get the bounds of the panel?
Could I get the bounds of the BorderLayout instead?
But every argument seems to be 0.
All Swing components have a size of (0, 0) when they are created. Components are only given a size when the frame is packed and made visible.
Because I want to paint inside the centered panel I have to get the bounds of it.
You override the paintComponent() method of the panel. Then you can use the getSize() method to control where the painting is done.
You will also need to override the getPreferredSize() method of the panel so the layout manager can pack() the frame properly.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.

drawing in negative coordinates java swing

How can I scroll by JScrollPannel left-down to the negative coordinates area? I am developing an editor with drag and drop. For example, we have a small circle at the center of JPanel wich has a coordinates (50, 50). This circle is only drawing on the JPanel, not a java.awt.Component, thats why it can get any values of coordinates. We drag this circle left-down and its coordinates becomes (-30, 50). How can I see this circle after moving?
After dragging the circle to the right I can increase a size of my JPanel if the location of the circle will be outside of the bounds of JPannel. But what need I do to the first case?
Well, in addition I tried to draw a picture.
Check out the Drag Layout. It is a layout manager designed to allow you to drag components.
if a component is dragged to a negative location then the location of all components are translated by the negative amount to make sure the location of all components is positive
the preferred size is recalculated after each component is dragged.

How to make JPanel resize to fits its components?

I have a JScrollPane whose viewport is a JPanel. The JPanel contains smaller JPanels that take up the entire viewport's width, and the big JPanel is set to a FlowLayout. The user should be able to add as many JPanels as they want (well, up to 200 for my purposes), and they should be able to scroll down the JScrollPane to see everything they have added. Basically I'm just trying to make the JScrollPane grow. I'm calculating what I thought the JPanel's height should be like this:
Dimension dimension = new Dimension(width, smallPanel.height * totalPanels
+ ((FlowLayout) getLayout()).getVGap * totalPanels);
setPreferredSize(dimension);
And it mostly works, but as you add more, it starts cutting the smaller JPanels off, and the bottom panel eventually isn't shown. Is there a way that I can determine the size of the JPanel for the viewport so that I wouldn't have to calculate its dimensions with variables? Like pack() does for JFrame? Or do I need to keep guessing and checking?
Thanks

Finding Coordinates on a Bufferred Image

I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage like this, you save a Rectangle for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains method of Rectangle to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout or BoxLayout works well.

Categories