setting a JScrollBar in the middle [duplicate] - java

I have a JPanel with a JLabel in it, added to a JScrollPane. I have an actionListener that calls JLabel.setIcon("file.jpg");. The image is displayed in the JScrollPane correctly and is full size. The scrollbars appear perfectly. I am trying to position the vertical and horizontal scrollbars in the center by default so you are looking at the center of the image by default.
Is there a JScrollPane method that will position the viewport on the center of the image? Or could I manually set the position of each scrollbar to max size divided by 2?
I have tried
JScrollPane.getVerticalScrollBar().setValue(JScrollPane.getVerticalScrollBar().getMaximum() / 2);
While it compiles it does not center the scrollbar. I have also tried setting the layout manager of my JPanel to GridBagLayout but that doesn't work either.

Basically, you need to know the size of the viewport's viewable area.
Rectangle bounds = scrollPane.getViewport().getViewRect();
Then you need the size of the component, but once it's added to the scroll pane, you can get this from the view port...
Dimension size = scrollPane.getViewport().getViewSize();
Now you need to calculate the centre position...
int x = (size.width - bounds.width) / 2;
int y = (size.height - bounds.height) / 2;
Then you need to simply adjust the view port position...
scrollPane.getViewport().setViewPosition(new Point(x, y));
Now, remember, this is only going to work once the scroll pane has being realised on the screen (or at least it has being laid out within it's parent container)

I'm guessing that the image has not been read at the time you try to execute your code so try something like the following:
label.setIcon( new ImageIcon("...") );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Rectangle bounds = scrollPane.getViewport().getViewRect();
JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
horizontal.setValue( (horizontal.getMaximum() - bounds.width) / 2 );
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( (vertical.getMaximum() - bounds.height) / 2 );
}
});
This will add code to the end of the Event Dispatch Thread so hopefully it executes after the image has been read in completely and the scrollbar values have all been updated.

Add an AdjustmentListener and see if that helps. It will let you know if the value of the component changes. That way, when the image is added, the scroll bar's properties will change and you will be notified. You can then try to set the caret position to the mid.
Tutorial: http://examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/

Related

How do I enable a button after the scrollbar goes to the bottom?

So, I used a JScrollPane and then I added a JTextArea. I used textArea.setCaretPosition(0) to reset the scroll and it went at the top. All good, until I wanted to set a disabled Button on enable when the scrollbar reaches at the bottom.
How can I do that?
You can listen for changes to the JScrollPane’s viewport, and compare the bottom of the viewport’s visible rectangle with the height of the viewport’s view (that is, the JTextArea):
JViewport viewport = scrollPane.getViewport();
viewport.addChangeListener(e -> {
Rectangle rect = viewport.getViewRect();
int bottom = rect.y + rect.height;
endButton.setEnabled(bottom >= viewport.getViewSize().height);
});

JavaFX: Window.sizeToScene() center at original position

I know Window.sizeToScene() will resize the window to the size that its scene needs, but the position of the window does not adjust accordingly (i.e. the stationary point is top-left corner of the window). Is there any way to make the window resize itself, and keep the window's center at the same place (i.e. make the stationary point at the center of the window)?
Do something like:
public void resize(Window win) {
double x = win.getX();
double y = win.getY();
double width = win.getWidth();
double height = win.getHeight();
win.sizeToScene();
win.setX(x + ((width - win.getWidth()) / 2));
win.setY(y + ((height - win.getHeight()) / 2));
}
The code above caches the position before the window is resized to the scene, then it moves the window the appropriate amount to keep the window centered in the same area. This code does not take into account where the window will be once it is moved/resized. You might want to add checks to make sure the window doesn't end up going off the screen.

Java Swing repaint(), revalidate() issue after addind Jpanel to Jpanel

