Java's Swing seems to be changing the layout from Card Layout
I am having some pretty strange symptoms with JPanels and CardLayout. Essentially, I have cards which act as "pages" since I can only hold 12 cells on the grid, and I display each page as a card, and clicking -> and <- buttons change pages accordingly (or that's the idea). Because this data is representing a changing model, every five seconds I was going to have new cards be added displaying updated information, and simply remove the old pages.
Essentially, I construct the initial pages when the program starts:
public OrderSimPanel() {
super( new CardLayout() );
// Create the map
frames = new TreeMap<Integer, String>();
// Update and draw
refreshRelevantOrders();
drawPanel();
// Set a timer for future updating
java.util.Timer timer = new java.util.Timer();
timer.schedule(new UpdateTask(), 5000);
System.out.println("Constructor DOES get called");
System.out.println("Panel type is " + this.getLayout().getClass().getName() );
}
Where refershReleventOrders() just updates an List, and drawPanel() is something like this:
private void drawPanel() {
System.out.println("Panel type is " + this.getLayout().getClass().getName() );
// Set all old frames to deprecated
for( Integer k : frames.keySet() ) {
String o = frames.get( k );
frames.remove(k);
k = -k;
frames.put(k, o);
开发者_JAVA百科 }
// Frame to add to
JPanel frame = new JPanel( new GridLayout(3,4) );
int f = 0;
// Create new cells in groups of 12
for( int i = 0; i < orders.size(); i++ ) {
// Pagination powers activate!
int c = i % 12;
f = i / 12;
// Create a new panel if we've run out of room on this one
if ( c == 0 && i > 0 ) {
// Add old frame
String id = new Double( Math.random() ).toString();
frames.put(f, id);
add( frame, id );
// Create new one
frame = new JPanel( new GridLayout(3,4) );
}
// Add the order cell to the panel
frame.add(new OrderCellPanel( orders.get(i) ));
i++;
}
// Add last frame
String id = new Double( Math.random() ).toString();
frames.put(f, id);
add( frame, id );
// Set active frame to 0'th frame
((CardLayout)(this.getLayout())).show(this, frames.get(0));
// Erase other frames and from map
for( Integer k : frames.keySet() ) {
if( k < 0 ) {
this.remove(0);
frames.remove(k);
}
}
}
The timer task created in the constructor calls refreshRelevantOrders() an drawPanel(). The initial print output looks like this:
Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout
But when the timer executes, this is displayed:
Panel type is javax.swing.GroupLayout
And of course, the casting code in drawPanel() fails. Thanks for any ideas!
Override setLayout() method, and print the old / new layout class names there (before or after calling the super implementation). This way you will see which part of your code sets GroupLayout on the panel.
精彩评论