JPanel mit Label aktualisieren

weidlix

Grünschnabel
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)

Java:
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)

Java:
	/**
	 * 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
 
Zuletzt bearbeitet:
Heyho

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

Vincent seine Lösung ist naürlich eleganter!

Viel Glück!
 

Neue Beiträge

Zurück