Jake
Jake

Reputation: 313

Having Multiple Panels with scroll panes

I created two panels and a main panel. Each panel contains a very large image, and I wanted both of them to be scroll-able to see the rest of the image. But when I add the two panels in the main panel and run it, the first panel is soo big that it covers the second panel. How would I implement ScrollPane for both panels?

import java.awt.BorderLayout;
import javax.swing.*;

public class BoardFrame extends JFrame {

   JPanel mainPanel = new JPanel(new BorderLayout());

   JLabel jLabel = new JLabel();
   JPanel jPanelNorth = new JPanel();
   JScrollPane scrollPane = new JScrollPane();

   JLabel jLabel2 = new JLabel();
   JPanel jPanelSouth = new JPanel();
   JScrollPane scrollPane2 = new JScrollPane();

   public BoardFrame() {
      jLabel.setIcon(new ImageIcon("an image here"));
      jPanelNorth.add(jLabel);

      jLabel2.setIcon(new ImageIcon("an image here"));
      jPanelSouth.add(jLabel2);

      mainPanel.add(jPanelNorth, BorderLayout.NORTH);
      mainPanel.add(jPanelSouth, BorderLayout.SOUTH);

      add(mainPanel);


      //where would I use this?
      //scrollPane.setViewportView();

   }
}

Upvotes: 0

Views: 2087

Answers (2)

Osama Najjar
Osama Najjar

Reputation: 72

it seems to use grid layout is much better than using border layout , in this case :

import java.awt.BorderLayout;
import javax.swing.*;

public class BoardFrame extends JFrame {
//1. use GridLayout with 2 rows and 1 column .
JPanel mainPanel = new JPanel(new GridLayout(2,1));

JLabel jLabel = new JLabel();
JPanel jPanelNorth = new JPanel();
JScrollPane scrollPane = new JScrollPane();

JLabel jLabel2 = new JLabel();
JPanel jPanelSouth = new JPanel();
JScrollPane scrollPane2 = new JScrollPane();

public BoardFrame() {
  jLabel.setIcon(new ImageIcon("an image here"));
  jPanelNorth.add(jLabel);

  jLabel2.setIcon(new ImageIcon("an image here"));
  jPanelSouth.add(jLabel2);


  //2.you should place .setViewportView() here :
  scrollPane.setViewportView(jPanelNorth);
  scrollPane2.setViewportView(jPanelSouth);


  mainPanel.add(scrollPane);//is in the top ("North")
  mainPanel.add(scrollPane2);//next ("South")

  //3.use setContentPane instead of add() 
  setContentPane(mainPanel);




}
}

Upvotes: 0

camickr
camickr

Reputation: 324118

Each panel contains a very large image>

//JPanel mainPanel = new JPanel(new BorderLayout());
JPanel mainPanel = new JPanel(new GridLayout(0, 1));

You may want to use a GridLayout so that each scroll pane takes up half the frame so as much of each image as possible is displayed.

//JScrollPane scrollPane = new JScrollPane();
JScrollPane scrollPane2 = new JScrollPane(jPanelNorth);

The easiest way to use the scroll pane is to create the scrollpane with the component you want displayed and the scrollpane will add the component to the viewport for you.

  //mainPanel.add(jPanelNorth, BorderLayout.NORTH);
  mainPanel.add(scrollPane); // don't need the constraint when using GridLayout.

Then you add the scrollPane to the main panel, since the scrollpane contains the panel with the image.

Upvotes: 2

Related Questions