I have a window(frame) that has three panels - top, middle and bottom. When the window is shrunk vertically, I need a scrollbar to show up for the middle panel only. So the maximum vertical shrink possible would be the sum of the top and bottom panels plus some small amount (to see the middle panel scroll bar). So the top and bottom panels would always show completely (height wise). Please review my code and suggest corrections. I've tried couple of different ways but without success....The code below is my most recent attempt.
Thanks.....
import java.awt.*;
import java.awt.Color.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
public class PanelResizingA extends JPanel implements MouseListener, MouseMotionListener
{
/**
* @param args
*/
public static void main(String[] args)
{
JFrame frame = new JFrame ("All Panels");
PanelResizingA allThreePanels = new PanelResizingA();
JScrollPane allHorizontalScroll = new JScrollPane();
//frame.add(allHorizontalScroll);
allHorizontalScroll.setViewportView(allThreePanels);
allHorizontalScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
allHorizontalScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
frame.getContentPane().add(allHorizontalScroll);
frame.setVisible(true);
frame.pack();
}
private JPanel topPanel;
private JPanel midPanel;
private JPanel bottomPanel;
private JPanel allPanels;
private JScrollPane midVerticalScroll;
private JScrollPane allHorizontalScroll;
private JLabel posnCheckLabel;
private int panelWidth = 0;
private int topPanelHt = 0;
private int midPanelHt = 0;
private int bottomPanelHt = 0;
private int allPanelsHt = 0;
private Point pointPressed;
private Point pointReleased;
public PanelResizingA()
{
createAllPanels();
}
private void createAllPanels()
{
topPanel = new JPanel();
midPanel = new JPanel();
bottomPanel = new JPanel();
allPanels = new JPanel();
posnCheckLabel = new JLabel("Label in Center");
panelWidth = 300;
topPanelHt = 150;
midPanelHt = 200;
bottomPanelHt = 150;
allPanelsHt = topPanelHt + midPanelHt + bottomPanelHt;
//topPanel.setMinimumSize(new Dimension(panelWidth-150, topPanelHt));
//topPanel.setMaximumSize(new Dimension(panelWidth, topPanelHt));
topPanel.setPreferredSize(new Dimension(panelWidth, topPanelHt));
topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
midPanel.setMinimumSize(new Dimension(panelWidth-150, midPanelHt-150));
midPanel.setMaximumSize(new Dimension(panelWidth, midPanelHt));
midPanel.setPreferredSize(new Dimension(panelWidth, midPanelHt-3));
midPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
midPanel.setLayout(new BorderLayout());
midPanel.add(posnCheckLabel, BorderLayout.CENTER);
//midPanel.add(new PanelVerticalDragger(midPanel), BorderLayout.SOUTH);
//bottomPanel.setMinimumSize(new Dimension(panelWidth-150, bottomPanelHt));
//bottomPanel.setMaximumSize(new Dimension(panelWidth, bottomPanelHt));
bottomPanel.setPreferredSize(new Dimension(panelWidth, bottomPanelHt));
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
allPanels.setMinimumSize(new Dimension (panelWidth-150, allPanelsHt-300));
allPanels.setMaximumSize(new Dimension(panelWidth+25, allPanelsHt+25));
allPanels.setPreferredSize(new Dimension(panelWidth, allPanelsHt));
midVerticalScroll = new JScrollPane();
//midPanel.add(midVerticalScroll);
midVerticalScroll.setViewportView(midPanel);
midVerticalScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
midVerticalScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
allPanels.setLayout(new BoxLayout(allPanels,BoxLayout.Y_AXIS));
allPanels.add(topPanel);
allPanels.add(midPanel);
allPanels.add(bottomPanel);
this.add(allPanels);
addMouseListener(this);
addMouseMotionListener(this);
}
private void updateCursor(boolean on)
{
if (on)
{
setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
}
else
{
setCursor(null);
}
}
@Override
public void mousePressed(MouseEvent e)
{
pointPressed = e.getLocationOnScreen();
updateCursor(true);
}
@Override
public void mouseDragged(MouseEvent e)
{
mouseReleased(e);
pointPressed = e.getLocationOnScreen();
}
@Override
public void mouseReleased(MouseEvent e)
{
pointReleased = e.getLocationOnScreen();
Dimension allPanelsPrefSize = this.allPanels.getPreferredSize();
Dimension midPanelPrefSize = this.midPanel.getPreferredSize();
Dimension allPanelsSize = this.allPanels.getSize();
Dimension allPanelsMinSize = this.allPanels.getMinimumSize();
int midPanelPrefHt = midPanelPrefSize.height;
int midPanelPrefWidth = midPanelPrefSize.width;
int maxHtDelta = allPanelsPrefSize.height - allPanelsMinSize.height;
//int deltaY = pointPressed.y - pointReleased.y;
Point panelLocation = this.getLocation();
Dimension size = this.getSize();
if (size.height < allPanelsSize.height)
{
int deltaY = pointPressed.y - pointReleased.y;
if (deltaY < maxHtDelta)
{
midPanelPrefHt = midPanelPrefHt-deltaY;
}
else {midPanelPrefHt = this.midPanel.getMinimumSize().height;}
this.midPanel.setPreferredSize(new Dimension(midPanelPrefWidth, midPanelPrefHt));
this.midVerticalScroll.setViewportView(this.midPanel);
allPanels.setLayout(new BoxLayout(allPanels,BoxLayout.Y_AXIS));
allPanels.add(topPanel);
allPanels.add(midVerticalScroll);
allPanels.add(bottomPanel);
}
midVerticalScroll.revalidate();
pointPressed = null;
pointReleased = null;
}
@Override
public void mouseEntered(MouseEvent e)
{
updateCursor(true);
}
@Override
public void mouseExited(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e)
{
}
}
Edited by: 799076 on Oct 8, 2010 12:53 PM
Edited by: 799076 on Oct 8, 2010 12:55 PM