GUI - Jpanel - Splitpane will nicht aktualisieren

LMB

Mitglied
Hallo,
ich habe folgendes Problem. Ich möchte in ein Panel eine Splitting machen in zwei Teile. Im ersten Teil
links sollte Buttons hinzugefügt werden mit den Namen von Verzeichnisse die aus ein bestimmtes Verzeichnis geladen werden. Also ich habe ein Verzeichnis Produktordner in dieses Verzeichnis sind mehrere Verzeichnisse mit einem Nummer als Verzeichnisname. In diesen Ordner liegen PDF Dokumenten.

Im Schritt 1 sollte das Hauptverzeichnis Produktordner ausgelesen werden und die Name als Buttons gezeigt werden. Dies klappt auch mit der unterstehenden Code. In Schritt 2 sollte dann nach dem ich auf einem Button im linken Teil (also Produkt) geklickt habe dann rechts die Dokumenten erscheinen, auch wieder als Button. Jetzt habe ich das Problem das beim Start klappt alles wunderbar, allerdings wenn ich dann auf ein Button links klicke, das rechter Teil nicht aktualisiert wird.

Was muss ich ändern damit das rechte Teil aktualisiert wird?



Java:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package infopad;

/**
 *
 * @author Leonie
 */
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
 
public class MainPanel extends JPanel implements ActionListener {
  private JButton button1, button2, button3;
  private JPanel docPanel, cardPanel;
  private String productnumber;
 
  public MainPanel() {
    super(new BorderLayout());
 
    add(createTitlePanel(), BorderLayout.NORTH);
    add(createButtonPanel(), BorderLayout.WEST);
    add(createCardPanel(), BorderLayout.CENTER);
  }
 
  //Erzeugt Überschrift
  private JPanel createTitlePanel() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.setBorder(BorderFactory.createEtchedBorder());
    panel.setBackground(Color.WHITE);
 
    JLabel title = new JLabel("Infopad");
    title.setFont(title.getFont().deriveFont(16.0f));
 
    panel.add(title);
    return panel;
  }
 
  //Erzeugt Menü-Buttons (links)
  private JPanel createButtonPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createTitledBorder("Auswahl")));
    GridBagConstraints gbc = new GridBagConstraints();
 
    File directory = null;

        //Öffne Hauptverzeichnis
        directory = new File("/Test/Produktordner");

        int numberfiles = 0;
        String buttonname ="";

        // Inhalt von directory
        File[] files = directory.listFiles();

      //Number of Products in directory
        numberfiles = files.length;
        File file = files[1];
        productnumber = file.getName();

        for (int i = 1; i < numberfiles; i++) {
            //Name für Button aus Verzeichnisliste holen
            file = files[i];
            buttonname = file.getName();
            
            
            button1 = new JButton(buttonname);
            button1.setActionCommand(buttonname);
            button1.addActionListener(this);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(5, 5, 0, 5);
    
            panel.add(button1, gbc);
            
            gbc.gridx = 0; 
            gbc.gridy = i;
            
        }
    
  
    return panel;
  }
 
  //Erzeugt die Panels mit variablem Inhalten
  private JPanel createCardPanel() {
    JPanel cardPanel = new JPanel(new GridBagLayout());
    cardPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createTitledBorder("Auswahl")));
    GridBagConstraints gbc = new GridBagConstraints();

    String map = ("/Test/Produktordner/")+productnumber;
        
    
        File directory = null;

        //Öffne Hauptverzeichnis
        directory = new File(map);
 
     int numberfiles = 0;

        // Inhalt von directory
        File[] files = directory.listFiles();

      //Number of Products in directory
        numberfiles = files.length;

        for (int i = 1; i < numberfiles; i++) {
            //Name für Button aus Verzeichnisliste holen
             File file = files[i];

            button1 = new JButton(file.getName());
            button1.setActionCommand(file.getName());
            button1.addActionListener(this);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(5, 5, 0, 5);
    
            cardPanel.add(button1, gbc);
            
            gbc.gridx = 0; 
            gbc.gridy = i;
            
        }
    
    
    return cardPanel;
  }
 
  //Ereignisbehandlung für Menü-Buttons
  public void actionPerformed(ActionEvent e) {
    CardLayout cards = (CardLayout)cardPanel.getLayout();
    productnumber = e.getActionCommand();
   
    cards.show(cardPanel, "cardPanel");
    
  }
}
 
Hallo,

auf anhieb würde ich sagen das da nirgends ein Panel mit einem CardLayout existiert, demnach auch nichts aktuallisiert werden kann.
Ausserdem hast du da lokale Variablen und Klassenvariablen mit gleichem Namen (cardPanel);
Das cardPanel muss das Layout für das CardLayout bekommen sonst funktioniert das
Java:
CardLayout cards = (CardLayout)cardPanel.getLayout();
nicht.

Da du dem oberen Kommentar nach, anscheinend mit NetBeans arbeitest, solltest du bei den Variablen auf die gelben Ausrufezeichen am Rand achten. Wenn da welche sind, stimmt oft was nicht 😉

