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.
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);
}
});
}
}