Question

J'ai quelques symptômes assez étranges avec JPanels et CardLayout. Essentiellement, j'ai des cartes qui agissent comme des « pages » depuis que je ne peux contenir 12 cellules sur la grille, et j'afficher chaque page comme une carte, et en cliquant sur -> et <- boutons changent pages en conséquence (ou qui est l'idée). Parce que ces données représente un modèle changeant, toutes les cinq secondes je vais avoir de nouvelles cartes ajoutées afficher des informations mises à jour et supprimer simplement les anciennes pages.

Pour l'essentiel, je construis les premières pages lorsque le programme démarre:

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() );
}

Où refershReleventOrders () met à jour une liste juste et drawPanel () est quelque chose comme ceci:

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);
        }
    }
}

La tâche de minuterie créée dans le constructeur appelle refreshRelevantOrders () un drawPanel (). La sortie d'impression initiale ressemble à ceci:

Panel type is java.awt.CardLayout
Constructor DOES get called
Panel type is java.awt.CardLayout

Mais lorsque la minuterie exécute, il est affiché:

Panel type is javax.swing.GroupLayout

Et bien sûr, le code de coulée dans drawPanel () échoue. Merci pour toutes les idées!

Était-ce utile?

La solution

Remplacer la méthode setLayout (), et imprimer les anciens / nouveaux noms de classe de mise en page il (avant ou après avoir appelé le super mise en œuvre). De cette façon, vous verrez quelle partie de votre code définit GroupLayout sur le panneau.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top