Hi all,
The code below shows what I think is a bug with TableLayout. The TableLayout class file--which is needed to try out the code given below--can be downloaded from here:
http://java.sun.com/products/jfc/tsc/articles/tablelayout/Download.html The problem is this: If you add a panel to a table layout in location (x1,y1), then remove it, and then add the same panel to location (x2,y2), it instead gets put back in location (x1,y1). Does anyone know a fix? Does anyone have suggestions?
After compiling the code below, run it in two ways. Type "java Gui" to run it and show the problem of re-adding a panel. Type "java Gui arg" to run it and show how if a panel not yet used is added, it goes in a different place from one that has been used.
Thanks,
Andy
CODE:
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
// TableLayout stuff
import layout.*;
class Gui extends JPanel {
protected JPanel panel1, panel2, panel3, panel4;
protected double myLayout[][] = {{0.5, 0.5}, {0.5, 0.5}};
protected TableLayout tableLayout;
protected Gui() {
super();
tableLayout = new TableLayout(myLayout);
this.setLayout(tableLayout);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel1.setBackground(new Color(200,0,0,255));
panel2.setBackground(new Color(0,200,0,255));
panel3.setBackground(new Color(0,0,200,255));
panel4.setBackground(new Color(150,150,0,255));
}
protected void render(int i, boolean flag) {
if (i == 0) {
this.add(panel1, "0,0,0,1");
this.add(panel2, "1,0");
this.add(panel3, "1,1");
}
else {
this.remove(panel1);
this.remove(panel2);
this.remove(panel3);
if (!flag) this.add(panel3, "0,0,1,1");
else this.add(panel4, "0,0,1,1");
}
this.repaint();
this.validate();
}
public static void main(String args[]) {
boolean flag = false;
if (args.length != 0) flag = true;
Gui gui = new Gui();
JFrame f = new JFrame();
f.getContentPane().add(gui);
f.setSize(640, 480);
f.setVisible(true);
for (int i=0; i<2; i++) {
try { Thread.sleep(1000); } catch (Exception e) { }
gui.render(i, flag);
}
}
}
[ May 12, 2002: Message edited by: Andrew Keidel ]