check if JScrollPane's scrollbars are visible

J

johnmmcparland

I'm trying to implement a fitToWindow type mechanism and a way I've
thought of doing this is to increase / descrease the size of something
until just before the scrollbars are showing. Unfortunately
JScrollPane doesn't seem to provide this functionality directly. Is
there anything else I can do.

Code:
/*
 * Zooming.java
 * 8 Jan 2009
 * Copyright (c) 2009 John McParland
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
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;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * Increases / decreases the size of something
 */
public class Zooming extends JPanel
{

    /**
     * {@link java.io.Serializable}
     */
    public static final long serialVersionUID = 0;

    /**
     * The scroller for this panel
     */
    private JScrollPane scroller = null;

    /**
     * Text box to change size of
     */
    private JTextArea textbox = new JTextArea("Blah blah blah", 3,
30);

    /*
     * Strings for buttons
     */
    private static final String INCREASE_TXT = "Increase";

    private static final String DECREASE_TXT = "Decrease";

    private static final String FIT_TO_WINDOW_TXT = "Fit to window";

    /**
     * Create a Zooming window
     */
    public Zooming()
    {
        this.setLayout(new BorderLayout());
        textbox.setLineWrap(false);
        textbox.setFont(new java.awt.Font("Monospaced", 0, 11));
        textbox.setEditable(true);
        JScrollPane scroller = new JScrollPane();
        this.add(scroller, BorderLayout.CENTER);
        scroller.getViewport().add(textbox, null);
    }

    /**
     * Handler for buttons
     */
    private class ButtonHandler implements ActionListener
    {
        /**
         * Name of this handler
         */
        private String name = "";

        /**
         * How much to adjust the font size by
         */
        private int ADJUSTMENT = 10;

        /**
         * Create a button handler with a given name
         *
         * @param nm
         *            The name of the button
         */
        public ButtonHandler(String nm)
        {
            name = nm;
        }

        /*
         * (non-Javadoc)
         *
         * @see java.awt.event.ActionListener#actionPerformed
(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent ae)
        {
            // Check what the name is and adjust font of textbox
accordingly
            if (name.equals(INCREASE_TXT))
            {
                Font currFont = textbox.getFont();
                textbox.setFont(currFont.deriveFont(currFont.getSize2D
() + ADJUSTMENT));
            }
            else if (name.equals(DECREASE_TXT))
            {
                Font currFont = textbox.getFont();
                // Only decrease if it won't end up < 0
                float size = currFont.getSize2D();
                if (ADJUSTMENT < size)
                {
                    textbox.setFont(currFont.deriveFont
(currFont.getSize2D() - ADJUSTMENT));
                }
            }
            else if (name.equals(FIT_TO_WINDOW_TXT))
            {
                // TODO: Work out how to find it scroll bars are
visible or not and
                // set the font to a width which allows this
            }
            else
            {
                System.err.println("Whoops something exectued
ButtonHandler#actionPerformed that shouldn't have");
            }
        }
    }

    /**
     * Main program
     *
     * @param args
     *            Program arguments, not required and will be ignored
if provided
     */
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            public void run()
            {
                // Setup a frame for the Zoomer
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(new BorderLayout());
                Zooming zoom = new Zooming();
                frame.getContentPane().add(zoom, BorderLayout.CENTER);

                // Buttons to manipulate the size
                JButton increase = new JButton(INCREASE_TXT);
                increase.addActionListener(zoom.new ButtonHandler
(INCREASE_TXT));
                JButton decrease = new JButton(DECREASE_TXT);
                decrease.addActionListener(zoom.new ButtonHandler
(DECREASE_TXT));
                JButton fitToWindow = new JButton(FIT_TO_WINDOW_TXT);
                fitToWindow.addActionListener(zoom.new ButtonHandler
(FIT_TO_WINDOW_TXT));

                // Add then to a grid on the south of the frame
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new GridLayout(1,3));
                buttonPanel.add(increase);
                buttonPanel.add(decrease);
                buttonPanel.add(fitToWindow);
                frame.add(buttonPanel, BorderLayout.SOUTH);

                // Show it
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
 
J

johnmmcparland

I'm trying to implement a fitToWindow type mechanism and a way I've
thought of doing this is to increase / descrease the size of something
until just before the scrollbars are showing.  Unfortunately
JScrollPane doesn't seem to provide this functionality directly.  Is
there anything else I can do.

Code:
/*
 * Zooming.java
 * 8 Jan 2009
 * Copyright (c) 2009 John McParland
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
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;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * Increases / decreases the size of something
 */
public class Zooming extends JPanel
{

    /**
     * {@link java.io.Serializable}
     */
    public static final long serialVersionUID = 0;

