Re: How do I anchor JPanel location?
WTodd wrote:
There is a panel within each option pane tab. There are three
collapsable subpanels within each panel. > On tab 2, the subpanels center themselves vertically, with whatever
spacing is defined by the expanded subpanel(s) on the first tab.
[snip]
As I mentioned above, I would prefer to have each tab maintain it's
own sizing, but am content with just keeping the subpanels aligned
to the top left of the parent JPanel (which is within the
JOptionPane).
Maybe you should create an SSCCE
Here's one I threw together for you
-----------------------------------8<----------------------------------------
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Shrinkables {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Shrinkables();
}
});
}
Shrinkables() {
JTabbedPane tabPane = new JTabbedPane();
tabPane.add("Tab 1", new JScrollPane(new GroupPanel()));
tabPane.add("Tab 2", new JScrollPane(new GroupPanel()));
JFrame f = new JFrame("Layout problem");
f.add(tabPane);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(500, 300)); // ugh!
f.pack();
f.setVisible(true);
}
class GroupPanel extends JPanel {
GroupPanel() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(new ShrinkablePanel("subPanel1", Color.CYAN));
add(new ShrinkablePanel("subPanel2", Color.GREEN));
add(new ShrinkablePanel("subPanel3", Color.YELLOW));
//add(Box.createVerticalGlue());
}
}
class ShrinkablePanel extends JPanel implements ActionListener {
boolean shrunk = true;
JTextArea area = new JTextArea();
String essay =
"Lorem ipsum dolor sit amet, consetetur sadipscing\n"
+ " elitr, sed diam nonumy eirmod tempor invidunt ut\n"
+ "labore et dolore magna aliquyam erat, sed diam\n"
+ "voluptua. At vero eos et accusam et justo duo\n"
+ "dolores et ea rebum. Stet clita kasd gubergren, no\n"
+ "sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
+ "Lorem ipsum dolor sit amet, consetetur sadipscing";
ShrinkablePanel(String name, Color color) {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
setBackground(color);
JButton b = new JButton(name);
b.addActionListener(this);
b.setAlignmentY(Component.TOP_ALIGNMENT);
area.setAlignmentY(Component.TOP_ALIGNMENT);
area.setBackground(color);
add(b);
add(area);
}
public void actionPerformed(ActionEvent e) {
if (shrunk) {
area.setText(essay);
} else {
area.setText("");
}
shrunk = !shrunk;
}
} // ShrinkablePanel
} // Shrinkables
------------------------------------- 8< -------------------------------