-2

I am starting this new question because this question has been voted to close since it didnt have an MVCE.

I have included the question with an SSCCE now.

Please take a look at the below code

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

public class ScrollPaneTest {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            JTabbedPane tabbedPane=new JTabbedPane();


            tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
            tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

            frame.setContentPane(tabbedPane);
            frame.pack();
            frame.setVisible(true);
        }



        private JPanel getPanel() {
            JPanel panel=new JPanel();
            Box box = Box.createVerticalBox();
            for (int i = 1; i <= 100; i++) {
                box.add(new JLabel("This is Label #" + i));
            }
            panel.add(box);
            return panel;
        }
    });
  }
} 

When I run this code I get a frame shown as below

Last JLabel getting cut by taskbar.

In the same code if I add a

 frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

Then the frame comes above the task bar. I would like my JFrame to stop just above the taskbar of windows.

Community
  • 1
  • 1
sethu
  • 8,181
  • 7
  • 39
  • 65
  • 1
    Why not display those labels in a `JList`? – Andrew Thompson Jan 21 '14 at 01:51
  • @sethu please what do you really want to do – mKorbel Jan 21 '14 at 08:09
  • I would like my JFrame extend upto the taskbar in height. And the width should be just enough to fit the tabbed pane in it. When I add a panel into a scroll pane into a tabbed pane and place the tabbed pane in a frame, the last component in the panel is getting cut like it is shown in the image. – sethu Jan 21 '14 at 08:25

1 Answers1

1

This is basically a little hacky.

What this does is uses the screen bounds minus it's insets to calculate not only the height of the window, but also the position to encourage the window to appear within the available desktop space.

The main reason for this, is I have my taskbar pinned to the top of my screen...

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane = new JTabbedPane();

                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                Rectangle viewBounds = getScreenViewableBounds();
                frame.setSize(frame.getWidth(), viewBounds.height);
                frame.setLocation(viewBounds.x, viewBounds.y);
                frame.setVisible(true);
            }

            private JPanel getPanel() {
                JPanel panel = new JPanel();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

    public static Rectangle getScreenViewableBounds() {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        Rectangle bounds = new Rectangle(0, 0, 0, 0);

        if (gd != null) {

            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            bounds = gc.getBounds();

            Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

            bounds.x += insets.left;
            bounds.y += insets.top;
            bounds.width -= (insets.left + insets.right);
            bounds.height -= (insets.top + insets.bottom);

        }

        return bounds;

    }
}

You may also wish to consider using the Scrollable interface to restrict the viewable area, for example...

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.Scrollable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane = new JTabbedPane();

                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                frame.setVisible(true);
            }

            private JPanel getPanel() {
                JPanel panel = new ScrollablePane();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

    public static class ScrollablePane extends JPanel implements Scrollable {

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(200, 400);
        }

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

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

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

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    A `JList` seems perfect for this, with no hacks needed. See [my answer](http://stackoverflow.com/a/21247788/418556) on the original thread.. – Andrew Thompson Jan 21 '14 at 02:07
  • 1
    @AndrewThompson No dispute there but there's no context as to what the OP is actually trying to achieve, the labels could just be a representation of a more complex interface. But I agree, as it stands right now, a `JList` would seem to be more suitable. – MadProgrammer Jan 21 '14 at 02:10
  • @MadProgrammer.. thats true. The actual JPanel is more complex than repeated labels. I just used it to demonstrate whats happening. Your suggestion above of setting the bounds goes against the philosophy of not setting bounds on any swing component and use a layout manager instead. So if I get this right, in this situation, there is no other way apart from setting the bounds... right? – sethu Jan 21 '14 at 03:56
  • A window is a "different" beast. What I've done is only set the height to an arbitrary value based on the needs. But generally, if you can avoid, you should avoid suppling values to the size values where possible – MadProgrammer Jan 21 '14 at 03:59
  • @MadProgrammer .. thanks! I think this should work. Let me try this.. – sethu Jan 21 '14 at 08:26
  • @MadProgrammer thanks.. The first example works. – sethu Jan 24 '14 at 03:13
  • @sethu What ever gets you across the line... – MadProgrammer Jan 24 '14 at 03:16