I’ve a problem with the layout of this applet. Its got a List next to a Canvas in a border layout. Now sometimes when I run it the list is very narrow, 51 pixels wide. Other times with a tiny change in the code it works fine and the list is wide enough to display the entries. The list is populated from an array and commenting out one item from the array is
enough to change behavior. To be precise commenting this line fixes the problem
{"---- Lissajous curves ","t","t","1","1"}, // commenting this line fixes problem
I suspect this is a bug, but it would be nice to know if its reproducible or if I’m doing something silly. Also not quite sure of right place to submit a bug, I’m using the standard Java on Mac OS X 10.6.8
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.List;
import javax.swing.SwingUtilities;
public class Zap extends Applet {
private static final long serialVersionUID = 330L;
String[][] equations = new String[][]{
// {"Line ----------------------","3 t-1","t+1"},
{"Line","3 t-1","t+1"},
{"Circle","cos(t)","sin(t)","-pi","pi"},
{"Sq Circle","cos(t)","2 sin(t)","-pi","pi"},
{"Ellipse","2 cos(t)","sin(t)","-pi","pi"},
{"Cycloid","t-sin(t)","1-cos(t)","-20","20"},
{"---- Hypocycloids ----"},
{"Deltoid","2 cos(t)+cos(2t)","2 sin(t)-sin(2t)","-pi","pi"},
{"Astroid","cos(t)^3","sin(t)^3","-pi","pi"},
{"Hypocycloids 5","cos(t) + cos(4 t)/4","sin(t) - sin(4 t)/4","-pi","pi" },
{"---- Epicycloids ----"},
{"Cardioid",
"cos(t) - cos(2 t)/2",
"sin(t) - sin(2 t)/2","-pi","pi" },
{"Nephroid",
"cos(t) - cos(3 t)/3",
"sin(t) - sin(3 t)/3","-pi","pi" },
{"Epicycloid 3",
"cos(t) - cos(4 t)/4",
"sin(t) - sin(4 t)/4","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 4",
"cos(t) - cos(5 t)/5",
"sin(t) - sin(5 t)/5","-pi","pi" },
{"Epicycloid 5",
"cos(t) - cos(6 t)/6",
"sin(t) - sin(6 t)/6","-pi","pi" },
{"---- Lissajous curves ","t","t","1","1"}, // commenting this line
{"Lissajous 1,2","sin(t)","sin(2 t)","-pi","pi"},
{"Lissajous 1,3","sin(t-pi/2)","sin(3 t)","-pi","pi"},
{"Lissajous 1,4","sin(t)","sin(4 t)","-pi","pi"},// {"a Lissajousxxxxxxxxxxb",""},
{"Lissajous 2,1","sin(2t)","sin(t)","-pi","pi"},
{"Lissajous 2,2","sin(2t-pi/4)","sin(2t)","-pi","pi"},
{"Lissajous 2,3","sin(2t-pi/3)","sin(3t)","-pi","pi"},
{"Lissajous 3,4","sin(3t)","sin(4t)","-pi","pi"},
};
/** The canvas for plotting the graph */
private Canvas graphCanvas;
/** List of equations */
List list = new List();
/**
* Initializes the applet FunctionPlotter
*/
@Override
public void init () {
try {
initComponents();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets the layout of the applet window to BorderLayout, creates all
* the components and associates them with event listeners if necessary.
* @param j
* @throws JepException
*/
private void initComponents () {
setLayout(new BorderLayout());
// GridBagConstraints gbc1 = new GridBagConstraints(0, 0, 1, 1,0.9, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0 , 0) ;
// GridBagConstraints gbc2 = new GridBagConstraints(1, 0, 1, 1, 0.1, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
// setBackground (java.awt.Color.white);
// list.addItemListener(this);
list.setMinimumSize(new Dimension(220, 200));
list.setSize(new Dimension(220, 200));
list.setPreferredSize(new Dimension(220, 200));
System.out.println("list "+list.getBounds());
System.out.println("list ps"+list.getPreferredSize());
//add(list,gbc2);
add("East",list);
for(String[] eles:equations) {
list.add(eles[0]);
}
// create the graph canvas and add it
graphCanvas = createGraphCanvas();
//add (graphCanvas,gbc1);
add("Center",graphCanvas);
System.out.println("list ps before validate"+list.getPreferredSize());
list.validate();
this.validate();
System.out.println("list ps after validate"+list.getPreferredSize());
System.out.println(((Object)list).toString());
System.out.println("\n\n");
}
protected Canvas createGraphCanvas() {
Canvas gc = new Canvas();
gc.setPreferredSize(new Dimension(300,300));
gc.setSize(new Dimension(300,300));
System.out.println("PC size "+gc.size());
return gc;
}
public static void main(String argv[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
protected static void createAndShowGUI() {
Frame f = new Frame();
f.setSize(600, 400);
Zap pp = new Zap();
f.add(pp);
pp.init();
System.out.println("pack");
//f.pack();
System.out.println("setVis");
f.setVisible(true);
System.out.println("setVis done");
}
}
Try this (Swing based) variant of the list.