Java by API/javax.swing/JScrollPane

Материал из Java эксперт
Перейти к: навигация, поиск

JScrollPane: getHorizontalScrollBar()

   <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>
   
  
 
  



JScrollPane: getHorizontalScrollBarPolicy()

   <source lang="java">
 

import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main {

 public static void main(String[] argv) throws Exception {
   JTextArea textArea = new JTextArea();
   JScrollPane pane = new JScrollPane(textArea);
   // Get the default scrollbar policy
   int hpolicy = pane.getHorizontalScrollBarPolicy();
   // JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
   int vpolicy = pane.getVerticalScrollBarPolicy();
   // JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
 }

}


 </source>
   
  
 
  



JScrollPane: getVerticalScrollBar()

   <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>
   
  
 
  



JScrollPane: getVerticalScrollBarPolicy()

   <source lang="java">
 

import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main {

 public static void main(String[] argv) throws Exception {
   JTextArea textArea = new JTextArea();
   JScrollPane pane = new JScrollPane(textArea);
   // Get the default scrollbar policy
   int hpolicy = pane.getHorizontalScrollBarPolicy();
   // JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
   int vpolicy = pane.getVerticalScrollBarPolicy();
   // JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
 }

}


 </source>
   
  
 
  



JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS

   <source lang="java">
 

import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main {

 public static void main(String[] argv) throws Exception {
   JTextArea textArea = new JTextArea();
   JScrollPane pane = new JScrollPane(textArea);
   pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 }

}


 </source>
   
  
 
  



JScrollPane: setColumnHeaderView(Component view)

   <source lang="java">
 

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; public class MainClass {

 public static void main(final String args[]) {
   String labels[] = { "A", "B", "C", "D", "E" };
   JFrame frame = new JFrame("Selection Modes");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JList list1 = new JList(labels);
   JScrollPane sp1 = new JScrollPane(list1);
   sp1.setColumnHeaderView(new JLabel("List"));
   frame.add(sp1, BorderLayout.CENTER);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JScrollPane: setCorner(String key, Component corner)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollBar; import javax.swing.JScrollPane; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Cornering Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Icon acrossLogo = new ImageIcon("logo-across.jpg");
   Icon downLogo = new ImageIcon("logo-down.jpg");
   Icon bookCover = new ImageIcon("puzzlebook.jpg");
   JLabel columnLabel = new JLabel(acrossLogo);
   JLabel rowLabel = new JLabel(downLogo);
   JLabel coverLabel = new JLabel(bookCover);
   JButton button = new JButton("+");
   button.setFont(new Font("Monospaced", Font.PLAIN, 8));
   JScrollPane scrollPane = new JScrollPane(coverLabel);
   scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button);
   scrollPane.setColumnHeaderView(columnLabel);
   scrollPane.setRowHeaderView(rowLabel);
   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>
   
  
 
  



JScrollPane: setRowHeaderView(Component com)

   <source lang="java">
 

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JViewport; public class MainClass {

 public static void main(final String args[]) {
   final Object rows[][] = { { "one", "1" }, { "two", "2" },
       { "three", "3" } };
   final Object headers[] = { "English", "Digit" };
   
   JFrame frame = new JFrame("Scrollless Table");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
   JTable table = new JTable(rows,headers);
   
   JScrollPane scrollPane = new JScrollPane(table);
   JViewport viewport = new JViewport();
   viewport.setView(table);
   scrollPane.setColumnHeaderView( new JLabel("table header here"));
   scrollPane.setRowHeaderView(viewport);
   
   frame.add(scrollPane, BorderLayout.CENTER);
   frame.setSize(300, 150);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JScrollPane: setViewportView(Component view)

   <source lang="java">
 

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JLabel dogLabel = new JLabel("www.jexp.ru");
   JScrollPane scrollPane = new JScrollPane();
   scrollPane.setViewportView(dogLabel);

// scrollPane.getViewport().setView(dogLabel);

   frame.add(scrollPane, BorderLayout.CENTER);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JScrollPane.VERTICAL_SCROLLBAR_ALWAYS

   <source lang="java">
 

import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main {

 public static void main(String[] argv) throws Exception {
   JTextArea textArea = new JTextArea();
   JScrollPane pane = new JScrollPane(textArea);
   pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 }

}


 </source>
   
  
 
  



new JScrollPane(Component view, int vsbPolicy, int hsbPolicy)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public class MainClass extends JPanel {

 public MainClass() {
   setLayout(new BorderLayout());
   JPanel jp = new JPanel();
   jp.setLayout(new GridLayout(20, 20));
   int b = 0;
   for (int i = 0; i < 20; i++) {
     for (int j = 0; j < 20; j++) {
       jp.add(new JButton("Button " + b));
       ++b;
     }
   }
   // Add panel to a scroll pane.
   int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
   int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
   JScrollPane jsp = new JScrollPane(jp, v, h);
   // Add scroll pane to the content pane.
   add(jsp, BorderLayout.CENTER);
 }
 public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.getContentPane().add(new MainClass());
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(200, 200);
   frame.setVisible(true);
 }

}


 </source>