    /**
     * The scroller for this panel
     */
    private JScrollPane scroller = null;

    /**
     * Text box to change size of
     */
    private JTextArea textbox = new JTextArea("Blah blah blah", 3,
30);

    /*
     * Strings for buttons
     */
    private static final String INCREASE_TXT = "Increase";

    private static final String DECREASE_TXT = "Decrease";

    private static final String FIT_TO_WINDOW_TXT = "Fit to window";

    /**
     * Create a Zooming window
     */
    public Zooming()
    {
        this.setLayout(new BorderLayout());
        textbox.setLineWrap(false);
        textbox.setFont(new java.awt.Font("Monospaced", 0, 11));
        textbox.setEditable(true);
        JScrollPane scroller = new JScrollPane();
        this.add(scroller, BorderLayout.CENTER);
        scroller.getViewport().add(textbox, null);
    }

    /**
     * Handler for buttons
     */
    private class ButtonHandler implements ActionListener
    {
        /**
         * Name of this handler
         */
        private String name = "";

        /**
         * How much to adjust the font size by
         */
        private int ADJUSTMENT = 10;

        /**
         * Create a button handler with a given name
         *
         * @param nm
         *            The name of the button
         */
        public ButtonHandler(String nm)
        {
            name = nm;
        }

        /*
         * (non-Javadoc)
         *
         * @see java.awt.event.ActionListener#actionPerformed
(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent ae)
        {
            // Check what the name is and adjust font of textbox
accordingly
            if (name.equals(INCREASE_TXT))
            {
                Font currFont = textbox.getFont();
                textbox.setFont(currFont.deriveFont(currFont.getSize2D
() + ADJUSTMENT));
            }
            else if (name.equals(DECREASE_TXT))
            {
                Font currFont = textbox.getFont();
                // Only decrease if it won't end up < 0
                float size = currFont.getSize2D();
                if (ADJUSTMENT < size)
                {
                    textbox.setFont(currFont.deriveFont
(currFont.getSize2D() - ADJUSTMENT));
                }
            }
            else if (name.equals(FIT_TO_WINDOW_TXT))
            {
                // TODO: Work out how to find it scroll bars are
visible or not and
                // set the font to a width which allows this
            }
            else
            {
                System.err.println("Whoops something exectued
ButtonHandler#actionPerformed that shouldn't have");
            }
        }
    }

    /**
     * Main program
     *
     * @param args
     *            Program arguments, not required and will be ignored
if provided
     */
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            public void run()
            {
                // Setup a frame for the Zoomer
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(new BorderLayout());
                Zooming zoom = new Zooming();
                frame.getContentPane().add(zoom, BorderLayout.CENTER);

                // Buttons to manipulate the size
                JButton increase = new JButton(INCREASE_TXT);
                increase.addActionListener(zoom.new ButtonHandler
(INCREASE_TXT));
                JButton decrease = new JButton(DECREASE_TXT);
                decrease.addActionListener(zoom.new ButtonHandler
(DECREASE_TXT));
                JButton fitToWindow = new JButton(FIT_TO_WINDOW_TXT);
                fitToWindow.addActionListener(zoom.new ButtonHandler
(FIT_TO_WINDOW_TXT));

                // Add then to a grid on the south of the frame
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new GridLayout(1,3));
                buttonPanel.add(increase);
                buttonPanel.add(decrease);
                buttonPanel.add(fitToWindow);
                frame.add(buttonPanel, BorderLayout.SOUTH);

