Question

I'm working right now on a project, and I can't find a solution for my problem.

So here is my problem: I have a JFrame, then I add a container JPanel, and I add 2 other JPanel to this container panel , first panel (InputPanel) is for user input, second panel (Board) for displaying the specified algorithm based on the user input.

But the displayed algorithm is too large, so I thought I will add a JScrollPane to the DisplayPanel, but it didn't worked like I thought it would have. Here is a picture, the red rectangle is the area what changed after I added the JScollPane: enter image description here

Notice that at my Board class I override the paintComponent(Graphics g) to draw the algorithm.

My code in the main frame:

    container = new JPanel();
    container.setLayout(new BorderLayout());

    board = new Board();
    container.add(board, BorderLayout.CENTER);

    inputPanel = new InputPanel(board);
    container.add(inputPanel, BorderLayout.SOUTH);

    pane = new JScrollPane(board);
    pane.setViewportView(board);
    pane.setPreferredSize(new Dimension(700, 0));
    container.add(pane, BorderLayout.WEST);

    add(container);

My initial plan was to add a horizontal scrollpane to the Board panel. Can somebody post an example code, or point out for my problem pls?

Was it helpful?

Solution

You added the board twice to container. Add the JScrollPane to the center of container, and continue to pass board to the constructor of your JScrollPane. Don't add the board individually to the container if you want to add it via a JScrollPane.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top