i'm quite new in java and i'm trying create my own scrabble game. I created my own classes Board and Tile both JPanels. while im drawing tiles on my Board :
Tile tile = new Tile(currentlyChosenLetter, jump);
board.add(tile);
tile.setBounds(x * jump + 1, y * jump + 1, jump - 2, jump - 2);
when im doing like this everything seems working fine :
but after adding :
board.revalidate();
board.repaint();
tiles are misplaced, i need to repaint in case of removing Tiles.
x and y im getting from my mouse position :
int jump = board.getHeight() / 15;
int x = (e.getX() / jump);
int y = (e.getY() / jump);
where e is MouseEvent.
board.revalidate();
board.repaint();
The revalidate() statement invokes the layout manager so the child components are given a size and location based on the rules of the layout manager. The default layout manager for a JPanel is the FlowLayout so the components appear on a single line.
So don't use setBounds(...). Instead use a proper layout manager like the GridLayout and add components to each square of the grid.
I would suggestion you might want to a JLabel to each grid. Then you can add and Icon to each label with the default icon for a given square. Then as a letter is added you replace the Icon with the text.

How do I center the vertical and horizontal scrollbars in a JScrollPane?

I have a JPanel with a JLabel in it, added to a JScrollPane. I have an actionListener that calls JLabel.setIcon("file.jpg");. The image is displayed in the JScrollPane correctly and is full size. The scrollbars appear perfectly. I am trying to position the vertical and horizontal scrollbars in the center by default so you are looking at the center of the image by default.
Is there a JScrollPane method that will position the viewport on the center of the image? Or could I manually set the position of each scrollbar to max size divided by 2?
I have tried
JScrollPane.getVerticalScrollBar().setValue(JScrollPane.getVerticalScrollBar().getMaximum() / 2);
While it compiles it does not center the scrollbar. I have also tried setting the layout manager of my JPanel to GridBagLayout but that doesn't work either.
Basically, you need to know the size of the viewport's viewable area.
Rectangle bounds = scrollPane.getViewport().getViewRect();
Then you need the size of the component, but once it's added to the scroll pane, you can get this from the view port...
Dimension size = scrollPane.getViewport().getViewSize();
Now you need to calculate the centre position...
int x = (size.width - bounds.width) / 2;
int y = (size.height - bounds.height) / 2;
Then you need to simply adjust the view port position...
scrollPane.getViewport().setViewPosition(new Point(x, y));
Now, remember, this is only going to work once the scroll pane has being realised on the screen (or at least it has being laid out within it's parent container)
I'm guessing that the image has not been read at the time you try to execute your code so try something like the following:
label.setIcon( new ImageIcon("...") );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Rectangle bounds = scrollPane.getViewport().getViewRect();
JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
horizontal.setValue( (horizontal.getMaximum() - bounds.width) / 2 );
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( (vertical.getMaximum() - bounds.height) / 2 );
}
});
This will add code to the end of the Event Dispatch Thread so hopefully it executes after the image has been read in completely and the scrollbar values have all been updated.
Add an AdjustmentListener and see if that helps. It will let you know if the value of the component changes. That way, when the image is added, the scroll bar's properties will change and you will be notified. You can then try to set the caret position to the mid.
Tutorial: http://examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/

Getting coordinates of a component in java

I have a JLayeredPane, and inside the JLayeredPane is a JInternalFrame. What I need are the bounds (x position, y position, width, height) of the content pane in the JInternalFrame. The bounds need to be in relation to the JLayeredPane. My problem is the borders, title bar, and I'm not sure what else of the JInternalFrame are messing with my calculation. It's a few pixels off.
Here is how I try to calculate it. This code is in the JInternalFrame:
Rectangle area = new Rectangle(
getX() + 2, //The x coordinate of the JInternalFrame + 2 for the border
getY() + ((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getHeight(), //The Y position of the JinternalFrame + theheight of the title bar of the JInternalFrame
getContentPane().getWidth() - 2, //The width of the Jinternals content pane - the border of the frame
getContentPane().getHeight() //the height of the JinternalFrames content pane
);
I need to get the location of the content pane of the internal frame.
Coordinates with respect to what? The screen? The window?
Calling getLocation() will give you the coordinates relative to the component's parent in the window hierarchy.
SwingUtilities has several methods for transforming coordinates from one frame of reference to another. So, to compensate for the title bar and frame, you can do this:
JInternalFrame iframe = ...
Container c = iframe.getContentPane();
Rectangle r = c.getBounds();
r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent());
The getBounds() method of JInternalFrame returns a Rectangle with the coordinates you need. Why not just do the following:
{
...
JInternalFrame iframe = ...;
Rectangle r = new Rectangle(iframe.getBounds());
}
This way you don't need to manually calculate the bounds.

Categories

Resources