|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Feb 2006
Città: Catanzaro
Messaggi: 186
|
[Java] piccolo problemino con jTable
raga ho un piccolo problema..
praticamente ho creato una jTable cone dei campi editabili, il problema è questo che quando inserisco deii valori nelle celle "con l'applicazione avviata", il valore viene inserito ma subito dopo cancellato, noto che facendo un metodo di stampa il valore viene stampato insieme alla sua posizione, praticamente il problema è che il valore non viene scritto nella cella... ecco il codice che io ho utilizzato quetsa e la tabella: package InterfaccieGrafiche; import java.awt.BorderLayout; import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import java.awt.SystemColor; public class DatiProcessi { private int nProcessi; private JInternalFrame frame = null; // @jve:decl-index=0:visual-constraint="191,68" private JPanel jPanelPricipale = null; private JPanel jPanelTitolo = null; private JPanel jPanelCentro = null; private JPanel jPanelBottoni = null; private JLabel jLabelIntestazione = null; private JButton jButtonIndietro = null; private JButton jButtonChiudi = null; private JButton jButtonElabora = null; private nProcessi nP ; private JScrollPane jScrollPane = null; private JTable jTable = null; protected JInternalFrame createFrame(final JDesktopPane desktop,int m) { setNProcessi(m); frame = new JInternalFrame(); frame.setTitle("Descrizione Processi"); frame.setResizable(false); frame.setClosable(true); frame.setMaximizable(false); frame.setLocation(120, 50); frame.setIconifiable(false); frame.setContentPane(getJPanelPricipale()); frame.setSize(579, 362); // Action List jButtonChiudi.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { frame.dispose(); } }); // Action List jButtonIndietro.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(nP==null) nP = new nProcessi(); JInternalFrame f = nP.createFrame(desktop); desktop.add(f); f.setVisible(true); frame.dispose(); } }); return frame; } /** * This method initializes jPanelPricipale * * @return javax.swing.JPanel */ private JPanel getJPanelPricipale() { if (jPanelPricipale == null) { jPanelPricipale = new JPanel(); jPanelPricipale.setLayout(new BorderLayout()); jPanelPricipale.add(getJPanelTitolo(), BorderLayout.NORTH); jPanelPricipale.add(getJPanelCentro(), BorderLayout.CENTER); jPanelPricipale.add(getJPanelBottoni(), BorderLayout.SOUTH); } return jPanelPricipale; } /** * This method initializes jPanelTitolo * * @return javax.swing.JPanel */ private JPanel getJPanelTitolo() { if (jPanelTitolo == null) { jLabelIntestazione = new JLabel(); jLabelIntestazione.setText("Per proseguire inserire i dati richiesti."); jLabelIntestazione.setForeground(Color.red); jLabelIntestazione.setPreferredSize(new Dimension(342, 45)); jLabelIntestazione.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); jPanelTitolo = new JPanel(); jPanelTitolo.setLayout(new GridBagLayout()); jPanelTitolo.setForeground(Color.red); jPanelTitolo.add(jLabelIntestazione); } return jPanelTitolo; } /** * This method initializes jPanelCentro * * @return javax.swing.JPanel */ private JPanel getJPanelCentro() { if (jPanelCentro == null) { GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.fill = GridBagConstraints.BOTH; gridBagConstraints1.gridy = 0; gridBagConstraints1.weightx = 1.0; gridBagConstraints1.weighty = 1.0; gridBagConstraints1.gridx = 0; GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridheight = 10; gridBagConstraints.gridy = 0; gridBagConstraints.gridx = 0; jPanelCentro = new JPanel(); jPanelCentro.setLayout(new GridBagLayout()); jPanelCentro.add(getJScrollPane(), gridBagConstraints1); } return jPanelCentro; } /** * This method initializes jPanelBottoni * * @return javax.swing.JPanel */ private JPanel getJPanelBottoni() { if (jPanelBottoni == null) { jPanelBottoni = new JPanel(); jPanelBottoni.setLayout(new GridBagLayout()); jPanelBottoni.setPreferredSize(new Dimension(240, 35)); jPanelBottoni.add(getJButtonIndietro(), new GridBagConstraints()); jPanelBottoni.add(getJButtonChiudi(), new GridBagConstraints()); jPanelBottoni.add(getJButtonElabora(), new GridBagConstraints()); } return jPanelBottoni; } /** * This method initializes jButtonIndietro * * @return javax.swing.JButton */ private JButton getJButtonIndietro() { if (jButtonIndietro == null) { jButtonIndietro = new JButton("Back"); jButtonIndietro.setPreferredSize(new Dimension(80, 23)); } return jButtonIndietro; } /** * This method initializes jButtonChiudi * * @return javax.swing.JButton */ private JButton getJButtonChiudi() { if (jButtonChiudi == null) { jButtonChiudi = new JButton("Annulla"); jButtonChiudi.setPreferredSize(new Dimension(80, 23)); } return jButtonChiudi; } /** * This method initializes jButtonElabora * * @return javax.swing.JButton */ private JButton getJButtonElabora() { if (jButtonElabora == null) { jButtonElabora = new JButton("Elabora"); jButtonElabora.setName("Elabora"); jButtonElabora.setText("Elabora"); jButtonElabora.setPreferredSize(new Dimension(80, 23)); } return jButtonElabora; } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setViewportView(getJTable()); } return jScrollPane; } /** * This method initializes jTable * * @return javax.swing.JTable */ private JTable getJTable() { try{ if (jTable == null) { jTable = new JTable(); jTable.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jTable.setColumnSelectionAllowed(false); jTable.setSize(new Dimension(550, 80)); jTable.setBackground(SystemColor.desktop); jTable.setFont(new Font("Tahoma", Font.PLAIN, 13)); jTable.setRowHeight(21); jTable.setGridColor(Color.orange); jTable.setShowGrid(true); jTable.setForeground(Color.red); jTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); jTable.setModel(new TableMode(getNProcessi())); } }catch (Exception e) { // TODO: handle exception } return jTable; } public int getNProcessi() { return nProcessi; } public void setNProcessi(int processi) { nProcessi = processi; } } questa AbstractTableModel package InterfaccieGrafiche; import javax.swing.table.AbstractTableModel; @SuppressWarnings("serial") public class TableMode extends AbstractTableModel { /** * */ private static final long serialVersionUID = 1L; private int nProcessi; private int priorita; public TableMode(int nProcessi) { this.nProcessi = nProcessi; } public String getColumnName(int col) { if (col == 0) { return "Nome processo"; } if (col == 1) { return "Tempo di arrivo"; } if (col == 2) { return "Durata esecuzione"; } if (col == 3) { return "Priorità"; } return null; } public int getColumnCount() { return 4; } public int getRowCount() { return nProcessi; } public Object getValueAt(int row, int col) { priorita = 5; String nome[] = null; nome = new String[nProcessi]; Object result = null; if(col==0){ for (int i= 0; i<nProcessi;i++){ nome[i]="Processo "+i; } result=nome[row]; return result; } if (col==3) return getPriorita(); return null; } public boolean isCellEditable(int row, int col) {return ( col > 0);} public void setValueAt(Object new_value, int row, int col) { String s = (String)new_value; try { int i = new Integer(s).intValue(); if (i!=-1){ System.out.println("Il valore è :"+i); System.out.println("row :"+row+" col :"+col); super.fireTableDataChanged(); } else javax.swing.JOptionPane.showMessageDialog(null,"Il valore inserito non è corretto."); } catch(RuntimeException e) {System.out.println("Ecce tableModel SetvalueAt :"+e); } } public int getPriorita() { return priorita; } public void setPriorita(int priorita) { this.priorita = priorita; } } MI sapreste aiutare..???? Grazie
__________________
@CpU 930 |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
Sei tu che devi preoccuparti di mantenere i dati da qualche parte ... in un array bidimensionale di oggetti, in un array di "record" (una tua classe che "rappresenta" una riga) o quello che vuoi. EDIT: il tuo codice è scritto molto male, principalmente a livello di stile di scrittura (spaziature, indentazioni, ecc....)
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) Ultima modifica di andbin : 24-08-2007 alle 22:46. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 01:50.




