                // Show it
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

There is a slight bug in the program,

in

public Zooming()

the following line;

JScrollPane scroller = new JScrollPane();

should be replaced by

scroller = new JScrollPane();
 
J

johnmmcparland

I'm trying to implement a fitToWindow type mechanism and a way I've
thought of doing this is to increase / descrease the size of something
until just before the scrollbars are showing.  Unfortunately
JScrollPane doesn't seem to provide this functionality directly.  Is
there anything else I can do.
Code:
/*
 * Zooming.java
 * 8 Jan 2009
 * Copyright (c) 2009 John McParland
 */
package gui;[/QUOTE]
[QUOTE]
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;[/QUOTE]
[QUOTE]
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;[/QUOTE]
[QUOTE]
/**
 * Increases / decreases the size of something
 */
public class Zooming extends JPanel
{[/QUOTE]
[QUOTE]
    /**
     * {@link java.io.Serializable}
     */
    public static final long serialVersionUID = 0;[/QUOTE]
[QUOTE]
    /**
     * The scroller for this panel
     */
    private JScrollPane scroller = null;[/QUOTE]
[QUOTE]
    /**
     * Text box to change size of
     */
    private JTextArea textbox = new JTextArea("Blah blah blah", 3,
30);[/QUOTE]
[QUOTE]
    /*
     * Strings for buttons
     */
    private static final String INCREASE_TXT = "Increase";[/QUOTE]
[QUOTE]
    private static final String DECREASE_TXT = "Decrease";[/QUOTE]
[QUOTE]
    private static final String FIT_TO_WINDOW_TXT = "Fit to window";[/QUOTE]
[QUOTE]
    /**
     * Create a Zooming window
     */
    public Zooming()
    {
        this.setLayout(new BorderLayout());
        textbox.setLineWrap(false);
        textbox.setFont(new java.awt.Font("Monospaced", 0, 11));
        textbox.setEditable(true);
        JScrollPane scroller = new JScrollPane();
        this.add(scroller, BorderLayout.CENTER);
        scroller.getViewport().add(textbox, null);
    }[/QUOTE]
[QUOTE]
    /**
     * Handler for buttons
     */
    private class ButtonHandler implements ActionListener
    {
        /**
         * Name of this handler
         */
        private String name = "";[/QUOTE]
[QUOTE]
        /**
         * How much to adjust the font size by
         */
        private int ADJUSTMENT = 10;[/QUOTE]
[QUOTE]
        /**
         * Create a button handler with a given name
         *
         * @param nm
         *            The name of the button
         */
        public ButtonHandler(String nm)
        {
            name = nm;
        }[/QUOTE]
[QUOTE]
        /*
         * (non-Javadoc)
         *
         * @see java.awt.event.ActionListener#actionPerformed
(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent ae)
        {
            // Check what the name is and adjust font of textbox
accordingly
            if (name.equals(INCREASE_TXT))
            {
                Font currFont = textbox.getFont();
                textbox.setFont(currFont.deriveFont(currFont.getSize2D
() + ADJUSTMENT));
            }
            else if (name.equals(DECREASE_TXT))
            {
                Font currFont = textbox.getFont();
                // Only decrease if it won't end up < 0
                float size = currFont.getSize2D();
                if (ADJUSTMENT < size)
                {
                    textbox.setFont(currFont.deriveFont
(currFont.getSize2D() - ADJUSTMENT));
                }
            }
            else if (name.equals(FIT_TO_WINDOW_TXT))
            {
                // TODO: Work out how to find it scroll bars are
visible or not and
                // set the font to a width which allows this
            }
            else
            {
                System.err.println("Whoops something exectued
ButtonHandler#actionPerformed that shouldn't have");
            }
        }
    }[/QUOTE]
[QUOTE]
    /**
     * Main program
     *
     * @param args
     *            Program arguments, not required and will be ignored
if provided
     */
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            public void run()
            {
                // Setup a frame for the Zoomer
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(new BorderLayout());
                Zooming zoom = new Zooming();
                frame.getContentPane().add(zoom, BorderLayout.CENTER);[/QUOTE]
[QUOTE]
                // Buttons to manipulate the size
                JButton increase = new JButton(INCREASE_TXT);
                increase.addActionListener(zoom.new ButtonHandler
(INCREASE_TXT));
                JButton decrease = new JButton(DECREASE_TXT);
                decrease.addActionListener(zoom.new ButtonHandler
(DECREASE_TXT));
                JButton fitToWindow = new JButton(FIT_TO_WINDOW_TXT);
                fitToWindow.addActionListener(zoom.new ButtonHandler
(FIT_TO_WINDOW_TXT));[/QUOTE]
[QUOTE]
                // Add then to a grid on the south of the frame
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new GridLayout(1,3));
                buttonPanel.add(increase);
                buttonPanel.add(decrease);
                buttonPanel.add(fitToWindow);
                frame.add(buttonPanel, BorderLayout.SOUTH);[/QUOTE]
[QUOTE]
                // Show it
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    } [QUOTE]
}[/QUOTE]

There is a slight bug in the program,

in

    public Zooming()

the following line;

    JScrollPane scroller = new JScrollPane();

should be replaced by

    scroller = new JScrollPane();

Solved by checking the height of the text box VS height of the
scrollpane;

Code:
/*
 * Zooming.java
 * 8 Jan 2009
 * Copyright (c) 2009 John McParland
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * Increases / decreases the size of something
 */
public class Zooming extends JPanel
{

    /**
     * {@link java.io.Serializable}
     */
    public static final long serialVersionUID = 0;

    /**
     * The scroller for this panel
     */
    private JScrollPane scroller = null;

    /**
     * Text box to change size of
     */
    private JTextArea textbox = new JTextArea("Blah blah blah", 3,
30);

    /*
     * Strings for buttons
     */
    private static final String INCREASE_TXT = "Increase";

