public void componentResized(ComponentEvent event)
{
}
viewport.getSize();
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main extends JFrame
{
public static void main(String[] args)
{
Main m = new Main();
m.setLayout(new FlowLayout(FlowLayout.CENTER));
final JScrollPane sp = new JScrollPane();
final JPanel p = new JPanel();
p.setPreferredSize(new Dimension(100, 1000));
sp.setViewportView(p);
sp.setPreferredSize(new Dimension(120, 100));
JButton b = new JButton("Berechner");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Rectangle viewRect = sp.getViewport().getViewRect();
int scrollHeight = sp.getHeight();
int page = (int) Math.round(((viewRect.y + viewRect.height) / scrollHeight) + 0.5);
int total = (int) Math.round((p.getHeight() / scrollHeight) + 0.5);
System.out.println(page + "/" + total);
}
});
m.add(sp);
m.add(b);
// m.setPreferredSize(new Dimension(110,120));
m.pack();
m.setVisible(true);
m.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int yPosition = sp.getViewport().getViewRect().y;
int scrollHeight = sp.getHeight();
int totalHeight = p.getHeight();
int page = (int) Math.round(((yPosition + scrollHeight) / scrollHeight));
int total = (int) Math.round(( totalHeight / scrollHeight));
System.out.println(page + "/" + total);
}
});
if(bar.getValue() + bar.getHeight() >= bar.getMaximum()-50)
{
bar.setValue(bar.getMaximum());
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Main extends JFrame
{
public static void main(String[] args)
{
Main m = new Main();
m.setLayout(new FlowLayout(FlowLayout.CENTER));
final JScrollPane sp = new JScrollPane();
final JPanel p = new JPanel();
p.setPreferredSize(new Dimension(100, 2000));
final JLabel label = new JLabel("");
label.setPreferredSize(new Dimension(50, 20));
p.setLayout(new GridLayout(0, 1));
for (int i = 1; i <= 100; i++)
{
p.add(new JLabel("" + i));
}
sp.setViewportView(p);
sp.setPreferredSize(new Dimension(120, 100));
sp.setAutoscrolls(false);
sp.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
int yPosition = sp.getViewport().getViewRect().y;
int scrollHeight = sp.getHeight();
int totalHeight = p.getHeight();
int page = (int) Math.round(((yPosition + scrollHeight) / scrollHeight));
int total = (int) Math.round((totalHeight / scrollHeight));
label.setText(page + "/" + total);
}
});
m.add(sp);
m.add(label);
// m.setPreferredSize(new Dimension(110,120));
m.pack();
m.setVisible(true);
m.setDefaultCloseOperation(EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
sp.getVerticalScrollBar().setUnitIncrement(sp.getSize().height);
int yPosition = sp.getViewport().getViewRect().y;
int scrollHeight = sp.getHeight();
int totalHeight = p.getHeight();
int page = (int) Math.round(((yPosition + scrollHeight) / scrollHeight));
int total = (int) Math.round((totalHeight / scrollHeight));
label.setText(page + "/" + total);
}
});
}
}