Search code examples
javaswingjscrollbar

How do I get the current location of bar in scroll bar?


Overview:

Consider the following image:

My Screenshot

There is a vertical scrollbar and a horizontal scrollbar. After clicking on OK, the frame would disappear.

After some other works, I want to leave the user at the same position as it is now, which may be in different image with having same width and height. So, I want to keep track of the bars in scroll bars.

Question:

  • Is there any built-in function to get the percentage or something to get and set the bar position?

    • If yes, what are they?
    • If no, what is the way to handle it?

Code:

MultipleProcess.java class, which actually opens the frame.

// imports and package description were here

public class MultipleProcess extends javax.swing.JDialog {
    SinglePaperEvaluation upperClass;
    BatchPaperEvaluation upperClass2;
    File imageFilePath;
    BufferedImage image;

    public MultipleProcess(boolean modal,File imageFile,SinglePaperEvaluation upper, BatchPaperEvaluation upper2) {
        super(upper==null?upper2:upper,"Co-ordinate Detector", modal);
        initComponents();
        upperClass = upper;
        upperClass2 = upper2;
        imageFilePath = imageFile;

        //adjust screen and windows listener were here

        loadImage();//custom function, it loads image in the jScrollPaneImageScreen

        if(upperClass2!=null){
            upperClass2.setVisible(false);

            //scrollbars states loading part
            jScrollPaneImageScreen.getVerticalScrollBar().setValue(upperClass2.verticalBar);
            jScrollPaneImageScreen.getHorizontalScrollBar().setValue(upperClass2.horizontalBar);
//            if(upperClass2.react!=null)jScrollPaneImageScreen.scrollRectToVisible(upperClass2.react);
        }

        setVisible(true);
    }

    // you can skip this function but have a look if you eager to see how am I loaded image in the pane
     private void loadImage() {
        try {
            image = ImageIO.read(imageFilePath);
            ImageIcon ii = new ImageIcon(imageFilePath.toString());
            JLabel jLabel = new JLabel(ii);
            jLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {

                    int x = e.getX();
                    int y = e.getY();

                    jTextFieldX.setText(String.format("%d",x));
                    jTextFieldY.setText(String.format("%d",y));

                }
            });
            jScrollPaneImageScreen.setViewportView(jLabel);
        } catch (IOException ex) {
            Logger.getLogger(SkeletonPreparing.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   //init Component function was here

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {                                          

        if(upperClass2!=null){
            //other irrelevant processing deleted 

            //scrollbars states storing part
            upperClass2.verticalBar = jScrollPaneImageScreen.getVerticalScrollBar().getValue();
            upperClass2.horizontalBar = jScrollPaneImageScreen.getHorizontalScrollBar().getValue();
//            upperClass2.react = jScrollPaneImageScreen.getViewport().getViewRect();
            System.out.println("Stored: "+upperClass2.verticalBar+","+upperClass2.horizontalBar);
            upperClass2.setVisible(true);
        }
        dispose();
    }                                         
    //variable initializations were here
}

BatchPaperEvaluation.java class, from where I created the instance of the above class:

private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    File[] filesToBeProcessed = new File(jTextFieldOMRFolderPath.getText()).listFiles(filter);
    for (File inputFile : filesToBeProcessed) {
        MultipleProcess dialog = new MultipleProcess(true, inputFile, null, this);
        //other processing
    }

    //further processing
} 

Note:

  • I have checked int getVerticalScrollBarPolicy(), which does not serve my work.

Solution

  • Try JScrollPane.getViewport().getViewRect() to get the visible rectangle. You can restore this by doing JScrollPane.getViewport().scrollRectToVisible(<saved rect>).