import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ScrollDemo extends JFrame implements ActionListener { JScrollPane scrollPane; public ScrollDemo() { super("JScrollPane Demonstration"); scrollPane = new JScrollPane(getScrollComponent()); //Just for demonstration, set the vertical scrollbar to always show. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //Set a row and column header with a large font. JLabel l = new JLabel("Column Header"); l.setFont(new Font("Serif", Font.BOLD, 40)); scrollPane.setColumnHeaderView(l); l = new JLabel("RH"); l.setFont(new Font("Serif", Font.BOLD, 40)); scrollPane.setRowHeaderView(l); //Place buttons in the corners. JButton b = new JButton("UL"); b.addActionListener(this); scrollPane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, b); b = new JButton("UR"); b.addActionListener(this); scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, b); b = new JButton("LL"); b.addActionListener(this); scrollPane.setCorner(ScrollPaneConstants.LOWER_LEFT_CORNER, b); b = new JButton("LR"); b.addActionListener(this); scrollPane.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, b); getContentPane().add(scrollPane, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(300, 200); setVisible(true); } public void actionPerformed(ActionEvent e) { System.out.println("action command = " + e.getActionCommand()); } public JPanel getScrollComponent() { //Assemble the scrollable component, in this case a JPanel. JPanel p = new JPanel(); p.setBackground(Color.BLUE); p.setPreferredSize(new Dimension(500, 500)); return p; } public static void main(String args[]) { new ScrollDemo(); } }