-1

Here I go again... battling with swings!!! So I'm creating an online test which will be displayed in an applet. The number of questions in the tests isn't fixed, so I need to ask questions according to the test. In order to display the questions I created a question jpanel that then I added to container panel which be displayed in the applet. For the container panel I'm using a boxlayout that allows me to stack questions one on top of the other.

My issue is that after adding more than 5 questions to the container panel the questions start overlapping. So can anyone guide me?

First, how can I avoid the overlapping? Second, does a jpanel have a fixed maximum size? Or is there a way I can make it big enough to fit all the test question in the panel container? I thought about embedding the panel in a jscrollpane or I don't know if once the container panel is embedded in the applet it will scroll down as I scroll down the browser... Thank you guys for any help

Here's a pic of what it looks like when there aren't many questions...

enter image description here

Here is the code...

 public class Test extends JPanel {

    public Test() {
        setLayout(null);            
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(0, 5, 712, 1200);
        add(scrollPane);

        JPanel panel = new JPanel();
        scrollPane.setViewportView(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        MultipleChoice q1 = new MultipleChoice();
        panel.add(q1);
        MultipleChoice q2 = new MultipleChoice();
        panel.add(q2);
        MultipleChoice q3 = new MultipleChoice();
        panel.add(q3);
        MultipleChoice q4 = new MultipleChoice();
        panel.add(q4);
        MultipleChoice q5 = new MultipleChoice();
        panel.add(q5);    
    }    
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
JLA
  • 329
  • 1
  • 6
  • 12
  • 3
    Number one: don't do `setLayout(null);`. Just don't. It will screw you over every time. Number two: if you need our help, truly need help, then please put in the effort to post an [sscce](http://sscce.org). Else you are forcing us to guess, and that's not nice. – Hovercraft Full Of Eels Jul 13 '13 at 02:03
  • don't know how to make it sscce since I'm not even sure what's the correct approach to fix it – JLA Jul 13 '13 at 02:17
  • Without our being able to reproduce your error, I doubt that we'll be able to fully understand your error. Do your question JPanels use null Layout? – Hovercraft Full Of Eels Jul 13 '13 at 02:19
  • I know buddy... But how can I be more specific when I don't know much about swings? I'm using Windowbuilder and that's the layout given when setting a container layout to absolute. – JLA Jul 13 '13 at 02:24
  • 1
    you can accept previous question if you don't mind xD http://stackoverflow.com/questions/17621185/adding-jpanels-through-a-loop – nachokk Jul 13 '13 at 02:34
  • 1
    ohh ... this is basically a gimme-the-codz-coz-i-don-hav-de-time type of question? -1 learn or perish – kleopatra Jul 13 '13 at 10:17

1 Answers1

2

I'm guessing, and all I can do is guess without an sscce, but if your MultipleChoice JPanel uses null layout, then it won't be able to give a decent preferredSize to your layout managers allowing for overlapping components. If so, again the solution is to not use null layout, almost ever.

You state in comment:

I know buddy... But how can I be more specific when I don't know much about swings? I'm using Windowbuilder and that's the layout given when setting a container layout to absolute.

  • "buddy"?
  • regarding, "when I don't know much about swings": then learn about Swing. Go to the Layout Manager Tutorials and read up on the layout managers.
  • regarding, "I'm using Windowbuilder and that's the layout given when setting a container layout to absolute.": part of your problem, as you yourself admit, is that you don't yet fully understand Swing and in particular use of its layout managers, and one reason for this problem is that you're using a tool that buffers you from having to understand this. I urge you not to use WindowBuilder. Again read up on the layout managers and learn how to use them. You will not be sorry that you've done this.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Trust me, I'm eager to learn everything about it... but I got big time constraints since this is a project for a SUMMER course (a whole semester squeezed in a couple of weeks) so I really don't have the time to get a full understanding of every single aspect of Swings; but I understand your point... buddy - I think it's used when trying to be friendly. Thank you Hovercraft – JLA Jul 13 '13 at 02:44
  • 1
    @user2108393: You will need to learn ***some*** important Swing concepts if you're going to code your project successfully. I would suggest that layouts are of critical importance and thus should be on the short list of topics that you plan to study soon (like starting tonight). Otherwise, you won't have enough knowledge to post an answerable question, as you're finding out. – Hovercraft Full Of Eels Jul 13 '13 at 02:47
  • 1
    @user2108393 tonight friday night? can wait tomorrow, but what hovercraft say is true, layout manager are a must, and they areN'T too difficult , the code will get more simpler cause you delegate responsabilities to layoutManagers. if you delete the line setLayout(null) you'll see that will be better, windowsBuilder sure has an option to setLayout – nachokk Jul 13 '13 at 02:56
  • 1
    @nachokk: He's asking us to help him with code on a Friday night, so it's not too much to ask him to study on a Friday night, right? – Hovercraft Full Of Eels Jul 13 '13 at 02:58