    private static final String DECREASE_TXT = "Decrease";

    private static final String FIT_TO_WINDOW_TXT = "Fit to window";

    /**
     * Create a Zooming window
     */
    public Zooming()
    {
        this.setLayout(new BorderLayout());
        textbox.setLineWrap(false);
        textbox.setFont(new java.awt.Font("Monospaced", 0, 11));
        textbox.setEditable(true);
        scroller = new JScrollPane();
        this.add(scroller, BorderLayout.CENTER);
        scroller.getViewport().add(textbox, null);
    }

    /**
     * Handler for buttons
     */
    private class ButtonHandler implements ActionListener
    {
        /**
         * Name of this handler
         */
        private String name = "";

        /**
         * How much to adjust the font size by
         */
        private int ADJUSTMENT = 10;

        /**
         * Create a button handler with a given name
         *
         * @param nm
         *            The name of the button
         */
        public ButtonHandler(String nm)
        {
            name = nm;
        }

        /*
         * (non-Javadoc)
         *
         * @see java.awt.event.ActionListener#actionPerformed
(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent ae)
        {
            // Check what the name is and adjust font of textbox
accordingly
            if (name.equals(INCREASE_TXT))
            {
                Font currFont = textbox.getFont();
                textbox.setFont(currFont.deriveFont(currFont.getSize2D
() + ADJUSTMENT));
            }
            else if (name.equals(DECREASE_TXT))
            {
                Font currFont = textbox.getFont();
                // Only decrease if it won't end up < 0
                float size = currFont.getSize2D();
                if (ADJUSTMENT < size)
                {
                    textbox.setFont(currFont.deriveFont
(currFont.getSize2D() - ADJUSTMENT));
                }
            }
            else if (name.equals(FIT_TO_WINDOW_TXT))
            {
                showDims("Fit to width - before adjustment");

                double scrollerHeight = scroller.getSize().getHeight
();
                double scrollerWidth = scroller.getSize().getWidth();
                while (scrollerHeight < textbox.getSize().getHeight()
||
                        scrollerWidth < textbox.getSize().getWidth())
                {
                    Font currFont = textbox.getFont();
                    textbox.setFont(currFont.deriveFont
(currFont.getSize2D() - ADJUSTMENT));
                    showDims("Fit to width - after adjustment");
                    break;
                }
            }
            else
            {
                System.err.println("Whoops something exectued
ButtonHandler#actionPerformed that shouldn't have");
            }
        }
    }

    /**
     * Main program
     *
     * @param args
     *            Program arguments, not required and will be ignored
if provided
     */
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            /* (non-Javadoc)
             * @see java.lang.Runnable#run()
             */
            public void run()
            {
                // The zooming panel
                final Zooming zoom = new Zooming();

                // Setup a frame for the Zoomer
                final JFrame frame = new JFrame();
                frame.addWindowListener(new WindowAdapter()
                {
                    /* (non-Javadoc)
                     * @see java.awt.event.WindowAdapter#windowClosing
(java.awt.event.WindowEvent)
                     */
                    public void windowClosing(WindowEvent we)
                    {
                        zoom.showDims("Closing the window");
                        ((JFrame)we.getSource()).dispose();
                    }
                });
                frame.getContentPane().setLayout(new BorderLayout());
                frame.getContentPane().add(zoom, BorderLayout.CENTER);

                // Buttons to manipulate the size
                JButton increase = new JButton(INCREASE_TXT);
                increase.addActionListener(zoom.new ButtonHandler
(INCREASE_TXT));
                JButton decrease = new JButton(DECREASE_TXT);
                decrease.addActionListener(zoom.new ButtonHandler
(DECREASE_TXT));
                JButton fitToWindow = new JButton(FIT_TO_WINDOW_TXT);
                fitToWindow.addActionListener(zoom.new ButtonHandler
(FIT_TO_WINDOW_TXT));

                // Add then to a grid on the south of the frame
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new GridLayout(1,3));
                buttonPanel.add(increase);
                buttonPanel.add(decrease);
                buttonPanel.add(fitToWindow);
                frame.add(buttonPanel, BorderLayout.SOUTH);

                // Show it
                frame.setSize(300,400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                zoom.showDims("Intitial");
            }
        });
    }

    /**
     * Show the dimensions of the GUI components
     * @param title The title for this showing
     */
    public void showDims(String title)
    {
        System.out.println(title);
        System.out.println("Font size: "+textbox.getFont().getSize2D
());
        System.out.println("textbox dims: "+textbox.getSize().toString
());
        System.out.println("scroller dims: "+scroller.getSize
().toString());
        System.out.println();
    }

}

Amazing how talking to yourself fixes these things!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top