SoupMonster
SoupMonster

Reputation: 109

Java Swing: Layout Manager like "North" in BorderLayout needed

I would like a Layout Manager like the Border Layout Manager, except only having a single region: North.

The North region of the Border Layout Manager is working fine for me (i.e. taking up the correct space), but, the center is occupying all available space (see the blue area below). Ideally I would like the center to occupy zero space.

Is this possible?

Thanks.

Example 1 of 2 (pic)

example

Example 2 of 2 (code)

import java.awt.*;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;

public class test
{
    public static void main(String[] args)
    {
         SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            JFrame j = new JFrame("Test");
        WorkBench right = new WorkBench();

        JScrollPane sp 
            = new JScrollPane(
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        sp.setAutoscrolls(false);
        sp.setViewportView(right);

        j.getContentPane().add(sp, BorderLayout.CENTER);

        right.addPortal(getNewPortalPane());
        right.addPortal(getNewPortalPane());

        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() {
                sp.getViewport().setViewPosition(new java.awt.Point(0, 0));
            }
        });


        // center the jframe, then make it visible
        j.setSize(800, 600);
        j.setLocationRelativeTo(null);
        j.setVisible(true);
      }
    });
  }


public static JPanel getNewPortalPane()
{
    JEditorPane html_box = new JEditorPane();
    html_box.setEditorKit(new HTMLEditorKit());
    html_box.setOpaque(true);
    html_box.setBackground(Color.WHITE);

    html_box.setContentType("text/html");

    String htmlString = "<html>\n"
        + "<body>\n"
        + "<h1>Welcomfve!  sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n"
        + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n"
        + "<p>This is some sample text  sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n"
        + "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n"
        + "</body>\n";

    html_box.setText(htmlString);


    JPanel p = new JPanel(new BorderLayout());
    p.setBackground(Color.blue);
    p.add(html_box, BorderLayout.NORTH);

    return p;
}

public static class WorkBench extends JPanel implements Scrollable
{
    private Box vertical_box = null;

    public WorkBench()
    {
        setLayout(new BorderLayout());

        this.vertical_box = Box.createVerticalBox();
        add(this.vertical_box , BorderLayout.CENTER);
    }

    public void addPortal(JPanel portal)
    {
        this.vertical_box.add(portal);

        validate();
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() 
    {
        return getPreferredSize();
    }


    @Override
    public int getScrollableUnitIncrement(
            Rectangle visibleRect,
            int orientation, 
            int direction) 
    {
        return 20;
    }


    @Override
    public int getScrollableBlockIncrement(
            Rectangle visibleRect,
            int orientation, 
            int direction)
    {
        return 60;
    }


    @Override
    public boolean getScrollableTracksViewportWidth() 
    {
        return true;
    }


    @Override
    public boolean getScrollableTracksViewportHeight()
    {
         if (getParent() instanceof JViewport)
         {
             return (((JViewport)getParent()).getHeight() > getPreferredSize().height);
         }

         return false;
    }
}

}

Upvotes: 0

Views: 287

Answers (1)

SamYonnou
SamYonnou

Reputation: 2068

There is probably a more elegant solution somewhere, but what I've found works is using BorderLayout.NORTH to add an inner panel, set the inner panel's layout to BoxLayout using PAGE_AXIS, and then add components to the inner panel. So in your example it would be (edited to fix scrollbar) :

public class test {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        JFrame j = new JFrame("Test");
        WorkBench right = new WorkBench();

        final JScrollPane sp = new JScrollPane(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        sp.setAutoscrolls(false);
        sp.setViewportView(right);

        j.getContentPane().add(sp, BorderLayout.CENTER);

        right.addPortal(getNewPortalPane());
        right.addPortal(getNewPortalPane());

        SwingUtilities.invokeLater(new Runnable() {

          public void run() {
            sp.getViewport().setViewPosition(new java.awt.Point(0, 0));
          }
        });

        // center the jframe, then make it visible
        j.setSize(800, 600);
        j.setLocationRelativeTo(null);
        j.setVisible(true);
      }
    });
  }

  public static JPanel getNewPortalPane() {
    JEditorPane html_box = new JEditorPane();
    html_box.setEditorKit(new HTMLEditorKit());
    html_box.setOpaque(true);
    html_box.setBackground(Color.WHITE);

    html_box.setContentType("text/html");

    String htmlString = "<html>\n"
        + "<body>\n"
        + "<h1>Welcomfve!  sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n"
        + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n"
        + "<p>This is some sample text  sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n"
        + "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n"
        + "</body>\n";

    html_box.setText(htmlString);

    JPanel p = new JPanel(new BorderLayout());
    p.setBackground(Color.blue);
    p.add(html_box, BorderLayout.NORTH);

    return p;
  }

  public static class WorkBench extends JPanel implements Scrollable {

    private final JPanel inner;

    public WorkBench() {
      setLayout(new BorderLayout());
      inner = new JPanel();
      inner.setLayout(new BoxLayout(inner, BoxLayout.PAGE_AXIS));
      add(inner, BorderLayout.NORTH);
    }

    public void addPortal(JPanel portal) {
      inner.add(portal);
      validate();
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
      return getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
        int orientation, int direction) {
      return 20;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
        int orientation, int direction) {
      return 60;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
      return true;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
      if (getParent() instanceof JViewport) {
        return (((JViewport) getParent()).getHeight() > getPreferredSize().height);
      }

      return false;
    }
  }
}

Upvotes: 1

Related Questions