sharpGregor
sharpGregor

Reputation: 21

Add a JScrollpane that split between JSplitpane and JPanel?

I'm just trying to insert a scrollpane, but I'm not going to get it. I have 3 areas defined by a panel. The left area to the middle is separated by a splitpane. The middle area is separated by a scrollpane. Well clear vertical from top to bottom. I hope you can help me because I am currently really frustrated and no answer to my solution.

Here is a Picture which shows what my goal is to create.

enter image description here

In this Method we define the split and return this to the JFrame.

 public JSplitPane defineSplit() {
    JPanel leftPanel = leftArea();
    JPanel centerPanel = middleArea();
    JPanel rightPanel = rightArea();
    JSplitPane splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
    JSplitPane splitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitPane1,rightPanel);
    splitPane1.setVisible(true);
    splitPane2.setVisible(true);
    return splitPane2;
}

Here is my Method which creates the content and return the Information to defineSplit().

public JPanel leftArea() {
    JPanel panel = new JPanel(new FlowLayout());
    panel.add(new JLabel("left area"));
    return panel;
}

public JPanel middleArea() {
    JPanel panel = new JPanel(new FlowLayout());
    panel.add(new JLabel("middle area"));
    return panel;
}

public JPanel rightArea() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(new Label("right area"));
    return panel;
}

The last one is my Frame. My frame is in a extra class. Here you can just see the method.

public void createFrame() {
    setJMenuBar(AnimeMenuBar.getInstance().createMenu(this));
    setTitle("Anime");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(1000, 500));
    JSplitPane splitPane = content.defineSplit();
    add(splitPane);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

Upvotes: 2

Views: 374

Answers (1)

UkFLSUI
UkFLSUI

Reputation: 5672

defineSplit() method:

public JSplitPane defineSplit() {
    JPanel leftPanel = leftArea();
    JScrollPane centerScrollPane = middleArea();
    JPanel rightPanel = rightArea();
    JSplitPane splitPane1 = new JSplitPane();
    JSplitPane splitPane2 = new JSplitPane();

    splitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
    splitPane1.setRightComponent(splitPane2);
    splitPane1.setLeftComponent(leftPanel);
    splitPane2.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
    splitPane2.setRightComponent(rightPanel);
    splitPane2.setLeftComponent(centerPanel);

    return splitPane1;
}

middleArea() method:

public JScrollPane middleArea() {
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    return scrollPane;
}

Upvotes: 1

Related Questions