Here is repaired code. Now JScrollPane should be (400,400) - variable mainSize. Also I changed LayoutManager of JScrollPane to SpringLayout which is, I think, most suitable for me.
Now calling function below, JPanel is moved and resized. Is this resizing algorithm ok? For me this is strange that I have to change parameters by setBounds, setPreferredSize and layout.putConstraints (!?)... 3 operations to make a new position and resize of JPanel? Is there better way to do this?
And still there is a problem with setting horizontal scrollbar.
private void resizeScrollActionPerformed(ActionEvent evt)
{
SpringLayout layout = (SpringLayout)sPane.getViewport().getLayout();
layout.putConstraint(SpringLayout.WEST, panel, 50, SpringLayout.WEST, sPane);
layout.putConstraint(SpringLayout.NORTH, panel, 50, SpringLayout.NORTH, sPane);
this.panel.setPreferredSize(new Dimension(1000, 100));
this.panel.setBounds(0, 0, 1000, 100);
this.setScrollPosition(50, 50);
}
Full Program:
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
/**
*
* @author blazejp
*/
class JPanelCircle extends JPanel
{
public JPanelCircle()
{
super();
this.setLayout(new FlowLayout());
this.setBackground(Color.red);
this.setBounds(0, 0, 200, 50);
this.setPreferredSize(new Dimension(200, 50));
}
@Override
protected void paintComponent(Graphics g)
{
int h = getHeight();
int w = getWidth();
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval(w/2, h/2, w, h);
}
}
class Scroll2 extends JPanel
{
private JPanelCircle panel;
private JScrollPane sPane;
private Dimension mainSize = new Dimension(400, 400);
public Scroll2()
{
super(new BorderLayout());
panel = new JPanelCircle();
panel.repaint();
sPane = new JScrollPane(panel);
sPane.setBounds(0,0,mainSize.width,mainSize.height);
sPane.setPreferredSize(mainSize);
sPane.getViewport().setBackground(Color.black);
sPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sPane.setBorder(BorderFactory.createBevelBorder(0, Color.lightGray, Color.yellow));
sPane.getViewport().setLayout(new SpringLayout());
this.add(sPane, BorderLayout.NORTH);
JButton resizeScroll = new JButton("resize&scroll");
resizeScroll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resizeScrollActionPerformed(evt);
}
});
this.add(resizeScroll);
}
private void scrollActionPerformed(ActionEvent evt)
{
this.setScrollPosition(50, 50);
}
private void resizeScrollActionPerformed(ActionEvent evt)
{
SpringLayout layout = (SpringLayout)sPane.getViewport().getLayout();
layout.putConstraint(SpringLayout.WEST, panel, 50, SpringLayout.WEST, sPane);
layout.putConstraint(SpringLayout.NORTH, panel, 50, SpringLayout.NORTH, sPane);
this.panel.setPreferredSize(new Dimension(1000, 100));
this.panel.setBounds(0, 0, 1000, 100);
this.setScrollPosition(50, 50);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Scroll");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Scroll2 newContentPane = new Scroll2();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public final int getHorizontalScrollPosition()
{
int maxPositionH = sPane.getHorizontalScrollBar().getMaximum () - sPane.getHorizontalScrollBar().getVisibleAmount();
int scrollPos = sPane.getHorizontalScrollBar().getValue();
int percentage = 0;
if(maxPositionH > 0)
{
percentage = (int)(((float)scrollPos / (float)maxPositionH)*100);
}
return percentage;
}
public final void setHorizontalScrollPosition(int posPercentage)
{
int maxPositionH = sPane.getHorizontalScrollBar().getMaximum () - sPane.getHorizontalScrollBar().getVisibleAmount();
int scrollPos = maxPositionH;
if(maxPositionH > 0 && posPercentage >= 0 && posPercentage <= 100)
{
scrollPos = (int)(((float)maxPositionH * (float)posPercentage)/100);
}
else if(posPercentage > 100)
{
scrollPos = maxPositionH;
}
sPane.getHorizontalScrollBar().setValue(scrollPos);
}
/*
* Gets vertical scroll position in percentage
*/
public final int getVerticalScrollPosition()
{
int maxPositionV = sPane.getVerticalScrollBar().getMaximum () - sPane.getVerticalScrollBar().getVisibleAmount();
int scrollPos = sPane.getVerticalScrollBar().getValue();
int percentage = 0;
if(maxPositionV > 0)
{
percentage = (int)(((float)scrollPos / (float)maxPositionV)*100);
}
return percentage;
}
public final void setVerticalScrollPosition(int posPercentage)
{
int maxPositionV = sPane.getVerticalScrollBar().getMaximum () - sPane.getVerticalScrollBar().getVisibleAmount();
int scrollPos = maxPositionV;
if(maxPositionV > 0 && posPercentage >= 0 && posPercentage <= 100)
{
scrollPos = (int)(((float)maxPositionV * (float)posPercentage)/100);
}
else if(posPercentage > 100)
{
scrollPos = maxPositionV;
}
sPane.getVerticalScrollBar().setValue(scrollPos);
}
public final void setScrollPosition(int hPercentage, int vPercentage)
{
this.setHorizontalScrollPosition(hPercentage);
this.setVerticalScrollPosition(vPercentage);
}
public static void main(String[] args) {
// TODO code application logic here
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run()
{
createAndShowGUI();
}
});
}
}