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

    Aggiungere righe in JTable, problema refresh?

    Utilizzo il metodo addRow(ob), dove ob è
    codice:
    Object[] ob = {new String("ciao"), new String(), new String(), new Integer(0), new String(), new String(), new Integer(0)};
    Stampando il numero delle righe (che inizialmente era 1) viene 2, quindi la riga la aggiunge, ma non la visualizza nella tabella.
    Ho provato fireTableDataChanged() ma nulla.

    Uso il DefaultTableModel.

    Suggerimenti?

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

    Re: Aggiungere righe in JTable, problema refresh?

    Originariamente inviato da TheBestNeo
    Stampando il numero delle righe (che inizialmente era 1) viene 2, quindi la riga la aggiunge, ma non la visualizza nella tabella.
    Ho provato fireTableDataChanged() ma nulla.

    Uso il DefaultTableModel.
    Ok, ma bisognerebbe sapere qualcosa di più ... il addRow() già di per sé si occupa di notificare l'aggiunta della riga. Il problema presumo ... ipotizzo sia dovuto ad altro.
    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
    Cioè a cosa?
    Cosa dovrei far sapere???

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da TheBestNeo
    Cioè a cosa?
    Cosa dovrei far sapere???
    Come hai impostato il model sul JTable? In che contesto fai l'addRow()? Inizialmente hai dei dati nel DefaultTableModel?? (cioè qualcosa si vede ed è solo l'addRow che non va?)
    Guarda che la sfera di cristallo non ce l'ho ....
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    No ma non sapevo cosa ti serviva sapere!

    codice:
    Object[][] dati = {{new String(), new String(), new String(), new Integer(0), new String(), new String(), new Integer(0)}};
    
    Object[] columnNames = {
    "Identificatore", 
    "Categoria", 
    "Destinazione", 
    "Metratura (in mq)", 
    "Posizione interna", 
    "Proprietario", 
    "Quota", };
    			
    datiUIModel = new DefaultTableModel(dati, columnNames);
    tbl_datiUI = new JTable(datiUIModel);
    tbl_datiUI.setRowHeight(20);
    Qui l'action listener di un pulsante che dovrebbe aggiungere una riga (vuota):
    codice:
    btn_conferma.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    Object[] ob = {new String(), new String(), new String(), new Integer(0), new String(), new String(), new Integer(0)};
    datiUIModel.addRow(ob);
    
    				}

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Almeno il codice postato mi sembra tutto corretto. La tabella si vede a video, giusto? (e immagino, spero che l'abbia messa in un JScrollPane). E cliccando su quel pulsante non aggiunge nulla?? Strano .....

    Il tuo codice completo è lungo?? Se no, postalo.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    La prima riga si vede, le altre no premendo quel pulsante.

    E' un po' lungo ma lo posto.

    codice:
     package boundary.gui;
    
    import javax.swing.JPanel;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    
    import boundary.InserireUnitaImmobiliare;
    import boundary.gui.ModelTables.DatiUIModelTable;
    import boundary.gui.Renderers.ComboBoxRenderer;
    import javax.swing.JButton;
    import javax.swing.table.DefaultTableModel;
    
    import java.awt.Insets;
    import java.awt.Dimension;
    import java.awt.SystemColor;
    import java.awt.Color;
    import java.util.Vector;
    
    public class GUI_InserireDatiUI extends JDialog {
    
    	private static final long serialVersionUID = 1L;
    
    	private JPanel pnl_main = null;
    
    	private JScrollPane scrpnl_datiUI = null;
    
    	private JTable tbl_datiUI = null;
    
    	private JButton btn_insNuovoCond = null;
    
    	private JButton btn_conferma = null;
    
    	private JButton btn_annulla = null;
    
    	private InserireUnitaImmobiliare iui = null;
    	
    	
    	private DefaultTableModel datiUIModel;
    	/**
    	 * @param owner
    	 */
    	public GUI_InserireDatiUI(InserireUnitaImmobiliare iui) {
    		super();
    		this.iui = iui;
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		final String windowsTheme = new String("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    		try
    		{
    			UIManager.setLookAndFeel(windowsTheme);
    		}
    		catch(Exception e)
    		{
    			System.out.println("Error creating Look And Feel.");
    		}
    		this.setSize(733, 300);
    		this.setTitle("Inserimento Dati Unità Immobiliare");
    		this.setContentPane(getPnl_main());
    		this.setVisible(true);
    		this.addWindowListener(new java.awt.event.WindowAdapter() {
    			public void windowClosing(java.awt.event.WindowEvent e) {
    				setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    			}
    		});
    	}
    
    	/**
    	 * This method initializes pnl_main
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getPnl_main() {
    		if (pnl_main == null) {
    			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
    			gridBagConstraints3.gridx = 1;
    			gridBagConstraints3.anchor = GridBagConstraints.WEST;
    			gridBagConstraints3.insets = new Insets(0, 50, 10, 0);
    			gridBagConstraints3.gridy = 2;
    			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
    			gridBagConstraints2.gridx = 0;
    			gridBagConstraints2.insets = new Insets(0, 280, 10, 0);
    			gridBagConstraints2.gridy = 2;
    			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
    			gridBagConstraints1.gridx = 0;
    			gridBagConstraints1.gridwidth = 2;
    			gridBagConstraints1.weightx = 0.0;
    			gridBagConstraints1.anchor = GridBagConstraints.EAST;
    			gridBagConstraints1.insets = new Insets(0, 0, 30, 100);
    			gridBagConstraints1.gridy = 1;
    			GridBagConstraints gridBagConstraints = new GridBagConstraints();
    			gridBagConstraints.fill = GridBagConstraints.BOTH;
    			gridBagConstraints.gridy = 0;
    			gridBagConstraints.ipadx = 0;
    			gridBagConstraints.ipady = 0;
    			gridBagConstraints.weightx = 1.0;
    			gridBagConstraints.weighty = 1.0;
    			gridBagConstraints.gridwidth = 2;
    			gridBagConstraints.gridheight = 1;
    			gridBagConstraints.insets = new Insets(0, 0, 20, 0);
    			gridBagConstraints.gridx = 0;
    			pnl_main = new JPanel();
    			pnl_main.setLayout(new GridBagLayout());
    			pnl_main.add(getScrpnl_datiUI(), gridBagConstraints);
    			pnl_main.add(getBtn_insNuovoCond(), gridBagConstraints1);
    			pnl_main.add(getBtn_conferma(), gridBagConstraints2);
    			pnl_main.add(getBtn_annulla(), gridBagConstraints3);
    		}
    		return pnl_main;
    	}
    
    	/**
    	 * This method initializes scrpnl_datiUI	
    	 * 	
    	 * @return javax.swing.JScrollPane	
    	 */
    	private JScrollPane getScrpnl_datiUI() {
    		if (scrpnl_datiUI == null) {
    			scrpnl_datiUI = new JScrollPane();
    			scrpnl_datiUI.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    			scrpnl_datiUI.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    			scrpnl_datiUI.setViewportView(getTbl_datiUI());
    		}
    		return scrpnl_datiUI;
    	}
    
    	/**
    	 * This method initializes tbl_datiUI	
    	 * 	
    	 * @return javax.swing.JTable	
    	 */
    	private JTable getTbl_datiUI() {
    		if (tbl_datiUI == null) {
    			//DatiUIModelTable datiUIModel= new DatiUIModelTable();
    			
    			Object[][] dati = {{new String(), new String(), new String(), new Integer(0), new String(), new String(), new Integer(0)}};
    			Object[] columnNames = {
    				"Identificatore", 
    				"Categoria", 
    				"Destinazione", 
    				"Metratura (in mq)", 
    				"Posizione interna", 
    				"Proprietario", 
    				"Quota", };
    			
    			datiUIModel = new DefaultTableModel(dati, columnNames);
    			tbl_datiUI = new JTable(datiUIModel);
    			tbl_datiUI.setRowHeight(20);
    			tbl_datiUI.setRowSelectionAllowed(false);
    			tbl_datiUI.setPreferredSize(new Dimension(525, 20));
    			tbl_datiUI.setIntercellSpacing(new Dimension(1, 1));
    			tbl_datiUI.setBackground(SystemColor.WHITE);
    			tbl_datiUI.setForeground(Color.DARK_GRAY);
    			tbl_datiUI.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    			tbl_datiUI.setShowVerticalLines(true);
    			tbl_datiUI.setSize(new Dimension(710, 20));
    			tbl_datiUI.addFocusListener(new java.awt.event.FocusAdapter() {
    				public void focusLost(java.awt.event.FocusEvent e) {
    					/*if(checkId())
    					{
    					}*/
    				}
    			});
    			
    			String[] comboBoxItems = {"Genova", "Milano"}; //Array di prova
    			//String[] Cities;
    			//String[] Owners;
    			/*Devo implementare il recupero dei comuni e il recupero dei proprietari.*/
    
    			tbl_datiUI.getColumnModel().getColumn(6).setPreferredWidth(20);
    			tbl_datiUI.getColumnModel().getColumn(2).setCellRenderer(new ComboBoxRenderer(comboBoxItems));    
    			tbl_datiUI.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor( new JComboBox(comboBoxItems)));
    			tbl_datiUI.getColumnModel().getColumn(5).setCellRenderer(new ComboBoxRenderer(comboBoxItems));    
    			tbl_datiUI.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor( new JComboBox(comboBoxItems)));
    			
    			
    		}
    		return tbl_datiUI;
    	}
    
    	/**
    	 * This method initializes btn_insNuovoCond	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getBtn_insNuovoCond() {
    		if (btn_insNuovoCond == null) {
    			btn_insNuovoCond = new JButton();
    			btn_insNuovoCond.setText("Inserisci Nuovo Condomino");
    		}
    		return btn_insNuovoCond;
    	}
    
    	/**
    	 * This method initializes btn_conferma	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getBtn_conferma() {
    		if (btn_conferma == null) {
    			btn_conferma = new JButton();
    			btn_conferma.setText("Conferma");
    			btn_conferma.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					Object[] ob = {new String("ciao"), new String(), new String(), new Integer(0), new String(), new String(), new Integer(0)};
    					datiUIModel.addRow(ob);
    										
    					//datiUIModel.addRow(ob); 
    					//datiUIModel.fireTableDataChanged();
    					//fireTableDataChanged();
    				}
    			});
    		}
    		return btn_conferma;
    	}
    
    	/**
    	 * This method initializes btn_annulla	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getBtn_annulla() {
    		if (btn_annulla == null) {
    			btn_annulla = new JButton();
    			btn_annulla.setText("Annulla");
    			btn_annulla.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					iui.annulla();
    					setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    				}
    			});
    		}
    		return btn_annulla;
    	}
    
    }  //  @jve:decl-index=0:visual-constraint="10,10"

  8. #8
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Il problema è qui:

    tbl_datiUI.setPreferredSize(new Dimension(525, 20));

    Se imponi una altezza preferita di 20 sfido io che non vedi più righe!!


    P.S.: si vede chiaramente che parte del codice è autogenerato da un IDE .... sulla qualità e pulizia di questo codice meglio non fare commenti .....
    P.S.2: come vedi postare il codice è servito .... se non lo avessi postato, come poteva uno anche solo pensare che avevi settato esplicitamente un preferred size insufficiente???
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  9. #9
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Si ho usato Eclipse con Visual Editor...
    Provo ad aggiustare e ti faccio sapere!!!
    Thanks

  10. #10
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    218
    Era esattamente quello.
    Grazie mille!

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.