Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 20 su 20
  1. #11
    Ciao progAnd..... posso chiederti un aiuto!?
    Sempre riguardo alla JTable.
    Dopo aver letto i miei dati un un DB e messi in una JTable

  2. #12
    Ciao progAnd..... posso chiederti un aiuto!?
    Sempre riguardo alla JTable.
    Dopo aver letto i dati in un DB e messi in una JTable, da un JDialog l'utente puo inserirne altri, l'inserimento funziona bene per quanto riguarda il DB, ma non riesco ad aggiornare in tempo reale la JTable. Ho provato con questo metodo, ma mi da errore sull'ultima riga di codice.
    codice:
    public void addDati(String[] dati) {
    		
      model.addRow(dati);
      for(int x=0; x<model.getColumnCount(); x++) {
    	table.getColumnModel().getColumn(x).setCellRenderer(new RegModelTable(checkRed));// SETUP TABELLA
      }
      model.fireTableDataChanged();
    		
      table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true)); // POSIZIONE VERTICALE DELLA SCROLLPANE
    }
    ...dove dati è l'array dei nuovi dati inseriti nel DB e "anticipati" nella JTable, in quanto al riavvio dell'applicazione vengono letti dal DB.

    Grazie per l'attenzione.

  3. #13
    Utente di HTML.it L'avatar di progAnd
    Registrato dal
    Jan 2012
    Messaggi
    119
    Che errore ti dà sull'ultima riga?
    Se semplicemente sembra non avvenire nulla, la risposta la trovi nel link che ti avevo dato sopra

    Ciao

  4. #14
    Grazie per l'attenzione progAnd.
    L'errore è qiesto:
    codice:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at grafica.PageRegistro.addDati(PageRegistro.java:250)
    	at grafica.NewRegPage.actionPerformed(NewRegPage.java:374)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.WaitDispatchSupport$2.run(Unknown Source)
    	at java.awt.WaitDispatchSupport$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.awt.WaitDispatchSupport.enter(Unknown Source)
    	at java.awt.Dialog.show(Unknown Source)
    	at java.awt.Component.show(Unknown Source)
    	at java.awt.Component.setVisible(Unknown Source)
    	at java.awt.Window.setVisible(Unknown Source)
    	at java.awt.Dialog.setVisible(Unknown Source)
    	at grafica.NewRegPage.<init>(NewRegPage.java:229)
    	at grafica.PageRegistro$ActionBtn.actionPerformed(PageRegistro.java:258)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Ho aggiunto questa parte di codcie come nel link da TE postatomi, ma non va, la JTable non si refresha!!
    codice:
    table.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
            table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true));
        }
    });

  5. #15
    Il problema può essere questo:
    codice:
    public void addDati(Object[] dati) {
    		
         System.out.println("METODO CHIAMATO!!! ");
         for(int i=0; i<dati.length; i++) {
              System.out.println("DATI PASSATI: "+ dati[i]);
         }
    		
         System.out.println("row Count: "+model.getRowCount());
    		
         model.addRow(dati); 
    		
         System.out.println("row Count: "+model.getRowCount());
    		
         for(int x=0; x<model.getColumnCount(); x++) {
              table.getColumnModel().getColumn(x).setCellRenderer(new RegModelTable(checkRed));// SETUP TABELLA
         }
         model.fireTableDataChanged();
    		
         table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true)); // POSIZIONE VERTICALE DELLA SCROLLPANE
    }
    ..... nelle righe evidenziate in rosso, prima dell'inserimento dei dati nella riga, il rowCount è ZERO, dopo diventa UNO!! Ma il rowCount non dovrebbe essere pari al totale numero di righe dati della tabella!!?

  6. #16
    Utente di HTML.it L'avatar di progAnd
    Registrato dal
    Jan 2012
    Messaggi
    119
    PageRegistro.java:250
    Questo è il punto dove vi è l'errore. Che c'è alla riga 250?

    Ma il rowCount non dovrebbe essere pari al totale numero di righe dati della tabella!!?
    Dipende da cos'è model.

    Ciao

  7. #17
    L'errore me lo da in questa riga:
    codice:
    table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true));
    model è il DefaultTableModel(); !!!

    Stavo provando quest'altra strada.... costruire una classe che mi restituisca la JTable, così ad ogni inserimento di nuovi dati ridisegno la JTable.
    La JTable la ottengo però ho due problemi:
    1) non mi si posiziona più all'ultima riga della JTable!!
    2) non riesco ad avere più le info della riga su cui clicco con il mouse!!

    Codice della classe generatrice della JTable:
    codice:
    package pannelli;
    
    import grafica.RegModelTable;
    import java.awt.Dimension;
    
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    
    import database.RegistroCassa;
    
    public class RegistroTable extends JTable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	final DefaultTableModel model = new DefaultTableModel();
    	private JTable table;	
    	
    	public RegistroTable(RegistroCassa registro) {
    		
    	Object[] columnHeaders = {"NUM","OPERAZIONE","IMPORTO","DATA","MOTIVO","CATEGORIA","SALDO","BOLLETTA"};
    	String[] checkRed = registro.getRedCheck();
    		final Object[][] rowData = registro.getRowData();
            int numRow = registro.getNumRow();
            
            
            System.out.println("rowData: "+rowData[1][100]);
            
    		
    		table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(1190,632));
            table.setFillsViewportHeight(true);
            table.setColumnSelectionAllowed(false);
            table.getTableHeader().setReorderingAllowed(false); // SPOSTAMENTO DELLE COLONNE
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setAutoResizeMode(0); //AUTO RESIZE OFF
            TableColumn colonna = null;
            // DATI JTABLE
            for(int i=0; i<rowData.length; i++) {
            	model.addColumn("<html>"+columnHeaders[i]+"</html>");
            }
            for(int i=0; i<numRow; i++) {
            	model.addRow(new Object[] {rowData[0][i],rowData[1][i],"€ "+rowData[2][i],rowData[3][i],
            								rowData[4][i],rowData[5][i],"€ "+rowData[6][i],rowData[7][i]});
            	for(int x=0; x<model.getColumnCount(); x++) {
            		table.getColumnModel().getColumn(x).setCellRenderer(new RegModelTable(checkRed));// SETUP TABELLA
            	}
            }
        		//SETUP delle COLONNE
            for(int i=0; i<rowData.length; i++) {
            	// IMPOSTAZIONI COLONNE
            	colonna = table.getColumnModel().getColumn(i);
            	colonna.setResizable(false);
            	//(String) colonna.getIdentifier()
            	switch(i) {
    	        	case 0:
    	        		colonna.setPreferredWidth(35); // ID
    	        	break;
    	        	case 1:
    	        		colonna.setPreferredWidth(85); // OPERAZIONE
    	        	break;
    	        	case 2:
    	        		colonna.setPreferredWidth(70); // IMPORTO
    	        	break;
    	        	case 3:
    	        		colonna.setPreferredWidth(80); // DATA
    	        	break;
    	        	case 4:
    	        		colonna.setPreferredWidth(650); // MOTIVO
    	        	break;
    	        	case 5:
    	        		colonna.setPreferredWidth(80); // CATEGORIA
    	        	break;
    	        	case 6:
    	        		colonna.setPreferredWidth(70); // SALDO
    	        	break;
    	        	case 7:
    	        		colonna.setPreferredWidth(80); // BOLLETTA
    	        	break;
            	}
            }
            //====================================================================================================
            table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true)); // POSIZIONE VERTICALE DELLA SCROLLPANE
    	}
    }
    Questo invece la parte di codice che richiama la JTable...
    codice:
    public PageRegistro(RegistroCassa registro) {
    		
    		// SETTING DATI JTABLE===============================================================================
    	final Object[][] rowData = registro.getRowData();
            table = new RegistroTable(registro);	
            
    	// DATI per l'invio alla classe NewRegPage
    	saldo = registro.getSaldo();
    	operazione = registro.getNum();
    
    .....
    
    // regPan JPANEL
    		JPanel regPan = new JPanel();
    		regPan.setBorder(BorderFactory.createEtchedBorder());
    		regPan.setLayout(new FlowLayout(FlowLayout.CENTER));
    		regPan.add(new JScrollPane(table));
    		table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true)); // POSIZIONE VERTICALE DELLA SCROLLPANE
    		
    		// ---------- AZIONI per JTable
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                	//Ricavo la tabella che ha prodotto l'evento
                	JTable table = (JTable)e.getSource();
                	//Calcolo la rigae colonna su cui si è fatto click
                	int rigaSelezionata = table.getSelectedRow();
                	int clickedRow = table.columnAtPoint(e.getPoint());
                	
                	Log.nor("rigaSelezionata: "+rigaSelezionata+"\nclickedRow: "+clickedRow);
                	
                	String[] infoTesto = new String[rowData.length];
                	for(int i=0; i<rowData.length; i++) {
                		infoTesto[i] = (String) table.getValueAt(rigaSelezionata,i);
        		}
                	subInfoPan.refresh(infoTesto);
            	cdLay.show(infoPan, "sub");
                }
            });
    
    .......

  8. #18
    Mannaggia, non riesco più ad ottenere la JTabel
    sbaglierò sicuramente la scrittura della classe e del metodo!!!

  9. #19
    Risolto.....

    così richiamo la JTable....
    codice:
    table = new RegistroTable(registro).refreshTable();
    ...e nella classe costruttrice ho aggiunto questo metodo che poi modificherò per il refresh...
    codice:
    public JTable refreshTable() {
     	return table;
    }
    .... ora ogni qualvolta aggiungo dei dati alla Classe RagistroCassa, farò ridisegnare la JTable e vediamo se funziona.

    ... to be continued

  10. #20
    Ho risolto il refresh della JTable... ecco come:

    Ho creato una classe che crea la JTable e riceve i dati in essa contenuti:
    codice:
    package pannelli;
    
    import java.awt.Dimension;
    
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    
    public class RegistroTable {
    	
    	final DefaultTableModel model = new DefaultTableModel();
    	private JTable table;	
    	
    	public RegistroTable(String[] checkRed, Object[][] rowData) {
    				
    		Object[] columnHeaders = {"NUM","OPERAZIONE","IMPORTO","DATA","MOTIVO","CATEGORIA","SALDO","BOLLETTA"};
    		table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(1190,632));
            table.setFillsViewportHeight(true);
            table.setColumnSelectionAllowed(false);
            table.getTableHeader().setReorderingAllowed(false); // SPOSTAMENTO DELLE COLONNE
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setAutoResizeMode(0); //AUTO RESIZE OFF
            TableColumn colonna = null;
            // DATI JTABLE
            for(int i=0; i<rowData.length; i++) {
            	model.addColumn("<html>"+columnHeaders[i]+"</html>");
            }
            for(int i=0; i<rowData[0].length; i++) {
            	model.addRow(new Object[] {rowData[0][i],rowData[1][i],"€ "+rowData[2][i],rowData[3][i],
            								rowData[4][i],rowData[5][i],"€ "+rowData[6][i],rowData[7][i]});
            	for(int x=0; x<model.getColumnCount(); x++) {
            		table.getColumnModel().getColumn(x).setCellRenderer(new RegModelTable(checkRed));// SETUP TABELLA
            	}
            }
        		//SETUP delle COLONNE
            for(int i=0; i<rowData.length; i++) {
            	// IMPOSTAZIONI COLONNE
            	colonna = table.getColumnModel().getColumn(i);
            	colonna.setResizable(false);
            	//(String) colonna.getIdentifier()
            	switch(i) {
    	        	case 0:
    	        		colonna.setPreferredWidth(35); // ID
    	        	break;
    	        	case 1:
    	        		colonna.setPreferredWidth(85); // OPERAZIONE
    	        	break;
    	        	case 2:
    	        		colonna.setPreferredWidth(70); // IMPORTO
    	        	break;
    	        	case 3:
    	        		colonna.setPreferredWidth(80); // DATA
    	        	break;
    	        	case 4:
    	        		colonna.setPreferredWidth(650); // MOTIVO
    	        	break;
    	        	case 5:
    	        		colonna.setPreferredWidth(80); // CATEGORIA
    	        	break;
    	        	case 6:
    	        		colonna.setPreferredWidth(70); // SALDO
    	        	break;
    	        	case 7:
    	        		colonna.setPreferredWidth(80); // BOLLETTA
    	        	break;
            	}
            }
    	}
    
    	public JTable drawTable() {
    		return table;
    	}
    }
    Nella classe che richiama la JTable, l'aggiornamento avviene così:
    codice:
    // rinnovo la JTable
    this.rowData = rowData;
    regPan.removeAll();
    JTable table = new RegistroTable(checkRed,rowData).drawTable();
    regPan.add(new JScrollPane(table));
    regPan.revalidate();
    table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true)); // POSIZIONE VERTICALE DELLA SCROLLPANE
    		
    // AZIONI sulla JTable
    table.addMouseListener(new MouseAdapter() {
    	public void mouseClicked(MouseEvent e) {
    		//Ricavo la tabella che ha prodotto l'evento
    		JTable table = (JTable)e.getSource();
    		//Calcolo la rigae colonna su cui si è fatto click
    		int rigaSelezionata = table.getSelectedRow();
    		int clickedRow = table.columnAtPoint(e.getPoint());
                    String[] infoTesto = new String[rowData.length];
    		for(int i=0; i<rowData.length; i++) {
    			infoTesto[i] = (String) table.getValueAt(rigaSelezionata,i);
    		}
    		subInfoPan.refresh(infoTesto);
    		cdLay.show(infoPan, "sub");
    	}
    });
    Grazie per l'aiuto

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.