Gruß
SC
 
Hallo, da hattest Du recht, das Cardlayout hatte ich vorher gehabt, dann aber in ein GridBagLayout geändert und vergessen es überall zu ändern.
Also geändert habe ich es in:
GridBagLayout cards = (GridBagLayout)cardPanel.getLayout();
Variabele habe ich auch geändert, leider krieg ich es trotzdem nicht hin. Könntest Du mir bitte noch mal helfen? Danke.

Code ist jetzt
Java:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package infopad;

/**
 *
 * @author Leonie
 */
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
 
public class MainPanel extends JPanel implements ActionListener {
  private JButton button1;
  private String productnumber;
  private JPanel CardPanel;
 
  public MainPanel() {
    super(new BorderLayout());
 
    add(createTitlePanel(), BorderLayout.NORTH);
    add(createButtonPanel(), BorderLayout.WEST);
    add(createCardPanel(), BorderLayout.CENTER);
  }
 
  //Erzeugt Überschrift
  private JPanel createTitlePanel() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.setBorder(BorderFactory.createEtchedBorder());
    panel.setBackground(Color.WHITE);
 
    JLabel title = new JLabel("Infopad");
    title.setFont(title.getFont().deriveFont(16.0f));
 
    panel.add(title);
    return panel;
  }
 
  //Erzeugt Menü-Buttons (links)
  private JPanel createButtonPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createTitledBorder("Auswahl")));
    GridBagConstraints gbc = new GridBagConstraints();
 
    File directory = null;

        //Öffne Hauptverzeichnis
        directory = new File("/Users/Leonie/Dropbox/Test/Produktordner");

        int numberfiles = 0;
        String buttonname ="";

        // Inhalt von directory
        File[] files = directory.listFiles();

      //Number of Products in directory
        numberfiles = files.length;
        File file = files[1];
        productnumber = file.getName();

        for (int i = 1; i < numberfiles; i++) {
            //Name für Button aus Verzeichnisliste holen
            file = files[i];
            buttonname = file.getName();
            
            
            button1 = new JButton(buttonname);
            button1.setActionCommand(buttonname);
            button1.addActionListener(this);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(5, 5, 0, 5);
    
            panel.add(button1, gbc);
            
            gbc.gridx = 0; 
            gbc.gridy = i;
            
        }
    
  
    return panel;
  }
 
  //Erzeugt die Panels mit variablem Inhalten
  private JPanel createCardPanel() {
    JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createTitledBorder("Auswahl")));
    GridBagConstraints gbc = new GridBagConstraints();

    String map = ("/Users/Leonie/Dropbox/Test/Produktordner/")+productnumber;
        
    
        File directory = null;

        //Öffne Hauptverzeichnis
        directory = new File(map);
 
     int numberfiles = 0;

        // Inhalt von directory
        File[] files = directory.listFiles();

      //Number of Products in directory
        numberfiles = files.length;

        for (int i = 1; i < numberfiles; i++) {
            //Name für Button aus Verzeichnisliste holen
             File file = files[i];

            button1 = new JButton(file.getName());
            button1.setActionCommand(file.getName());
            button1.addActionListener(this);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(5, 5, 0, 5);
    
            panel.add(button1, gbc);
            
            gbc.gridx = 0; 
            gbc.gridy = i;
            
        }
    
    
    return panel;
  }
 
  //Ereignisbehandlung für Menü-Buttons
  public void actionPerformed(ActionEvent e) {
    GridBagLayout cl = (GridBagLayout)CardPanel.getLayout();
    productnumber = e.getActionCommand();
   
    cl.show(CardPanel, productnumber);
   
  }
}
 
Wenn du die zentrale Funktionalität eines CardLayouts verwenden möchtest, nämlich einfach zwischen verschiedenen Components zu wechseln, musst du natürlich auch ein CardLayout verwenden!
In deinem ersten Codeschnipsel müsste eine ClassCastExceptopn geworfen werden, weil du versuchst, ein GridBagLayout zu einem CardLayout zu casten, der zweite Schnipsel dürfte gar nicht kompilieren, weil ein GridBagLayout keine show()-Methode hat.
Was möchest du erreichen? Die CardPanel-Funktionalität mit einem GridBagLayout nachbilden?:autsch:
 
Was ich machen möchte ist Dateien als Buttons auf ein Panel darstellen. Also in ein Verzeichnis Produkt1 sind fünf PDF Dateien. Das Verzeichnis Produkt1 wird im linken Panel als Button dargestellt (dies funktioniert auch). Klick ich jetzt auf diesem Button, möchte ich in Panel 2 das die Dateien aus dem Verzeichnis wieder als Button (oder später mal als thumblnail) dargestellt werden, deshalb habe ich die GridBagLayout gewählt ein CardLayout brauch ich ja eigentlich dafür nicht. Aber wahrscheinlich bin ich komplett auf dem falschen Spur und funktioniert das nicht mit dem GridBagLayout.
 

Zurück
Oben