Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- From: Daniel Moyne <daniel.moyne@xxxxxxx>
- Date: Mon, 22 Dec 2008 11:55:35 +0100
John B. Matthews wrote:
In article <494e4d00$0$28668$7a628cd7@xxxxxxxxxxxxxxxxxxxxx>,John what do you mean by adjust for :
Daniel Moyne <daniel.moyne@xxxxxxx> wrote:
[...] for the time being I am thinking of using absolute layout with
the need to calculate position of each marriage objects taking into
account their widths and heights.
I am wary of absolute layouts; I tend to prefer a layout that "breathes"
appropriately when the container is resized. One approach is a nested
BoxLayout in a vertical layout, which adjusts to the preferred height
and maximum width of each component. It's a way to accommodate the
slightly different font metrics extant among implementations. The
example uses vertical glue, but struts are an alternative:
<code>
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
/**
* @author John B. Matthews
*/
public class BoxTest {
public static final Random random = new Random();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
(new BoxTest()).create();
}
});
}
void create() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Box.createVerticalGlue());
for (int i = 0; i < 4; i++) {
panel.add(new SizePanel());
panel.add(Box.createVerticalGlue());
}
JFrame f = new JFrame("BoxTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
/**
* A SizePanel has a label showing its current size,
* as well as a variable number of text items.
*/
class SizePanel extends JPanel {
private static final String text =
"Sed ut perspiciatis unde omnis iste natus error sit.";
private final JLabel sizeLabel = new JLabel("Size:");
public SizePanel() {
this.setLayout(new GridLayout(0, 1));
this.add(sizeLabel);
int count = BoxTest.random.nextInt(4) + 1;
for (int i = 0; i < count; i++) {
this.add(new JLabel(text));
}
this.setBorder(BorderFactory.createCompoundBorder(
new LineBorder(Color.blue), new EmptyBorder(3,3,3,3)));
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int w = e.getComponent().getWidth();
int h = e.getComponent().getHeight();
sizeLabel.setText("Size: " + w + "\u00d7" + h);
}
});
}
}
</code>
.. width if for example an object inside the container becomes wider ?,
.. height if I have to add an extra object vertically ?.
Daniel
--
Daniel Moyne (Nulix) \\|||// Machine : x86_64
Distribution : Ubuntu Feisty Fawn / --- \ ATI Radeon X300
kernel : 2.6.20-16-generic (' o-o ') KDE 3.5.7
--------------------------------oOO-(_)-OOo---------------------------------
.
- Follow-Ups:
- Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- From: John B. Matthews
- Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- References:
- Prev by Date: Re: trouble compiling prog that contains strings with accented characters
- Next by Date: Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- Previous by thread: Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- Next by thread: Re: [handling data](1)how to go handle panel objects for edition inside a panel container
- Index(es):