|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
[JAVA - SWING] JButton in JTable non si aggiorna correttamente
Ciao a tutti. Dopo ore passate a scervellarmi non mi resta che ricorrere al vostro aiuto...
In breve (se necessario domani posto il codice). Sul DB: Codice:
Nome Flag Qui 1 Quo 1 Qua 1 Quindi inizialmente: Codice:
Nome JButton Qui JButton_Qui Quo JButton_Quo Qua JButton_Qua Il problema sorge quando vado ad eliminare una riga e ridisegnare il tutto. In particolare, facciamo finta che premo il bottone JButton_Qui. Viene scatenata una actionEvent che fa: update mia_tabella set flag=0 where nome = qui E anche fino a qui ci siamo. A questo punto pulisco il defaultTableModel per levare i dati caricati al giro precedente (con .clear()) e rilancio il metodo che ha caricato nella table/model i dati in precedenza, aspettando che non carichi piu Qui, in quanto il flag di Qui non è più ad 1. Questo il risultato: Codice:
Nome JButton Quo JButton_Qui Qua JButton_Qua ![]() Come sempre, grazie a tutti per l'aiuto! Ultima modifica di tylerdurden83 : 04-11-2009 alle 21:43. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Mi sa che è meglio se posti il codice...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Magari tra poco lo posto, non l' ho fatto prima perchè le classi impattate sono molte. Tuttavia, mi pare di aver risolto spostando
implements ActionListener dalla classe public class MyCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { alla classe "principale". Il motivo per cui avevo messo ActionListener insieme alla classe Editor era per poter chiamare fireEditingStopped(); Tuttavia ho letto che quando succede un problema tipo quello che ho descritto nel post precedente, è dovuto al fatto che la cella contenente il bottone (la sola che "scala" male) è ancora selezionata (e pensavo che fireEditingStopped(); la deselezionasse...). Ho spostato ActionListener alla classe principale, levato fireEditingStopped e aggiunto table.getCellEditor().stopCellEditing(); Ora pare che vada, faccio altre prove. Intanto, se la cosa per te ha senso, mi piacerebbe capirla ![]() Grazie, TD EDIT: come non detto, ora da Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 |
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Chiedevo di postare del codice perchè in genere in questo modo è più facile per gli altri capire e aiutarti a trovare l'errore. @EDIT: Non ho capito se hai un problema legato al corretto uso del modello degli eventi di Swing o se legato ad una corretta implementazione dell'editor di qualche componente...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 05-11-2009 alle 11:51. |
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Parti importanti di codice:
Codice:
// classe principale class JavaTecConsole extends TablesGeneric implements Runnable, ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); try { // mette il flag relativo al nome cliccato a 0 sul DB cosi non viene piu ricaricato Utilities.doUpdate(Long.parseLong(e.getActionCommand())); synchronized (this.datiRighe){ this.datiRighe.notifyAll(); } } @Override void createVector(Event event){ Vector<Object> vector = new Vector<Object>(); vector.addElement(event.getHostname()); // aggiunge i vari element, e per finire il bottone JButton startButton = new JButton("Close"+event.getId()); startButton.setActionCommand(String.valueOf(event.getId())); startButton.addActionListener(this); vector.addElement(startButton); // datiRighe è un defaulttablemodel definito in TablesGeneric this.datiRighe.add(0, vector); } @Override public void run() { try { makeUI(); this.setLocation(0,223); this.pack(); this.setVisible(true); this.getContentPane().validate(); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { stoppato=true; } }); List<Event> eventList = null;// doSelect(); while (stoppato==false) { datiRighe.clear(); eventList = doSelect(); Iterator<Event> it = eventList.iterator(); while (it.hasNext()){ Event event = it.next(); int totaleRighe = datiRighe.size(); if (totaleRighe == 400){ datiRighe.remove(399); model.rowsRemoved(new TableModelEvent (table.getModel(), 0, table.getRowCount(), 7, TableModelEvent.DELETE)); } createVector(event); model.newRowsAdded(new TableModelEvent (table.getModel(), 0, 0, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT)); } this.mce.setDatiRighe(this.datiRighe); synchronized (this.datiRighe) { this.datiRighe.wait(1000000L); } } Codice:
this.datiRighe = new Vector<Vector<Object>>(); // create the DefaultTableModel with the vector of vectors to hold row // data, and the vector of column names this.model = new DefaultTableModel(this.datiRighe, columnNames2); // create the jtable using the DefaultTableModel this.table = new JTable(this.model){ // this method is necessary in order to render properly the objects in the cells // with this, the appropriate renderer will be used // without this, the only way to render stuff properly would be to specify the type // of renderer to use for each different column @Override public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; // install the default renderer and editor for all MyJButton objects stored in the table //this.table.setDefaultRenderer(MyJButton.class, new MyJButtonRenderer()); //this.table.setDefaultEditor(MyJButton.class, new MyCellEditor()); this.table.setDefaultRenderer(JButton.class, new MyJButtonRenderer()); this.table.setDefaultEditor(JButton.class, new MyCellEditor()); Codice:
public class MyJButtonRenderer extends JButton implements TableCellRenderer { private String buttonText=""; private Border selectedBorder = null; private Border unselectedBorder = null; public MyJButtonRenderer(String s){ this.buttonText=s; } public MyJButtonRenderer(){ } @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { if (selectedBorder == null) { selectedBorder = BorderFactory.createMatteBorder(2,5,2,5, table.getSelectionBackground()); } setBorder(selectedBorder); } else { if (unselectedBorder == null) { unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5, table.getBackground()); } setBorder(unselectedBorder); } setText( (value == null) ? "" : ((JButton)value).getText()); setToolTipText("RGB value: "); return this; } } Codice:
public class MyCellEditor extends AbstractCellEditor implements TableCellEditor { public MyCellEditor(){ } private JavaTecConsole jtc; private Vector<Vector<Object>> datiRighe; private JTable jtable; void setJTC(JavaTecConsole jtc){ this.jtc=jtc; } void setJTable(JTable jt){ this.jtable=jt; } void setDatiRighe(Vector<Vector<Object>> datiRighe){ this.datiRighe=datiRighe; if (this.jbutton!=null) System.out.println("JButton AE="+this.jbutton.getActionCommand()); } MyCellEditor(JavaTecConsole jtc){ this.jtc=jtc; } private JButton jbutton; private String text; @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { text = (value == null) ? "" : ((JButton)value).getText(); jbutton = (JButton)value; return jbutton; } @Override public Object getCellEditorValue() { return this.jbutton; } } Ultima modifica di tylerdurden83 : 05-11-2009 alle 11:56. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:48.