Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [Java]Visualizzazione dati JTable

    salve a tutti.
    il mio problema è il seguente: dopo aver creato una JTable non riesco a visualizzare i dati contenuti nel vettore.
    posto qui il codice.

    ----frame contentente la JTable con il relativo modello----
    public class TabellaJFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JScrollPane jScrollPane = null;
    private JTable jTable = null;
    private JPanel jPanel2 = null;
    private JLabel nomeJL = null;
    private JLabel cognomeJL = null;
    private JLabel lavoroJL = null;
    private JTextField nomeJTF = null;
    private JTextField cognomeJTF = null;
    private JTextField lavoroJTF = null;
    private JButton okJB = null;
    public Vector data = new Vector();

    class MyTableModel extends AbstractTableModel {

    private String[] columnNames = { "Nome", "Cognome", "Lavoro" };

    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.size();
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    GenericBean gb = new GenericBean();
    gb = (GenericBean) data.get(row);
    switch (col) {
    case 0:
    return gb.getNome();
    case 1:
    return gb.getCognome();
    case 2:
    return gb.getLavoro();
    default:
    return "";
    }
    }

    public void setValueAt(Object value, int row, int col) {
    GenericBean gb = new GenericBean();
    gb = (GenericBean) data.get(row);
    switch (col) {
    case 0:
    gb.setNome((String) value);
    break;
    case 1:
    gb.setCognome((String) value);
    break;
    case 2:
    gb.setLavoro((String) value);
    break;
    default:
    return;
    }
    fireTableCellUpdated(row, col);
    }
    }

    /**
    * This method initializes jScrollPane
    *
    * @return javax.swing.JScrollPane
    */
    private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
    try {
    jScrollPane = new JScrollPane();
    jScrollPane.setBounds(new Rectangle(1, 1, 462, 231));
    jScrollPane.setViewportView(getJTable());
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return jScrollPane;
    }

    /**
    * This method initializes jTable
    *
    * @return javax.swing.JTable
    */
    private JTable getJTable() {
    if (jTable == null) {
    try {
    jTable = new JTable(new MyTableModel());
    jTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQ UENT_COLUMNS);
    jTable.setCellSelectionEnabled(true);
    jTable.setColumnSelectionAllowed(true);
    jTable.setEnabled(true);
    jTable.setGridColor(Color.black);
    jTable.setShowGrid(true);
    jTable.setBackground(Color.white);
    jTable.setVisible(true);
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return jTable;
    }

    /**
    * This method initializes jPanel2
    *
    * @return javax.swing.JPanel
    */
    private JPanel getJPanel2() {
    if (jPanel2 == null) {
    try {
    lavoroJL = new JLabel();
    lavoroJL.setBounds(new Rectangle(325, 10, 120, 25));
    lavoroJL.setName("lavoroJL");
    lavoroJL.setPreferredSize(new Dimension(120, 25));
    lavoroJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFINE D);
    lavoroJL.setText("Lavoro");
    lavoroJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    cognomeJL = new JLabel();
    cognomeJL.setBounds(new Rectangle(150, 10, 160, 25));
    cognomeJL.setName("cognomeJL");
    cognomeJL.setPreferredSize(new Dimension(160, 25));
    cognomeJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFIN ED);
    cognomeJL.setText("Cognome");
    cognomeJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    nomeJL = new JLabel();
    nomeJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED) ;
    nomeJL.setName("nomeJL");
    nomeJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    nomeJL.setSize(new Dimension(120, 25));
    nomeJL.setLocation(new Point(15, 10));
    nomeJL.setPreferredSize(new Dimension(120, 25));
    nomeJL.setText("Nome");
    jPanel2 = new JPanel();
    jPanel2.setLayout(null);
    jPanel2.setBounds(new Rectangle(1, 232, 462, 125));
    jPanel2.setBackground(new Color(255, 255, 227));
    jPanel2.add(nomeJL, null);
    jPanel2.add(cognomeJL, null);
    jPanel2.add(lavoroJL, null);
    jPanel2.add(getNomeJTF(), null);
    jPanel2.add(getCognomeJTF(), null);
    jPanel2.add(getLavoroJTF(), null);
    jPanel2.add(getOkJB(), null);
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return jPanel2;
    }

    /**
    * This method initializes nomeJTF
    *
    * @return javax.swing.JTextField
    */
    private JTextField getNomeJTF() {
    if (nomeJTF == null) {
    try {
    nomeJTF = new JTextField();
    nomeJTF.setBounds(new Rectangle(15, 45, 120, 25));
    nomeJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    nomeJTF.setName("nomeJTF");
    nomeJTF.setPreferredSize(new Dimension(120, 25));
    nomeJTF.setColumns(100);
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return nomeJTF;
    }

    /**
    * This method initializes cognomeJTF
    *
    * @return javax.swing.JTextField
    */
    private JTextField getCognomeJTF() {
    if (cognomeJTF == null) {
    try {
    cognomeJTF = new JTextField();
    cognomeJTF.setBounds(new Rectangle(150, 45, 160, 25));
    cognomeJTF.setName("cognomeJTF");
    cognomeJTF.setPreferredSize(new Dimension(160, 25));
    cognomeJTF.setColumns(100);
    cognomeJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return cognomeJTF;
    }

    /**
    * This method initializes lavoroJTF
    *
    * @return javax.swing.JTextField
    */
    private JTextField getLavoroJTF() {
    if (lavoroJTF == null) {
    try {
    lavoroJTF = new JTextField();
    lavoroJTF.setBounds(new Rectangle(325, 45, 120, 25));
    lavoroJTF.setName("lavoroJTF");
    lavoroJTF.setPreferredSize(new Dimension(120, 25));
    lavoroJTF.setColumns(100);
    lavoroJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return lavoroJTF;
    }

    /**
    * This method initializes okJB
    *
    * @return javax.swing.JButton
    */
    private JButton getOkJB() {
    if (okJB == null) {
    try {
    okJB = new JButton();
    okJB.setHorizontalTextPosition(SwingConstants.CENT ER);
    okJB.setSize(new Dimension(130, 35));
    okJB.setLocation(new Point(165, 80));
    okJB.setName("okJB");
    okJB.setPreferredSize(new Dimension(130, 35));
    okJB.setText("OK");
    okJB.setFont(new Font("Tahoma", Font.BOLD, 16));
    okJB.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    processOK();
    }
    });
    } catch (java.lang.Throwable e) {
    e.printStackTrace();
    }
    }
    return okJB;
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    TabellaJFrame thisClass = new TabellaJFrame();
    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    thisClass.setVisible(true);
    }
    });
    }

    /**
    * This is the default constructor
    */
    public TabellaJFrame() {
    super();
    initialize();
    }

    /**
    * This method initializes this
    *
    * @return void
    */
    private void initialize() {
    this.setContentPane(getJContentPane());
    this.setTitle("Tabella");
    this.setResizable(false);
    this.setPreferredSize(new Dimension(470, 350));
    this.setName("frame");
    this.setMinimumSize(new Dimension(470, 350));
    this.setMaximumSize(new Dimension(470, 350));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setBounds(new Rectangle(0, 0, 470, 390));
    this.setVisible(true);
    }

    /**
    * This method initializes jContentPane
    *
    * @return javax.swing.JPanel
    */
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.setName("jpanel");
    jContentPane.setBackground(Color.white);
    jContentPane.add(getJScrollPane(), null);
    jContentPane.add(getJPanel2(), null);
    }
    return jContentPane;
    }

    private void processOK() {
    GenericBean gb = new GenericBean();
    gb.setNome(getNomeJTF().getText());
    gb.setCognome(getCognomeJTF().getText());
    gb.setLavoro(getLavoroJTF().getText());
    data.add(gb);
    }
    }

    ------sorgente del bean utilizzato---

    public class GenericBean {

    private String nome = new String();
    private String cognome = new String();
    private String lavoro = new String();

    /**
    *
    */
    public GenericBean(String nome, String cognome, String lavoro) {
    super();
    this.nome = nome;
    this.cognome = cognome;
    this.lavoro = lavoro;
    }

    public GenericBean() {
    super();
    }

    /**
    * @return the nome
    */
    public String getNome() {
    return nome;
    }

    /**
    * @param nome
    * the nome to set
    */
    public void setNome(String nome) {
    this.nome = nome;
    }

    /**
    * @return the cognome
    */
    public String getCognome() {
    return cognome;
    }

    /**
    * @param cognome
    * the cognome to set
    */
    public void setCognome(String cognome) {
    this.cognome = cognome;
    }

    /**
    * @return the lavoro
    */
    public String getLavoro() {
    return lavoro;
    }

    /**
    * @param lavoro
    * the lavoro to set
    */
    public void setLavoro(String lavoro) {
    this.lavoro = lavoro;
    }

    }

    qualcuno di voi può aiutarmi a risolvere questo problema??
    grazie!

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

    Re: [Java]Visualizzazione dati JTable

    Originariamente inviato da darksoullight88
    il mio problema è il seguente: dopo aver creato una JTable non riesco a visualizzare i dati contenuti nel vettore.
    Non basta fare

    data.add(gb);

    Bisogna anche invocare fireTableRowsInserted() del table model per "far sapere" a chi visualizza i dati (il JTable) che una riga è stata aggiunta.

    In genere la struttura dati (nel tuo caso è il Vector) la si mette tipicamente dentro il table model, non fuori. E nel tuo table model potresti mettere un metodo che riceve un GenericBean, il metodo si occuperà di inserirlo fisicamente nel vettore e di fare il "fire" della modifica.
    Questo sarebbe un design più appropriato.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3

    grazie mille

    grazie mille per il tuo aiuto e per il tuo tempo.
    posto qui il codice opportunamente modificato nel caso in cui possa servire a qualcun altro.

    codice:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.table.AbstractTableModel;
    
    /**
     * @author marialaura
     * 
     */
    public class TabellaJFrame extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JScrollPane jScrollPane = null;
    	private JTable jTable = null;
    	private JPanel jPanel2 = null;
    	private JLabel nomeJL = null;
    	private JLabel cognomeJL = null;
    	private JLabel lavoroJL = null;
    	private JTextField nomeJTF = null;
    	private JTextField cognomeJTF = null;
    	private JTextField lavoroJTF = null;
    	private JButton okJB = null;
    
    	class MyTableModel extends AbstractTableModel {
    		private Vector data = new Vector();
    		int row;
    		private String[] columnNames = { "Nome", "Cognome", "Lavoro" };
    
    		public int getColumnCount() {
    			return columnNames.length;
    		}
    
    		public int getRowCount() {
    			return data.size();
    		}
    
    		public String getColumnName(int col) {
    			return columnNames[col];
    		}
    
    		public Object getValueAt(int row, int col) {
    			GenericBean gb = new GenericBean();
    			gb = (GenericBean) data.get(row);
    			switch (col) {
    			case 0:
    				return gb.getNome();
    			case 1:
    				return gb.getCognome();
    			case 2:
    				return gb.getLavoro();
    			default:
    				return "";
    			}
    		}
    
    		public void setValueAt(Object value, int row, int col) {
    			GenericBean gb = new GenericBean();
    			gb = (GenericBean) data.get(row);
    			switch (col) {
    			case 0:
    				gb.setNome((String) value);
    				break;
    			case 1:
    				gb.setCognome((String) value);
    				break;
    			case 2:
    				gb.setLavoro((String) value);
    				break;
    			default:
    				return;
    			}
    			fireTableCellUpdated(row, col);
    		}
    
    		public void addRows(GenericBean gb) {
    			data.add(gb);
    			fireTableRowsInserted(row, row + 1);
    			row++;
    		}
    	}
    
    	private MyTableModel tm = new MyTableModel();
    
    	/**
    	 * This method initializes jScrollPane
    	 * 
    	 * @return javax.swing.JScrollPane
    	 */
    	private JScrollPane getJScrollPane() {
    		if (jScrollPane == null) {
    			try {
    				jScrollPane = new JScrollPane();
    				jScrollPane.setBounds(new Rectangle(1, 1, 462, 231));
    				jScrollPane.setViewportView(getJTable());
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jScrollPane;
    	}
    
    	/**
    	 * This method initializes jTable
    	 * 
    	 * @return javax.swing.JTable
    	 */
    	private JTable getJTable() {
    		if (jTable == null) {
    			try {
    				jTable = new JTable(tm);
    				jTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    				jTable.setCellSelectionEnabled(true);
    				jTable.setColumnSelectionAllowed(true);
    				jTable.setEnabled(true);
    				jTable.setGridColor(Color.black);
    				jTable.setShowGrid(true);
    				jTable.setBackground(Color.white);
    				jTable.setVisible(true);
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jTable;
    	}
    
    	/**
    	 * This method initializes jPanel2
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJPanel2() {
    		if (jPanel2 == null) {
    			try {
    				lavoroJL = new JLabel();
    				lavoroJL.setBounds(new Rectangle(325, 10, 120, 25));
    				lavoroJL.setName("lavoroJL");
    				lavoroJL.setPreferredSize(new Dimension(120, 25));
    				lavoroJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
    				lavoroJL.setText("Lavoro");
    				lavoroJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    				cognomeJL = new JLabel();
    				cognomeJL.setBounds(new Rectangle(150, 10, 160, 25));
    				cognomeJL.setName("cognomeJL");
    				cognomeJL.setPreferredSize(new Dimension(160, 25));
    				cognomeJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
    				cognomeJL.setText("Cognome");
    				cognomeJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    				nomeJL = new JLabel();
    				nomeJL.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
    				nomeJL.setName("nomeJL");
    				nomeJL.setFont(new Font("Tahoma", Font.BOLD, 14));
    				nomeJL.setSize(new Dimension(120, 25));
    				nomeJL.setLocation(new Point(15, 10));
    				nomeJL.setPreferredSize(new Dimension(120, 25));
    				nomeJL.setText("Nome");
    				jPanel2 = new JPanel();
    				jPanel2.setLayout(null);
    				jPanel2.setBounds(new Rectangle(1, 232, 462, 125));
    				jPanel2.setBackground(new Color(255, 255, 227));
    				jPanel2.add(nomeJL, null);
    				jPanel2.add(cognomeJL, null);
    				jPanel2.add(lavoroJL, null);
    				jPanel2.add(getNomeJTF(), null);
    				jPanel2.add(getCognomeJTF(), null);
    				jPanel2.add(getLavoroJTF(), null);
    				jPanel2.add(getOkJB(), null);
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return jPanel2;
    	}
    
    	/**
    	 * This method initializes nomeJTF
    	 * 
    	 * @return javax.swing.JTextField
    	 */
    	private JTextField getNomeJTF() {
    		if (nomeJTF == null) {
    			try {
    				nomeJTF = new JTextField();
    				nomeJTF.setBounds(new Rectangle(15, 45, 120, 25));
    				nomeJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    				nomeJTF.setName("nomeJTF");
    				nomeJTF.setPreferredSize(new Dimension(120, 25));
    				nomeJTF.setColumns(100);
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return nomeJTF;
    	}
    
    	/**
    	 * This method initializes cognomeJTF
    	 * 
    	 * @return javax.swing.JTextField
    	 */
    	private JTextField getCognomeJTF() {
    		if (cognomeJTF == null) {
    			try {
    				cognomeJTF = new JTextField();
    				cognomeJTF.setBounds(new Rectangle(150, 45, 160, 25));
    				cognomeJTF.setName("cognomeJTF");
    				cognomeJTF.setPreferredSize(new Dimension(160, 25));
    				cognomeJTF.setColumns(100);
    				cognomeJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return cognomeJTF;
    	}
    
    	/**
    	 * This method initializes lavoroJTF
    	 * 
    	 * @return javax.swing.JTextField
    	 */
    	private JTextField getLavoroJTF() {
    		if (lavoroJTF == null) {
    			try {
    				lavoroJTF = new JTextField();
    				lavoroJTF.setBounds(new Rectangle(325, 45, 120, 25));
    				lavoroJTF.setName("lavoroJTF");
    				lavoroJTF.setPreferredSize(new Dimension(120, 25));
    				lavoroJTF.setColumns(100);
    				lavoroJTF.setFont(new Font("Tahoma", Font.BOLD, 13));
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return lavoroJTF;
    	}
    
    	/**
    	 * This method initializes okJB
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getOkJB() {
    		if (okJB == null) {
    			try {
    				okJB = new JButton();
    				okJB.setHorizontalTextPosition(SwingConstants.CENTER);
    				okJB.setSize(new Dimension(130, 35));
    				okJB.setLocation(new Point(165, 80));
    				okJB.setName("okJB");
    				okJB.setPreferredSize(new Dimension(130, 35));
    				okJB.setText("OK");
    				okJB.setFont(new Font("Tahoma", Font.BOLD, 16));
    				okJB.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						processOK();
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return okJB;
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				TabellaJFrame thisClass = new TabellaJFrame();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setVisible(true);
    			}
    		});
    	}
    
    	/**
    	 * This is the default constructor
    	 */
    	public TabellaJFrame() {
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		this.setContentPane(getJContentPane());
    		this.setTitle("Tabella");
    		this.setResizable(false);
    		this.setPreferredSize(new Dimension(470, 350));
    		this.setName("frame");
    		this.setMinimumSize(new Dimension(470, 350));
    		this.setMaximumSize(new Dimension(470, 350));
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setBounds(new Rectangle(0, 0, 470, 390));
    		this.setVisible(true);
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.setName("jpanel");
    			jContentPane.setBackground(Color.white);
    			jContentPane.add(getJScrollPane(), null);
    			jContentPane.add(getJPanel2(), null);
    		}
    		return jContentPane;
    	}
    
    	private void processOK() {
    		GenericBean gb = new GenericBean();
    		gb.setNome(getNomeJTF().getText());
    		gb.setCognome(getCognomeJTF().getText());
    		gb.setLavoro(getLavoroJTF().getText());
    		tm.addRows(gb);
    	}
    }

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

    Re: grazie mille

    Originariamente inviato da darksoullight88
    codice:
    		public void addRows(GenericBean gb) {
    			data.add(gb);
    			fireTableRowsInserted(row, row + 1);
    			row++;
    		}
    Ci siamo ... quasi. Innanzitutto l'indice della riga aggiunta lo puoi facilmente dedurre dal size() del Vector. Inoltre al fireTableRowsInserted basta passare per entrambi i parametri lo stesso valore (aggiungi 1 riga, non di più).

    Detto in codice:

    data.add(gb);
    int row = data.size ()-1;
    fireTableRowsInserted(row, row);
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    grazie mille di nuovo per il tuo 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.