O balanço de Java parece estar mudando o layout do layout do cartão
-
25-09-2019 - |
Pergunta
Estou tendo alguns sintomas bastante estranhos com jpanels e cardlayout. Essencialmente, eu tenho cartões que atuam como "páginas", pois só posso segurar 12 células na grade, e os botões exibo cada página como um cartão e clicando -> e < - os botões alteram as páginas de acordo (ou essa é a ideia). Como esses dados estão representando um modelo em mudança, a cada cinco segundos, eu teria novos cartões sendo adicionados exibindo informações atualizadas e simplesmente removendo as páginas antigas.
Essencialmente, construo as páginas iniciais quando o programa iniciar:
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() );
}
Onde refershreleventorders () apenas atualiza uma lista, e drawPanel () é algo assim:
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);
}
// 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);
}
}
}
A tarefa do timer criada no construtor chama refreshRelevantorders () um drawPanel (). A saída inicial da impressão se parece com o seguinte:
Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout
Mas quando o timer é executado, isso é exibido:
Panel type is javax.swing.GroupLayout
E, claro, o código de elenco em drawPanel () falha. Obrigado por quaisquer idéias!
Solução
Substitua o método setLayout () e imprima os nomes da classe de layout antigos / novos lá (antes ou depois de chamar a super implementação). Dessa forma, você verá qual parte do seu código define o GrouPlayout no painel.