tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
2388
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    weidlix weidlix ist offline Grünschnabel
    Registriert seit
    May 2007
    Beiträge
    1
    Ich habe ein JPanel auf dem Labels liegen. Das JPanel möchte ich mit Hilfe eines Timers aktualisieren. Nach dem aktualisieren (mit DB-Abfrage) kann es sein das Labels die Auf dem JPanel liegen hinzukommen oder entfernt werden.
    Deswegen mache ich ein removeAll auf das JPanel. Danach rufe ich eine Funktion auf die mit das JPanel neu aufbaut (also die Labels wieder added). Das remove funktioniert auch wunderbar. Aber nach dem adden der Label und einem repaint bleibt das JPanel trotzdem lees. Ich bin am verzweifeln.

    Hier meine beiden Klassen:

    Erste Klasse (eigentliches JPanel - addThms ist wichtige funktion)

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    
    public class KommissionierDialogVpPanel extends JPanel{
        
        /** SWING-COMPONENTS */
        private JPanel jPnl_content;
        private Border border;
        private Color thmColor;
        private Font fontThmID;
        private PSBTableView psbTb_ThmInhalt;
        private Vector thmIDs;
        
     
        
        DialogVpPanel(Vector thmIDs, Color thmColor, Font thmFont, PSBTableView psbTb_ThmInhalt){
            super();
            border = BorderFactory.createLineBorder(Color.black);
            this.thmColor = thmColor;
            this.psbTb_ThmInhalt = psbTb_ThmInhalt;
            this.thmIDs = thmIDs;
            fontThmID = thmFont;
            jPnl_content = new JPanel();
            jPnl_content.setLayout(new GridBagLayout());
            this.setLayout(new BorderLayout());
            this.setBorder(BorderFactory.createTitledBorder("platz"));
            this.add(jPnl_content, BorderLayout.NORTH);
            addThms(thmIDs);
        }
        
     
        
        public void addThms(Vector thmIDs){
            jPnl_content.removeAll();
            for(int i=0; i<thmIDs.size(); i++){
                System.out.println("+++adding");
                JPanel comp = new JPanel(new GridLayout(1, 1), false);
                JLabel label = new JLabel("",JLabel.CENTER);
                label.addMouseListener(new LabelListener());
                label.setToolTipText(thmIDs.get(i).toString().trim());
                label.setOpaque(true);
                label.setBackground(thmColor);
                GUIUtilities.setComponentSize(label, new Dimension(50, 50));
                comp.add(label);
                comp.setBorder(border);
                JLabel jLbl_description = new JLabel(thmIDs.get(i).toString().trim());
                jLbl_description.addMouseListener(new LabelListener());
                GUIUtilities.setComponentSize(jLbl_description, new Dimension(300, 50));
                jLbl_description.setFont(fontThmID);
                jLbl_description.setVerticalAlignment(JLabel.CENTER);
                jLbl_description.setHorizontalAlignment(JLabel.LEFT);
     
                jPnl_content.add(comp, 
                        new GridBagConstraints(0, i, 1, 1, 0.0, 0.0, 
                             GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
                             new Insets(5, 5, 5, 5), 0, 0));
                jPnl_content.add(jLbl_description, 
                        new GridBagConstraints(1, i, 1, 1, 0.0, 0.0, 
                             GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, 
                             new Insets(5, 5, 5, 5), 0, 0));
            }
            jPnl_content.setVisible(true);
            jPnl_content.repaint();
            this.repaint();
        }

    Zweite Klasse (mit timer und refresh .. siehe ganz unten)

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    
        /**
         * INIT-Methode
         */
        public void init() {
            Font fontHeader = new Font("Dialog", Font.BOLD, 28);
            psbTb_ThmInhalt = new PSBTableView(this, "TAB_LHMI.XML");
     
            /** Label THM-ID */
            jLbl_ThmId = new JLabel();
            jLbl_ThmId.setFont(fontHeader);
            jLbl_ThmId.setText("nummer:");
            
            jTfld_ThmIdRead = new JTextField();
            GUIUtilities.setComponentSize(jTfld_ThmIdRead, new Dimension(170, 40));
            jTfld_ThmIdRead.setFont(fontHeader);
            jTfld_ThmIdRead.setEnabled(false);
            
            /** TextField THM-ID */
            jTfld_ThmId = new JTextField();
            GUIUtilities.setComponentSize(jTfld_ThmId, new Dimension(170, 40));
            jTfld_ThmId.setFont(fontHeader);
            jTfld_ThmId.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      setCursor(new Cursor(Cursor.WAIT_CURSOR));
                      jTfld_ThmIdRead.setText(jTfld_ThmId.getText());
                      jTfld_ThmId.setText("");
                      psbTb_ThmInhalt.setCondition("THM_ID" + " LIKE '" + jTfld_ThmIdRead.getText().trim() + "%'");
                      psbTb_ThmInhalt.refreshView();
                      setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                  }
              });
            // Focus Steuerung
            SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                      jTfld_ThmId.requestFocusInWindow();
                    }
            });
            
            /** Label für Auftrags-Typ */
            jLbl_auftagsTyp = new JLabel();
            jLbl_auftagsTyp.setText("Bildung von Mischtablaren");
            jLbl_auftagsTyp.setHorizontalAlignment(SwingConstants.CENTER);
            GUIUtilities.setComponentSize(jLbl_auftagsTyp, new Dimension(570, 40));
            jLbl_auftagsTyp.setFont(fontHeader);
            jLbl_auftagsTyp.setOpaque(true);
            jLbl_auftagsTyp.setBackground(Color.BLUE);
            
            Vector thmIds = daThm.getThmIDs(AREA, AISLE, X, Y, Z);
            jPnl_kommPlatz = new KommissionierDialogVpPanel(thmIds, Color.ORANGE,psbTb_ThmInhalt);
            
            /** Center-Panel */
            jPnl_Center = new JPanel();
            jPnl_Center.setLayout(new GridBagLayout());
            jPnl_Center.add(jLbl_ThmId, 
                    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
                         new Insets(5, 25, 5, 5), 0, 0));
            jPnl_Center.add(jTfld_ThmId, 
                    new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
                         new Insets(5, 5, 5, 5), 0, 0));
            jPnl_Center.add(jTfld_ThmIdRead, 
                    new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
                         new Insets(5, 5, 5, 5), 0, 0));
            jPnl_Center.add(jLbl_auftagsTyp, 
                    new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
                         new Insets(5, 45, 5, 5), 0, 0));
            jPnl_Center.add(jPnl_kommPlatz, 
                    new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, 
                         new Insets(5, 25, 5, 5), 0, 0));
            JLabel jLbl_icon = new JLabel();
            jLbl_icon.setIcon(new ImageIcon(this.getClass().getResource("./pfeilKomm.gif")));
            jLbl_icon.setVisible(true);
            jLbl_icon.setOpaque(true);
            jLbl_icon.setFont(fontHeader);
    //      jLbl_icon.setBackground(Color.black);
     
            jPnl_Center.add(jLbl_icon, 
                    new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, 
                         GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, 
                         new Insets(5, 25, 5, 5), 0, 0));
            jPnl_Center.add(psbTb_ThmInhalt, 
                    new GridBagConstraints(0, 2, 4, 1, 1.0, 1.0, 
                         GridBagConstraints.NORTH, GridBagConstraints.BOTH, 
                         new Insets(5, 25, 5, 5), 0, 0));
            
     
            ok_Button = new PSBButton("OK");
            GUIUtilities.setStandardButtonSize(ok_Button);
            
            /** Button-Panel */
            jPnl_Buttons = new JPanel();
            jPnl_Buttons.setLayout(new GridBagLayout());
            GUIUtilities.addButtonsToGridBag(jPnl_Buttons, ok_Button);
            
            
            this.getContentPane().add(jPnl_Center, BorderLayout.CENTER);
            this.getContentPane().add(jPnl_Buttons, BorderLayout.SOUTH);
        }
        
        public void initTimer(){
            int refreshRate = 3 * 1000;
            Timer refreshTimer = new Timer(refreshRate, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    KommissionierDialog.this.refresh();
                    System.out.println("+++ Refreshed");
                }
            });
            refreshTimer.start();
        }
        
        public void refresh() {
    //      Release unused memory
            Vector thmIds = daThm.getThmIDs(AREA, AISLE, X, Y, Z);
            jPnl_kommPlatz.addThms(thmIds);
            jPnl_kommPlatz.repaint();
            jPnl_Center.repaint();
            this.getContentPane().repaint();
    //          jPnl_Center.remove(jLbl_auftagsTyp);
    //      jLbl_auftagsTyp = new JLabel();
    //      jLbl_auftagsTyp.setText("Bildung von Mischtablaren");
    //      jLbl_auftagsTyp.setHorizontalAlignment(SwingConstants.CENTER);
    //      GUIUtilities.setComponentSize(jLbl_auftagsTyp, new Dimension(570, 40));
    //      jLbl_auftagsTyp.setFont(fontHeader);
    //      jLbl_auftagsTyp.setOpaque(true);
    //      jLbl_auftagsTyp.setBackground(Color.BLUE);
    //      jPnl_Center.add(jLbl_auftagsTyp, 
    //              new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 
    //                   GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
    //                   new Insets(5, 45, 5, 5), 0, 0));
    //      jPnl_Center.repaint();
        }
    }

    Wäre toll wenn mir jemand weiterhelfen könnte. Ich muss nämlich unbedingt mittels Refresh neue Labels hinzufügen können, weil die Anzahl variieren kann.

    Danke
    Geändert von weidlix (09.07.07 um 18:06 Uhr)
     

  2. #2
    Avatar von Vincentius
    Vincentius Vincentius ist offline Mitglied Brokat
    Registriert seit
    Sep 2004
    Ort
    Stuttgart
    Beiträge
    295
    Hallo,

    ich würde statt repaint() mit der validate()-Methode versuchen, denn genau für solche Fälle ist sie da: http://java.sun.com/j2se/1.5.0/docs/...html#validate()

    Grüße
    Vincent
     

  3. #3
    Wyatt Wyatt ist offline Mitglied Gold
    Registriert seit
    Apr 2007
    Beiträge
    128
    Heyho

    ich habe etwas ähnliches schonmal gemacht!
    Der Fehler war bei mir einfach nur, dass mein JPanel nicht aktualisiert wurde, was ich mit
    Code java:
    1
    2
    3
    
    panel.setVisible(false);
    // code
    panel.setVisible(true);
    gelöst habe. Nun läufts

    Vincent seine Lösung ist naürlich eleganter!

    Viel Glück!
     

Ähnliche Themen

  1. Label aus anderer Klasse aktualisieren.
    Von danielmueller1985 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 7
    Letzter Beitrag: 30.04.09, 09:00
  2. JPanel aktualisieren
    Von 3phasenpeterle im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 5
    Letzter Beitrag: 02.01.09, 07:17
  3. label.text aktualisieren
    Von zecken81 im Forum .NET Windows Forms
    Antworten: 3
    Letzter Beitrag: 27.06.08, 17:48
  4. label aktualisieren - nach datei
    Von GRU_EGO im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 1
    Letzter Beitrag: 11.01.07, 19:04
  5. In For Schleife Label aktualisieren
    Von Horusab im Forum Java
    Antworten: 4
    Letzter Beitrag: 17.06.05, 16:40