JPanel neu laden erst nach zweitem Drücken des Buttons

ascawath

Mitglied
Hallo alle zusammen,

ich habe versucht eine GUI zu erstellen. Wenn ich den Button NewButton Test drücke, sollten eigentlich nur noch 5 statt 45 Einträge gezeigt werden. Wenn man den Button allerdings drückt löscht der erst einmal alles und man sieht gar nichts. Drückt man den Button noch einmal, dann sieht man die 5 Einträge.
Das funktioniert aber auch nur so lange, wie ich validate() bzw. revalidate() benutze.
Sobald ich repaint() benutze geht gar nichts mehr...

Hier der Code:
Java:
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.WindowConstants;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.border.LineBorder;
import javax.swing.JCheckBox;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

public class main extends MouseAdapter {

    private JFrame frame;

    /**
    * Launch the application.
    */

    public RowSpec[] rowspec = new RowSpec[9 * 4 + 7];
    public ColumnSpec[] clspec = new ColumnSpec[35];

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    main window = new main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
    * Create the application.
    */
    public main() throws Exception {
        initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() throws Exception {
        Toolkit tk = Toolkit.getDefaultToolkit();

        frame = new JFrame();
        frame.setTitle("Kirchenfenster"); // Fenstertitel setzen
        frame.setSize((int) tk.getScreenSize().getWidth(), (int) tk.getScreenSize().getWidth()); // Fenstergröße
                                    // einstellen
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JSplitPane splitPane = new JSplitPane();
        splitPane.setEnabled(false);
        frame.getContentPane().add(splitPane, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        splitPane.setRightComponent(panel);
        panel.setLayout(new BorderLayout(0, 0));
      
        /**
        * Panel für die Liste
        */
      
        JPanel panel_3 = new JPanel();
      
        /**
        * MenüLeiste Oben
        */
      
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Datei");
        menuBar.add(mnNewMenu);

        JMenu mnNewMenu_1 = new JMenu("Einf\u00FCgen");
        menuBar.add(mnNewMenu_1);

        JMenu mnNewMenu_2 = new JMenu("Optionen");
        menuBar.add(mnNewMenu_2);
      
        listWindow(panel, panel_3);

        /**
        * OptionenLeiste
        */
      
        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(Color.WHITE));
        panel_1.setForeground(Color.WHITE);
        panel.add(panel_1, BorderLayout.NORTH);

        JButton button = new JButton("New button");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(button);

        JButton btnNewButton_5 = new JButton("New button Test");
        btnNewButton_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ChangePanel(panel, panel_3);
            }
        });
        panel_1.add(btnNewButton_5);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_2);

        JButton btnNewButton_4 = new JButton("New button");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_4);

        JButton btnNewButton_3 = new JButton("New button");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_3);

        /*
        * JLabel lblNewLabel_13 = new JLabel("1");
        * lblNewLabel_13.addMouseListener(new MouseAdapter(){public void
        * mouseClicked(MouseEvent e){ System.out.println("1 clicked"); sql dao
        * = new sql(); try { dao.readDataBase(); } catch (Exception e1) { //
        * TODO Auto-generated catch block e1.printStackTrace(); }}} );
        */

      
        /**
        *Schnellzugriffszeile
        */
      
        JPanel panel_2 = new JPanel();
        splitPane.setLeftComponent(panel_2);
        panel_2.setLayout(new FormLayout(
                new ColumnSpec[] { FormSpecs.LABEL_COMPONENT_GAP_COLSPEC, ColumnSpec.decode("150px"), },
                new RowSpec[] { FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("50px"), FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, }));

        JLabel lblSchnellzugriff = new JLabel("Schnellzugriff");
        panel_2.add(lblSchnellzugriff, "2, 4, center, center");

        JButton btnNewButton_8 = new JButton("New button 1");
        btnNewButton_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_8, "2, 6");

        JButton btnNewButton_6 = new JButton("New button 2");
        btnNewButton_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_6, "2, 8");

        JButton btnNewButton_10 = new JButton("New button 3");
        btnNewButton_10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_10, "2, 10");

        JButton btnNewButton_11 = new JButton("New button 4");
        btnNewButton_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_11, "2, 12");

        JButton btnNewButton_9 = new JButton("New button 5");
        btnNewButton_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_9, "2, 14");

        JButton btnNewButton_7 = new JButton("New button 6");
        btnNewButton_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        panel_2.add(btnNewButton_7, "2, 16");


    }

    /**
    * Methode zum Darstellen der Ergebnisse
    */
  
    public int anzahl = 45;
  
    public void listWindow(JPanel p_Haupt, JPanel p_Liste) {
      
      
        JScrollPane sP = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        sP.getVerticalScrollBar().setUnitIncrement(16);
        sP.setViewportView(p_Liste);

        int Spalten = 7;
        JLabel[][] LabelArray = new JLabel[anzahl][7];
        JLabel[] SpaltenLabel = new JLabel[Spalten];
        JCheckBox[] CheckBoxArray = new JCheckBox[anzahl];

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        p_Liste.setLayout(layout);
        p_Haupt.add(sP, BorderLayout.CENTER);

        c.weighty = .9;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.ipady = 20;
        c.ipadx = 100;

        for (int i = 0; i < Spalten; i++) {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = i + 1;
            c.gridy = 0;
            SpaltenLabel[i] = new JLabel("Spalte "+i);
            p_Liste.add(SpaltenLabel[i], c);
        }

        int i = 0;
        try {

            while (i < anzahl) {
              
                CheckBoxArray[i] = new JCheckBox("");
                //System.out.println(i * 2 + 4);
                //System.out.println(i + " i");

                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy = i + 3;
              
                p_Liste.add(CheckBoxArray[i], c);
                for (int e = 0; e < 7; e++) {
                    //System.out.println(e);
                    LabelArray[i][e] = new JLabel("" + i);
                    final int k = i;
                    LabelArray[i][e].addMouseListener(new MouseAdapter() {
                        int z = k;

                        public void mouseClicked(MouseEvent e) {
                            System.out.println(z + " clicked");
                        }
                    });
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = e + 1;
                    c.gridy = i + 3;
                    p_Liste.add(LabelArray[i][e], c);
                }
                p_Haupt.revalidate();
                p_Haupt.setVisible(true);
                p_Liste.revalidate();
                p_Liste.setVisible(true);
                //p_Liste.repaint();

                //p_Haupt.repaint();
                i++;
            }


        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
  
    public void ChangePanel(JPanel p_Haupt, JPanel p_Liste) {
        System.out.println("Test");
        p_Liste.removeAll();
        anzahl = 5;
        listWindow(p_Haupt, p_Liste);


    }

}

Vielen Dank :)
 
