Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218

    Problema aggiungendo Jpanel ad altro Jpanel

    Ho un jpanel con layout settato a null.
    Su questo dovrei andare poi a mettere un pannello diverso a seconda del pulsante che viene premuto.

    Questo è l'action performed di un pulsante:
    codice:
    	
    getPnl_mainPanel().removeAll();
    aca.passaADatiCondomini();
    JPanel pn = new JPanel();
    pn.setBackground(Color.BLUE);
    pn.add(new JButton("Click"));
    pn.setPreferredSize(new Dimension(200,300));
    pn.setVisible(true);
    System.out.println("Faccio il repaint");
    getPnl_mainPanel().validate();
    getPnl_mainPanel().repaint();
    Però non aggiunge niente.
    La cosa strana è che facendo la stessa cosa con un altro pulsante riesco ad aggiungerci una tabbedpane.

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Problema aggiungendo Jpanel ad altro Jpanel

    Originariamente inviato da TheBestNeo
    codice:
    pn.setPreferredSize(new Dimension(200,300));
    Però non aggiunge niente.
    setPreferredSize è ad uso dei (non tutti) layout manager. Se non usi un layout manager, il preferred size non viene sicuramente usato.
    Devi posizionare/dimensionare il componente che aggiungi (e nota: non lo vedo un getPnl_mainPanel.add(pn) ) con setBounds o la coppia setLocation/setSize.

    Se poi hai un tot di pannelli che sono sempre solo quelli ... fissi, potresti anche crearli tutti all'inizio e passare da uno all'altra sfruttando un CardLayout.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Bè il codice che ti ho postato era un esempio, in realtà aggiungo una gui creata in un altra classe. La cosa strana è che pur non vedendo i componenti, ho messo lo sfondo rosso, e questo appare.
    Quello che non appare sono i componenti.

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    UP

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Risolto, problema non inerente alle GUI.
    Thanks

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Mi ritrovo con lo stesso problema, ma con un codice diverso.

    Ho una dialog che funge da wizard, e alla pressione del pulsante avanti cambia jpanel.
    Questo il codice dell'actionListener:
    codice:
    public void actionPerformed(java.awt.event.ActionEvent e) {
    System.out.println("Stato: " + stato);
    if(stato == AvantiStato.InserimentoDati)
    {
    stato = AvantiStato.InserimentoUI;
    getPnl_dati().removeAll();
    pnl_inserisciUI = new Pnl_InserisciUI();
    getPnl_dati().add(pnl_inserisciUI);
    getPnl_dati().repaint();
    												System.out.println("Inserimento Dati");
    }
    else if(stato == AvantiStato.InserimentoUI)
    {
    stato = AvantiStato.InserimentoTabMill;
    getPnl_dati().removeAll();
    System.out.println("Inserimento UI");
    pnl_inserisciTabMill = new Pnl_InserisciTabMill();
    getPnl_dati().add(pnl_inserisciTabMill);
    pnl_inserisciTabMill.repaint();
    }
    else{
    getBtn_avanti().setText("Fine");
    System.out.println("Inserimento Tabella");
    }
    }
    });
    il pnl_dati è con un nullLayout, altrimenti mi creano problemi le dimensioni.

    Il fatto è che il primo pannello lo mette correttamente, ma quando crea quelli nuovi stranamente non disegna i componenti che vi sono dentro.

    Ad esempio Pnl_InserisciUI() è:
    codice:
     package boundary.gui;
    
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import java.awt.GridBagConstraints;
    import javax.swing.JList;
    import javax.swing.JButton;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.border.TitledBorder;
    import java.awt.Font;
    import java.awt.Color;
    
    public class Pnl_InserisciUI extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	private JScrollPane scrpnl_listaUI = null;
    	private JList lst_ui = null;
    	private JButton btn_aggiungiUI = null;
    
    	/**
    	 * This is the default constructor
    	 */
    	public Pnl_InserisciUI() {
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
    		gridBagConstraints2.gridx = 1;
    		gridBagConstraints2.insets = new Insets(0, 0, 0, 150);
    		gridBagConstraints2.anchor = GridBagConstraints.CENTER;
    		gridBagConstraints2.gridy = 0;
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.fill = GridBagConstraints.VERTICAL;
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.weightx = 1.0;
    		gridBagConstraints.weighty = 1.0;
    		gridBagConstraints.insets = new Insets(10, 0, 10, 0);
    		gridBagConstraints.gridx = 0;
    		this.setSize(480, 200);
    		this.setLayout(new GridBagLayout());
    		this.setBorder(BorderFactory.createTitledBorder(null, "Dati Unità Immobiliari", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), Color.black));
    		this.add(getScrpnl_listaUI(), gridBagConstraints);
    		this.add(getBtn_aggiungiUI(), gridBagConstraints2);
    		this.setVisible(true);
    	}
    
    	/**
    	 * This method initializes scrpnl_listaUI	
    	 * 	
    	 * @return javax.swing.JScrollPane	
    	 */
    	private JScrollPane getScrpnl_listaUI() {
    		if (scrpnl_listaUI == null) {
    			scrpnl_listaUI = new JScrollPane();
    			scrpnl_listaUI.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    			scrpnl_listaUI.setViewportView(getLst_ui());
    			scrpnl_listaUI.setPreferredSize(new Dimension(200, 10));
    		}
    		return scrpnl_listaUI;
    	}
    
    	/**
    	 * This method initializes lst_ui	
    	 * 	
    	 * @return javax.swing.JList	
    	 */
    	private JList getLst_ui() {
    		if (lst_ui == null) {
    			lst_ui = new JList();
    		}
    		return lst_ui;
    	}
    
    	/**
    	 * This method initializes btn_aggiungiUI	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getBtn_aggiungiUI() {
    		if (btn_aggiungiUI == null) {
    			btn_aggiungiUI = new JButton();
    			btn_aggiungiUI.setText("Inserisci UI");
    		}
    		return btn_aggiungiUI;
    	}
    
    }

  7. #7
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da TheBestNeo
    Il fatto è che il primo pannello lo mette correttamente, ma quando crea quelli nuovi stranamente non disegna i componenti che vi sono dentro.
    Se non si usa un layout manager, un componente aggiunto va esplicitamente posizionato/dimensionato con setBounds o con la coppia setLocation/setSize.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Ho risolto con un CardLayout, che mi sembra la scelta più giusta.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.