Layoutmanagers, JScrollPane and setLayout(null)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BSCode266
    New Member
    • Jan 2007
    • 38

    Layoutmanagers, JScrollPane and setLayout(null)

    Hey everyone,

    I'll cut right to the case. I have a JPanel and i call setLayout(null) on it. Simply because I would like to position everything myself and haven't seen a handy manager yet. Now i need to add this panel to a JScrollPane and this is where all the trouble starts. The JScrollPane simply doesn't like the absence of a LayoutManager.

    I have been trying to puzzle through this for hours now and could use some help. I have tried to write my own LayoutManager which only implements the preferredSize method completely from the LayoutManager interface, but couldn't find a way to make the JScrollPane notice the changes in size.

    I'd like to see a solution without a LayoutManager because I am really not too fond of these things. However I don't think there is one. Also if I am missing out a LayoutManager that would be perfect to have everything floating around the way I want it, feel free to point me in that direction. If anyone could show me a working preferredSize method that would also be fine.

    Thanks in advance,

    BSCode266
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Did you use a JViewport for your own JComponent? (i.e. wrap your component in the viewport and set the viewport in the scroll pane).

    kind regards,

    Jos

    Comment

    • BSCode266
      New Member
      • Jan 2007
      • 38

      #3
      I have done the following after your response:

      Code:
      JViewport viewport = new JViewport();
      viewport.setSize(frameWidth, frameHeight);
      viewport.setView(panel);
      
      JScrollPane scroll = new JScrollPane(viewport);
      This does display the upper elements of the panel but still does not scroll. I am not really that familiar with the JViewport and googling on it doesn't really give great examples.

      I did notice the method of JScrollPane called setViewport, however i have no idea what to fill in the constructor of JScrollPane if i use the viewport for the setViewport method.

      Am I doing something wrong?

      Comment

      • BSCode266
        New Member
        • Jan 2007
        • 38

        #4
        I really could use some help. Still haven't been able to find a solution for this.

        Does anyone perhaps have a sample of a preferredSize method for a custom LayoutManager which would work with a JScrollPane?

        Desperately in need of help,

        ~BSCode266

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by BSCode266
          I really could use some help. Still haven't been able to find a solution for this.
          We can't tell what your error is without seeing the relevant parts of your actual code. Have a look at this snippet:

          Code:
          		JFrame frame= new JFrame("test frame");
          		JPanel panel= new JPanel() {
          			public void paintComponent(Graphics g) {
          		
          				super.paintComponent(g);
          				g.setColor(Color.BLACK);
          				g.drawOval(0, 0, 200, 200);
          			}
          		};
          		
          		JLabel label= new JLabel("test");
          		
          		panel.add(label);
          		label.setBounds(400, 400, 60, 20);
          		
          		panel.setPreferredSize(new Dimension(500, 500));
          		panel.setLayout(null);
          		
          		JScrollPane sp= new JScrollPane(panel);
          		
          		frame.getContentPane().add(sp, BorderLayout.CENTER);
          		frame.pack();
          		frame.setVisible(true);
          This code runs and it draws a small circle at the top left and draws a JLabel with the text "test" near the bottom right corner. Resize the frame and see the scroll bars appear. Note that the panel doesn't have a layout manager.

          kind regards,

          Jos

          Comment

          • BSCode266
            New Member
            • Jan 2007
            • 38

            #6
            Thank you very much Jos. It was the preferredSize of the panel which was obviously way to small. Because if the panel is bigger then its preferredSize it doesn't have to scroll even tho there are object outside of the screen.

            Thanks alot!!!!

            Kind regards,

            BSCode266

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by BSCode266
              Thank you very much Jos. It was the preferredSize of the panel which was obviously way to small. Because if the panel is bigger then its preferredSize it doesn't have to scroll even tho there are object outside of the screen.

              Thanks alot!!!!
              That should teach you to set the layout manager to null ;-) Think twice before you do that because those layout managers can handle most (if not all) of that darn layout stuff for you. Think of different screen resolutions, *shudder*.

              kind regards,

              Jos

              Comment

              Working...