Zuletzt bearbeitet:
Hallo,
nach einigem Herumprobieren habe ich dann des Rätsels Lösung gefunden...
In der Methode listWindow habe ich das JScrollPane immer wieder neu erstellt und neu zugewiesen. Ist ja klar, dass das nicht funktioniert.
Also habe ich nun die Definition in eine andere Methode geschrieben, die nur Ein Mal aufgerufen wird.
Hier der neue Code:
Java:
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JViewport;
import javax.swing.WindowConstants;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;

public class main extends MouseAdapter {

    private JFrame frame;
    private JPanel panel;
    private JPanel panel_3;
    private JScrollPane sP;
    public int anzahl = 45; // Anzahl der Listeneinträge

    /**
    * Launch the application.
    */

    public RowSpec[] rowspec = new RowSpec[9 * 4 + 7];
    public ColumnSpec[] clspec = new ColumnSpec[35];

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    main window = new main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
    * Create the application.
    */

    public main() throws Exception {
        initialize();
    }

    /**
    * Initialize the contents of the frame.
    */

    private void initialize() throws Exception {
        Toolkit tk = Toolkit.getDefaultToolkit();

        frame = new JFrame();
        frame.setTitle("Kirchenfenster"); // Fenstertitel setzen
        frame.setSize((int) tk.getScreenSize().getWidth(), (int) tk.getScreenSize().getWidth()); // Fenstergröße
        // einstellen
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JSplitPane splitPane = new JSplitPane();
        splitPane.setEnabled(false);
        frame.getContentPane().add(splitPane, BorderLayout.CENTER);

        panel = new JPanel();

        panel.setLayout(new BorderLayout(0, 0));

        /**
        * Panel Und JScrollPane für die Liste
        */

        panel_3 = new JPanel();
        sP = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        sP.getVerticalScrollBar().setUnitIncrement(16);
        sP.setViewportView(panel_3);
        sP.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);

        splitPane.setRightComponent(panel);

        /**
        * MenüLeiste Oben
        */

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("Datei");
        menuBar.add(mnNewMenu);

        JMenu mnNewMenu_1 = new JMenu("Einf\u00FCgen");
        menuBar.add(mnNewMenu_1);

        JMenu mnNewMenu_2 = new JMenu("Optionen");
        menuBar.add(mnNewMenu_2);

        listWindow(sP);

