4

first of all I must say that I have checked these questions and didn't find my answer :

1 , 2 , 3 , 4 , 5 , 6 , 7

and many other questions like so

also I have checked these tutorials and examples:

1 , 9 , 10 , 11

and many other sites. but I couldn't fix my problem.

and this is the simple kind of my code:

public class Question extends JFrame {
public Question() {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLayout(new BorderLayout());
    setSize(d.width, d.height);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(d.width, d.height));
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setLayout(new BoxLayout(panel, 1));
    for (int i = 0; i < 100; i++) {
        panel.add(new JButton("kjdh"));
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(new Dimension(500, 500));
    getContentPane().add(scrollPane);
    getContentPane().add(panel);
    setVisible(true);

}

public static void main(String[] args) {
    new Question();
} 
}

but the JScrollPane doesn't appear. I have tested many things. I have changed the way adding panel and scrollPane to my frame but it didn't work. can any one help me plz?

Community
  • 1
  • 1
Paniz
  • 594
  • 6
  • 19
  • Have you tried scrollPane.add(panel), and adding only the panel? – Anton D Aug 25 '13 at 17:28
  • Did you try the framework to calculate dimensions? And right, preferred size requires preferences configuration, use the full size. – Roman C Aug 25 '13 at 17:32
  • You already added the jpanel inside scrollpane so there is no need to add the jpanel again in frame. Avoid adding unnecessary code like adding jpanel twice. Just remove the line `getContentPane().add(panel)`. – Vighanesh Gursale Aug 25 '13 at 19:33
  • *"I have checked these questions.."* BTW - excellent question what with the extensive research and the code. – Andrew Thompson Aug 26 '13 at 03:39

2 Answers2

17
  1. Don't set a preferred size on the panel. See Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing? for the reasons why.
  2. Add only the scroll pane to the content pane.
    1. A content pane using the default layout (BorderLayout) will default to putting the component in the CENTER constraint if none is supplied, and the CENTER area can only accept a single component.
    2. Besides that, the panel has already been added to the scroll pane, it will already appear inside it, and can only appear in a single container.
  3. Don't extend frame, just use an instance of one.
  4. Don't setSize, but setExtendedState.
  5. GUIs should be constructed and updated on the EDT.
  6. A better close operation is DISPOSE_ON_CLOSE.

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

public class Question {

    public Question() {
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setLayout(new BoxLayout(panel, 1));
        for (int i = 0; i < 100; i++) {
            panel.add(new JButton("kjdh"));
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        f.getContentPane().add(scrollPane);
        f.pack();
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new Question();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    +1, much better as an answer instead of a comment. Although, I would bold 2 as well since both are need to fix the problem and the other are good programming practices. – camickr Aug 25 '13 at 17:49
  • @camickr Good point. I edited the answer to expand on both points and **emphasize** the 2nd. – Andrew Thompson Aug 25 '13 at 17:55
  • what if I want the program to be full screen? I set the size to make it fullscreen – Paniz Aug 25 '13 at 18:16
  • 1
    *"what if I want the program to be full screen?"* What ***happens*** if you run the code I posted? It looks 'full-screen' to me. – Andrew Thompson Aug 25 '13 at 18:42
  • Thanks for your helpful answer, yes it worked but when I add `JmenuBar` to it, the JMenuBar doesn't appear!should I ask it in another question? – Paniz Aug 26 '13 at 21:25
  • *"in another question?"* Sounds like a good plan. That is what I would advise. – Andrew Thompson Aug 26 '13 at 23:27
  • Do You know how to do it for panel.setLayout(null); f.setLayout(null); ? I want to manually set position for every element. – arrowman Jul 04 '16 at 16:17
  • 1
    @arrowman *"I want to manually set position for every element."* That's not something I'll spend (i.e. waste) time on. – Andrew Thompson Jul 05 '16 at 00:01
  • @AndrewThompson I know now that it is wasting of time. Some people informed me. But thanks:-) – arrowman Jul 05 '16 at 10:29
5

You've added an unecessary duplicate panel on the context pane. Instead of:

getContentPane().add(scrollPane);
getContentPane().add(panel);

use only

getContentPane().add(scrollPane);

It makes sense as a scrool pane is a container for a panel, so it's enough to add a container on the context pane.

Jk1
  • 11,233
  • 9
  • 54
  • 64
  • 2
    please without scrollPane.setPreferredSize(new Dimension(500, 500));, because is laid to JFrame.CENTER, then is pretty ignored – mKorbel Aug 25 '13 at 17:32
  • Agree with @mKorbel. See [Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (basically 'yes') and my example. No 'size guessing' needed. – Andrew Thompson Aug 25 '13 at 17:45