Visualizzazione dei risultati da 1 a 3 su 3

Discussione: JTable Java

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2012
    Messaggi
    2

    JTable Java

    salve a tutti, sono nuovo del forum quindi vi prego di scusarmi qualora commetta qualche errore.

    Ho il seguente problema: devo sviluppare una tesi universitaria, in pratica devo collegare un database con un interfaccia java, il tutto con Hibernate. Ho il tutto corrattamente configurato per quanto riguarda il collegamento ma ho qualche problema con le JTable.

    ho due classi, la prima, che data una query, inserita in un textFiel richiama il modello della Jtable

    codice:
    package archivio_tesi;
    
    import javax.swing.JFrame;
    import java.awt.Toolkit;
    import java.awt.Font;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
    import javax.swing.JLabel;
    
    import javax.swing.JTextField;
    import javax.swing.JButton;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    import javax.swing.JTable;
    
    public class Create_query {
    
    	private JFrame frmCreazioneQuery;
    	private JTextField textField;
    	private PersonTableModel model;
    	private JTable table;
    
    
    	/**
    	 * Create the application.
    	 */
    	public Create_query() {
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frmCreazioneQuery = new JFrame();
    		frmCreazioneQuery.getContentPane().setFont(new Font("Book Antiqua", Font.PLAIN, 12));
    		frmCreazioneQuery.setFont(new Font("Book Antiqua", Font.BOLD, 12));
    		frmCreazioneQuery.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Giovanni\\workspace\\Archivio_tesi1.0\\img\\h8.png"));
    		frmCreazioneQuery.setTitle("Creazione query");
    		frmCreazioneQuery.setBounds(100, 100, 850, 429);
    		frmCreazioneQuery.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		frmCreazioneQuery.getContentPane().setLayout(null);
    		
    		JPanel panel = new JPanel();
    		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    		panel.setBounds(10, 11, 814, 35);
    		frmCreazioneQuery.getContentPane().add(panel);
    		
    		JLabel lblCreazioneQuery = new JLabel("CREAZIONE QUERY");
    		lblCreazioneQuery.setFont(new Font("Book Antiqua", Font.BOLD, 14));
    		panel.add(lblCreazioneQuery);
    		
    		JPanel panel_1 = new JPanel();
    		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    		panel_1.setBounds(10, 57, 814, 55);
    		frmCreazioneQuery.getContentPane().add(panel_1);
    		panel_1.setLayout(null);
    		
    		JLabel lblNewLabel = new JLabel("Query:");
    		lblNewLabel.setFont(new Font("Book Antiqua", Font.PLAIN, 11));
    		lblNewLabel.setBounds(10, 0, 48, 14);
    		panel_1.add(lblNewLabel);
    		
    		textField = new JTextField();
    		textField.setBounds(10, 24, 699, 20);
    		panel_1.add(textField);
    		textField.setColumns(10);
    		
    		JButton btnOk = new JButton("ESEGUI");
    		btnOk.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String hql = textField.getText();
    				model = new PersonTableModel(hql);
    				table = new JTable(model);
    				table.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    				table.setFillsViewportHeight(true);
    				table.setBounds(10, 123, 814, 177);
    				frmCreazioneQuery.getContentPane().add(table);	
    			}
    		});
    		btnOk.setFont(new Font("Book Antiqua", Font.BOLD, 13));
    		btnOk.setBounds(719, 23, 85, 23);
    		panel_1.add(btnOk);		
    		frmCreazioneQuery.setVisible(true);
    	}
    }
    e x finire il modello della jTable

    codice:
    package archivio_tesi;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import java.util.*;
    import javax.swing.table.AbstractTableModel;
    
    public class PersonTableModel extends AbstractTableModel
    {
        private static final long serialVersionUID = 6105842825518764825L;
        private ArrayList<Tesi> PersonList;
        
        public PersonTableModel(String hql)
        {
            super();
            SessionFactory sf=new Configuration().configure().buildSessionFactory();
            Session session=sf.openSession();
            
            Query q=session.createQuery(hql);
            PersonList=new ArrayList<Tesi>(q.list());
            
            session.close();
        }
    
        public int getRowCount()
        {
            return PersonList.size();
        }
    
        public int getColumnCount()
        {
            return 8;
        }
    
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            Tesi p=PersonList.get(rowIndex);
            Object[] values=new Object[]{p.getTitolo(),p.getTipologia(),p.getMateria_int(), p.getSessione(), p.getData_deposito(), p.getVoto_finale(),
                    p.getCodice_tesi(), p.getId_prof()};
            		return values[columnIndex];
        }
    
        @Override
        public String getColumnName(int column)
        {
            String[] columnNames=new String[]{"Titolo","Tipologia","Materia_int","sessione","data_deposito","voto_finale", "Codice_tesi", "Id_prof"};
            return columnNames[column];
        } 
    }
    Il problema è che quando la tabella viene visualizzata, non compare il nome dei campi ma soltanto i valori e ancora non posso settare le dimensioni dei campi in modo che venga visulaizzato tutto il campo. Sbaglio qualcosa nel codice?? qualcuno sa aiutarmi??

  2. #2
    Ciao wik89 e benvenuto,
    Il problema sta nel fatto che aggiungi la tabella direttamente ad un JPanel e non ad un ScrolPane in modo che essa possa essere "scrollata".
    Ti basta modificare questa parte di codice:
    codice:
    frmCreazioneQuery.getContentPane().add(table);
    In questo modo:
    codice:
    JScrollPane scrollPane = new JScrollPane(table);
    frmCreazioneQuery.getContentPane().add(scrollPane);
    P.S
    Quanto posti il codice usa i tag appositi.

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2012
    Messaggi
    2
    grazie mille finalemnte ci sono riuscito! GRAZIE

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.