Java by API/javax.swing/JScrollBar
Содержание
- 1 JScrollBar: addAdjustmentListener(AdjustmentListener l)
- 2 JScrollBar: getBlockIncrement(int direction)
- 3 JScrollBar: getMinimum()
- 4 JScrollBar: getModel()
- 5 JScrollBar: getUnitIncrement(int direction)
- 6 JScrollBar.HORIZONTAL
- 7 JScrollBar: setBlockIncrement(int blockIncrement)
- 8 JScrollBar: setFocusable(boolean focusable)
- 9 JScrollBar: setUnitIncrement(int unitIncrement)
- 10 JScrollBar: setValue(int value)
- 11 new JScrollBar(int orientation)
- 12 new JScrollBar(int orientation, int value, int extent, int min, int max)
JScrollBar: addAdjustmentListener(AdjustmentListener l)
<source lang="java">
import java.awt.BorderLayout; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import javax.swing.JFrame; import javax.swing.JScrollBar; public class MainClass {
public static void main(String args[]) throws Exception { AdjustmentListener adjustmentListener = new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) { System.out.println("Adjusted: " + adjustmentEvent.getValue()); } }; JScrollBar oneJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); oneJScrollBar.addAdjustmentListener(adjustmentListener);
JFrame frame = new JFrame("ScrollBars R Us"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(oneJScrollBar, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
JScrollBar: getBlockIncrement(int direction)
<source lang="java">
/*
* * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main extends JPanel implements MouseWheelListener {
JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); final static String newline = "\n"; public Main() { super(new BorderLayout()); textArea.setEditable(false); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(400, 250)); add(scrollPane, BorderLayout.CENTER); textArea.append("This text area displays information " + "about its mouse wheel events." + newline); textArea.addMouseWheelListener(this); setPreferredSize(new Dimension(450, 350)); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); } public void mouseWheelMoved(MouseWheelEvent e) { String message; int notches = e.getWheelRotation(); if (notches < 0) { message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline; } else { message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline; } if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { message += " Scroll type: WHEEL_UNIT_SCROLL" + newline; message += " Scroll amount: " + e.getScrollAmount() + " unit increments per notch" + newline; message += " Units to scroll: " + e.getUnitsToScroll() + " unit increments" + newline; message += " Vertical unit increment: " + scrollPane.getVerticalScrollBar().getUnitIncrement(1) + " pixels" + newline; } else { // scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL message += " Scroll type: WHEEL_BLOCK_SCROLL" + newline; message += " Vertical block increment: " + scrollPane.getVerticalScrollBar().getBlockIncrement(1) + " pixels" + newline; } saySomething(message, e); } void saySomething(String eventDescription, MouseWheelEvent e) { textArea.append(e.getComponent().getClass().getName() + ": " + eventDescription); textArea.setCaretPosition(textArea.getDocument().getLength()); } public static void main(String[] args) { JFrame frame = new JFrame("MouseWheelEventDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Main(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); }
}
</source>
JScrollBar: getMinimum()
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.border.LineBorder; public class MainClass {
public static void main(String args[]) throws Exception { JFrame frame = new JFrame("Cornering Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("+"); JLabel bigLabel = new JLabel("A"); bigLabel.setPreferredSize(new Dimension(400,400)); bigLabel.setBorder(new LineBorder(Color.red)); JScrollPane scrollPane = new JScrollPane(bigLabel); scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button); scrollPane.setColumnHeaderView(new JLabel("B")); scrollPane.setRowHeaderView(new JLabel("C")); ActionListener actionListener = new JScrollPaneToTopAction(scrollPane); button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
class JScrollPaneToTopAction implements ActionListener {
JScrollPane scrollPane; public JScrollPaneToTopAction(JScrollPane scrollPane) { if (scrollPane == null) { throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane"); } this.scrollPane = scrollPane; } public void actionPerformed(ActionEvent actionEvent) { JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar(); JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar(); verticalScrollBar.setValue(verticalScrollBar.getMinimum()); horizontalScrollBar.setValue(horizontalScrollBar.getMinimum()); }
}
</source>
JScrollBar: getModel()
<source lang="java">
import java.awt.BorderLayout; import javax.swing.BoundedRangeModel; import javax.swing.JFrame; import javax.swing.JProgressBar; import javax.swing.JScrollBar; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class MainClass {
public static void main(String args[]) throws Exception { ChangeListener changeListener = new BoundedChangeListener(); JScrollBar anotherJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); BoundedRangeModel model = anotherJScrollBar.getModel(); model.addChangeListener(changeListener); JFrame frame = new JFrame("ScrollBars R Us"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(anotherJScrollBar, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); }
} class BoundedChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent changeEvent) { Object source = changeEvent.getSource(); if (source instanceof BoundedRangeModel) { BoundedRangeModel aModel = (BoundedRangeModel) source; if (!aModel.getValueIsAdjusting()) { System.out.println("Changed: " + aModel.getValue()); } } else if (source instanceof JSlider) { JSlider theJSlider = (JSlider) source; if (!theJSlider.getValueIsAdjusting()) { System.out.println("Slider changed: " + theJSlider.getValue()); } } else if (source instanceof JProgressBar) { JProgressBar theJProgressBar = (JProgressBar) source; System.out.println("ProgressBar changed: " + theJProgressBar.getValue()); } else { System.out.println("Something changed: " + source); } }
}
</source>
JScrollBar: getUnitIncrement(int direction)
<source lang="java">
/*
* * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main extends JPanel implements MouseWheelListener {
JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); final static String newline = "\n"; public Main() { super(new BorderLayout()); textArea.setEditable(false); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(400, 250)); add(scrollPane, BorderLayout.CENTER); textArea.append("This text area displays information " + "about its mouse wheel events." + newline); textArea.addMouseWheelListener(this); setPreferredSize(new Dimension(450, 350)); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); } public void mouseWheelMoved(MouseWheelEvent e) { String message; int notches = e.getWheelRotation(); if (notches < 0) { message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline; } else { message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline; } if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { message += " Scroll type: WHEEL_UNIT_SCROLL" + newline; message += " Scroll amount: " + e.getScrollAmount() + " unit increments per notch" + newline; message += " Units to scroll: " + e.getUnitsToScroll() + " unit increments" + newline; message += " Vertical unit increment: " + scrollPane.getVerticalScrollBar().getUnitIncrement(1) + " pixels" + newline; } else { // scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL message += " Scroll type: WHEEL_BLOCK_SCROLL" + newline; message += " Vertical block increment: " + scrollPane.getVerticalScrollBar().getBlockIncrement(1) + " pixels" + newline; } saySomething(message, e); } void saySomething(String eventDescription, MouseWheelEvent e) { textArea.append(e.getComponent().getClass().getName() + ": " + eventDescription); textArea.setCaretPosition(textArea.getDocument().getLength()); } public static void main(String[] args) { JFrame frame = new JFrame("MouseWheelEventDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Main(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); }
}
</source>
JScrollBar.HORIZONTAL
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollBar; public class MainClass {
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollBar oneJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); frame.add(oneJScrollBar, BorderLayout.NORTH); frame.setSize(200, 44); frame.setVisible(true); }
}
</source>
JScrollBar: setBlockIncrement(int blockIncrement)
<source lang="java">
import java.awt.BorderLayout; 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; public class MainClass extends JPanel {
JLabel label = new JLabel(); public MainClass() { super(true); setLayout(new BorderLayout()); JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300); JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300); hbar.setUnitIncrement(2); hbar.setBlockIncrement(1); hbar.addAdjustmentListener(new MyAdjustmentListener()); vbar.addAdjustmentListener(new MyAdjustmentListener()); add(hbar, BorderLayout.SOUTH); add(vbar, BorderLayout.EAST); add(label, BorderLayout.CENTER); } class MyAdjustmentListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { label.setText(" New Value is " + e.getValue() + " "); repaint(); } } public static void main(String s[]) { JFrame frame = new JFrame("Scroll Bar Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new MainClass()); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
JScrollBar: setFocusable(boolean focusable)
<source lang="java">
import java.awt.Dimension; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class Main extends JPanel implements FocusListener {
public Main() { JTextField textField = new JTextField("A TextField"); textField.addFocusListener(this); JLabel label = new JLabel("A Label"); label.addFocusListener(this); add(label); String comboPrefix = "ComboBox Item #"; final int numItems = 15; Vector vector = new Vector(numItems); for (int i = 0; i < numItems; i++) { vector.addElement(comboPrefix + i); } JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); add(comboBox); JButton button = new JButton("A Button"); button.addFocusListener(this); add(button); String listPrefix = "List Item #"; Vector listVector = new Vector(numItems); for (int i = 0; i < numItems; i++) { listVector.addElement(listPrefix + i); } JList list = new JList(listVector); list.setSelectedIndex(1); list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); listScrollPane.getVerticalScrollBar().setFocusable(false); listScrollPane.getHorizontalScrollBar().setFocusable(false); add(listScrollPane); setPreferredSize(new Dimension(450, 450)); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); } void displayMessage(String prefix, FocusEvent e) { System.out.println(prefix + (e.isTemporary() ? " (temporary):" : ":") + e.getComponent().getClass().getName() + "; Opposite component: " + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName() : "null")); } public static void main(String[] args) { JFrame frame = new JFrame("FocusEventDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Main(); newContentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); } public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); }
}
</source>
JScrollBar: setUnitIncrement(int unitIncrement)
<source lang="java">
import java.awt.BorderLayout; 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; public class MainClass extends JPanel {
JLabel label = new JLabel(); public MainClass() { super(true); setLayout(new BorderLayout()); JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300); JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300); hbar.setUnitIncrement(2); hbar.setBlockIncrement(1); hbar.addAdjustmentListener(new MyAdjustmentListener()); vbar.addAdjustmentListener(new MyAdjustmentListener()); add(hbar, BorderLayout.SOUTH); add(vbar, BorderLayout.EAST); add(label, BorderLayout.CENTER); } class MyAdjustmentListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { label.setText(" New Value is " + e.getValue() + " "); repaint(); } } public static void main(String s[]) { JFrame frame = new JFrame("Scroll Bar Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new MainClass()); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
JScrollBar: setValue(int value)
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.border.LineBorder; public class MainClass {
public static void main(String args[]) throws Exception { JFrame frame = new JFrame("Cornering Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("+"); JLabel bigLabel = new JLabel("A"); bigLabel.setPreferredSize(new Dimension(400,400)); bigLabel.setBorder(new LineBorder(Color.red)); JScrollPane scrollPane = new JScrollPane(bigLabel); scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button); scrollPane.setColumnHeaderView(new JLabel("B")); scrollPane.setRowHeaderView(new JLabel("C")); ActionListener actionListener = new JScrollPaneToTopAction(scrollPane); button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
}
class JScrollPaneToTopAction implements ActionListener {
JScrollPane scrollPane; public JScrollPaneToTopAction(JScrollPane scrollPane) { if (scrollPane == null) { throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane"); } this.scrollPane = scrollPane; } public void actionPerformed(ActionEvent actionEvent) { JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar(); JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar(); verticalScrollBar.setValue(verticalScrollBar.getMinimum()); horizontalScrollBar.setValue(horizontalScrollBar.getMinimum()); }
}
</source>
new JScrollBar(int orientation)
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollBar; public class MainClass {
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollBar oneJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL); frame.add(oneJScrollBar, BorderLayout.NORTH); frame.setSize(200, 44); frame.setVisible(true); }
}
</source>
new JScrollBar(int orientation, int value, int extent, int min, int max)
<source lang="java">
import java.awt.BorderLayout; 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; public class MainClass extends JPanel {
JLabel label = new JLabel(); public MainClass() { super(true); setLayout(new BorderLayout()); JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300); JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300); hbar.setUnitIncrement(2); hbar.setBlockIncrement(1); hbar.addAdjustmentListener(new MyAdjustmentListener()); vbar.addAdjustmentListener(new MyAdjustmentListener()); add(hbar, BorderLayout.SOUTH); add(vbar, BorderLayout.EAST); add(label, BorderLayout.CENTER); } class MyAdjustmentListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { label.setText(" New Value is " + e.getValue() + " "); repaint(); } } public static void main(String s[]) { JFrame frame = new JFrame("Scroll Bar Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new MainClass()); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>