        /**
        * OptionenLeiste (Diverse Buttons)
        */

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(Color.WHITE));
        panel_1.setForeground(Color.WHITE);
        panel.add(panel_1, BorderLayout.NORTH);

        JButton button = new JButton("New button Repaint");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.validate();
            }
        });
        panel_1.add(button);

        JButton btnNewButton_5 = new JButton("New button Test");
        btnNewButton_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ChangePanel(sP);
            }
        });
        panel_1.add(btnNewButton_5);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_2);

        JButton btnNewButton_4 = new JButton("New button");
        btnNewButton_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_4);

        JButton btnNewButton_3 = new JButton("New button");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_1.add(btnNewButton_3);

        /*
        * JLabel lblNewLabel_13 = new JLabel("1");
        * lblNewLabel_13.addMouseListener(new MouseAdapter(){public void
        * mouseClicked(MouseEvent e){ System.out.println("1 clicked"); sql dao
        * = new sql(); try { dao.readDataBase(); } catch (Exception e1) { //
        * TODO Auto-generated catch block e1.printStackTrace(); }}} );
        */

        /**
        * Schnellzugriffszeile (Buttons)
        */

        JPanel panel_2 = new JPanel();
        splitPane.setLeftComponent(panel_2);
        panel_2.setLayout(new FormLayout(
                new ColumnSpec[] { FormSpecs.LABEL_COMPONENT_GAP_COLSPEC, ColumnSpec.decode("150px"), },
                new RowSpec[] { FormSpecs.LINE_GAP_ROWSPEC, RowSpec.decode("50px"), FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC,
                        FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC,
                        FormSpecs.DEFAULT_ROWSPEC, FormSpecs.RELATED_GAP_ROWSPEC, FormSpecs.DEFAULT_ROWSPEC, }));

        JLabel lblSchnellzugriff = new JLabel("Schnellzugriff");
        panel_2.add(lblSchnellzugriff, "2, 4, center, center");

        JButton btnNewButton_8 = new JButton("New button 1");
        btnNewButton_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_8, "2, 6");

        JButton btnNewButton_6 = new JButton("New button 2");
        btnNewButton_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_6, "2, 8");

        JButton btnNewButton_10 = new JButton("New button 3");
        btnNewButton_10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_10, "2, 10");

        JButton btnNewButton_11 = new JButton("New button 4");
        btnNewButton_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_11, "2, 12");

        JButton btnNewButton_9 = new JButton("New button 5");
        btnNewButton_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        panel_2.add(btnNewButton_9, "2, 14");

        JButton btnNewButton_7 = new JButton("New button 6");
        btnNewButton_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        panel_2.add(btnNewButton_7, "2, 16");

    }

    /**
    * Methode um die Listeninhalte anzusehen
    * @param Pane
    */

    public void listWindow(JScrollPane Pane) {
        panel.remove(Pane);
        panel.add(Pane);
        JPanel lokal = (JPanel) (Pane.getViewport().getView());
        lokal.removeAll();
        // panel_3.removeAll();

        int Spalten = 7;
        JLabel[][] LabelArray = new JLabel[anzahl][7];
        JLabel[] SpaltenLabel = new JLabel[Spalten];
        JCheckBox[] CheckBoxArray = new JCheckBox[anzahl];

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        lokal.setLayout(layout);

        c.weighty = .9;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.ipady = 20;
        c.ipadx = 100;

        for (int i = 0; i < Spalten; i++) {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = i + 1;
            c.gridy = 0;
            SpaltenLabel[i] = new JLabel("Spalte " + i);
            lokal.add(SpaltenLabel[i], c);
        }

        int i = 0;
        try {

            while (i < anzahl) {

                CheckBoxArray[i] = new JCheckBox("");
                // System.out.println(i * 2 + 4);
                // System.out.println(i + " i");

                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy = i + 3;

                lokal.add(CheckBoxArray[i], c);
                for (int e = 0; e < 7; e++) {
                    // System.out.println(e);
                    LabelArray[i][e] = new JLabel("" + i);
                    final int k = i;
                    LabelArray[i][e].addMouseListener(new MouseAdapter() {
                        int z = k;

                        public void mouseClicked(MouseEvent e) {
                            System.out.println(z + " clicked");
                        }
                    });
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = e + 1;
                    c.gridy = i + 3;
                    lokal.add(LabelArray[i][e], c);
                }

                i++;
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    /**
    * Methode zum Aktualisieren
    * 
    * @param Pane
    */

    public void ChangePanel(JScrollPane Pane) {
        System.out.println("Test");

        anzahl = 5;
        listWindow(Pane);
        frame.getContentPane().revalidate();
        frame.getContentPane().repaint();

    }

}
 
Zurück