HELP POUR DU JAVA

Résolu/Fermé
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011 - 19 nov. 2009 à 10:11
 vintz72 - 22 déc. 2009 à 10:11
Bonjour,

Je bloque sur un programme je n'arrive pas à voir ce qui me déclenche une "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException "
La ligne incriminée est la suivante " this.model.refreshPerson(person);" je vous joint le code si quelqu'un pouvait m'orienter ce serait vraiment sympa !!
j'ai mis entre ============== le code qui pose pb....ainsi que la création de la méthode
===============
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* AddFrame.java
*
* Created on 8 nov. 2009, 15:33:14
*/
package crudapplication;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
*
* @author Administrateur
*/
public class AddFrame extends javax.swing.JFrame {

public static final int MODE_CREATE = 0;
public static final int MODE_UPDATE = 1;
private int mode = MODE_CREATE;
private PersonsModel model = null;
private Person person = null;

/** Creates new form AddFrame */
public AddFrame(PersonsModel personsTableModel, int mode) {
this.model = personsTableModel;
initComponents();
setVisible(true);
setMode(mode);
}

public AddFrame(PersonsModel personsTableModel, int mode, Person person) {

this(personsTableModel, mode);
this.person = person;
textFirstName.setText(person.getFirstName());
textLastName.setText(person.getLastName());
}

private void setMode(int mode) {
this.mode = mode;

if (mode == MODE_CREATE) {
this.setTitle("Create Mode");
} else {
this.setTitle("Update Mode");
}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

labelFirstName = new javax.swing.JLabel();
labelLastName = new javax.swing.JLabel();
textFirstName = new javax.swing.JTextField();
textLastName = new javax.swing.JTextField();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(crudapplication.CrudApplication.class).getContext().getResourceMap(AddFrame.class);
labelFirstName.setText(resourceMap.getString("labelFirstName.text")); // NOI18N
labelFirstName.setName("labelFirstName"); // NOI18N

labelLastName.setText(resourceMap.getString("labelLastName.text")); // NOI18N
labelLastName.setName("labelLastName"); // NOI18N

textFirstName.setText(resourceMap.getString("textFirstName.text")); // NOI18N
textFirstName.setName("textFirstName"); // NOI18N

textLastName.setText(resourceMap.getString("textLastName.text")); // NOI18N
textLastName.setName("textLastName"); // NOI18N

okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
okButton.setName("okButton"); // NOI18N
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});

cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
cancelButton.setName("cancelButton"); // NOI18N
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(labelLastName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(labelFirstName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
.addComponent(textLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(okButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelFirstName)
.addComponent(textFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(labelLastName)
.addComponent(textLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okButton)
.addComponent(cancelButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>
======================================================================
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {

if ((textFirstName.getText().length() > 0) && (textLastName.getText().length() > 0)) {

if (mode == MODE_CREATE) {
Person newPerson = new Person();
newPerson.setFirstName(textFirstName.getText());
newPerson.setLastName(textLastName.getText());
this.model.addPerson(newPerson);

} else {
this.person.setFirstName(textFirstName.getText());
this.person.setLastName(textLastName.getText());
======================================================================


this.model.refreshPerson(person); <<<<<<<<<<<<<<< ligne incriminée


======================================================================
}

this.dispose();
}
}

======================================================================


//la méthode qui pose pb

public void refreshPerson(Person person) { <<<<<< Methode en question
int rowIndex = persons.indexOf(person);
fireTableRowsUpdated(rowIndex, rowIndex);
}

======================================================================

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JLabel labelFirstName;
private javax.swing.JLabel labelLastName;
private javax.swing.JButton okButton;
private javax.swing.JTextField textFirstName;
private javax.swing.JTextField textLastName;
// End of variables declaration

public void setCancelButton(JButton cancelButton) {
this.cancelButton = cancelButton;
}

public void setLabelFirstName(JLabel labelFirstName) {
this.labelFirstName = labelFirstName;
}

public void setLabelLastName(JLabel labelLastName) {
this.labelLastName = labelLastName;
}

public void setOkButton(JButton okButton) {
this.okButton = okButton;
}

public void setTextFirstName(JTextField textFirstName) {
this.textFirstName = textFirstName;
}

public void setTextLastName(JTextField textLastName) {
this.textLastName = textLastName;
}
// End of variables declaration


//Geters
public JButton getCancelButton() {
return cancelButton;
}

public JLabel getLabelFirstName() {
return labelFirstName;
}

public JLabel getLabelLastName() {
return labelLastName;
}

public JButton getOkButton() {
return okButton;
}

public JTextField getTextFirstName() {
return textFirstName;
}

public JTextField getTextLastName() {
return textLastName;
}
}

MERCI
A voir également:

22 réponses

sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 11:40
Ah, c'est une erreur de COMPILATION ? Ca change tout, lol. Je pensais qu'il s'agissait d'une erreur de runtime. Ben, pour la compil: tu es sous Eclipse, à tout hasard ?
1
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:53
désolé c'est bien un runtime que je fais... le terme compiler veut dire runtime pour moi j'ai tt faux apparement
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 10:31
Salut,

Je n'ai pas lu tout ton code, mais l'exception obtenue est simple: NullPointer = une variable qui n'est pas initialisée. Et ta ligne est

this.model.refreshPerson(person)

Regardons la variable model. Déclaration:

private PersonsModel model = null; 

Initialisation dans le constructeur de AddFrame

this.model = personsTableModel; 

Tu ne donnes pas le code (il me semble) d'appel de ce constructeur. Je pense que tu l'appelles avec un paramètre personsTableModel à null (vérifie ceci en mode debug).

++
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:16
Je n'ai pas compris ta phrase "Tu ne donnes pas le code (il me semble) d'appel de ce constructeur. Je pense que tu l'appelles avec un paramètre personsTableModel à null (vérifie ceci en mode debug).
"
de quel code parles-tu ?
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722 > femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:20
Ben, ton code posté n'est pas complet, n'est-ce pas ? Tu as une classe AddFrame avec 2 constructeurs. Mais je ne vois pas où tu appelles ces constructeurs (avec un new AddFrame(...))
0
choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
19 nov. 2009 à 11:18
salut

j'ai un peu regardé ton code, il y a un truc bizarre aussi...

public static final int MODE_CREATE = 0;
public static final int MODE_UPDATE = 1;
private int mode = MODE_CREATE;

Je remarque aussi qu'un peu partout dans ton code, la valeur de ta variable sera modifiée puis testée par rapport aux attributs statiques...

Je me pose donc la question si le fait de créer un objet sur base de cette condition n'est pas la source du problème.

if (mode == MODE_CREATE) {
Person newPerson = new Person();
newPerson.setFirstName(textFirstName.getText());
newPerson.setLastName(textLastName.getText());
this.model.addPerson(newPerson); 


la valeur de la variable "mode" me paraît "nébuleuse".

0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:22
ça m'aide pas vraiment j'ai pas très bien saisi à vrai dire..
Je galère tjrs snif..
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
19 nov. 2009 à 11:28
ok ...

j'ai quand même une question..

pourquoi deux constructeurs ?

Personnellement, je verrais bien deux classes (frame) distinctes. Une pour la création et une pour l'update.
Vu qu'apparemment, le mode (crate ou upgrade) est connu puisqu'il est mis en argument dans l'appel des constructeurs.

0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:29
LE MESSAGE D'ERREUR QUE J'AI EN COMPILANT


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at crudapplication.AddFrame.okButtonActionPerformed(AddFrame.java:169)
at crudapplication.AddFrame.access$200(AddFrame.java:21)
at crudapplication.AddFrame$3.actionPerformed(AddFrame.java:102)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:36
Help please....
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 11:38
Help please....

Nanméoh... Faut lire les réponses, bonhomme... Sinon tu ne peux pas avancer
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:48
NetBeans... je ne vois tjrs pas où ça cloche pourtant NullPointer = une variable qui n'est pas initialisée comme tu dis ;
tout dépend de cette ligne "this.model.refreshPerson(person)"

c'est model qui est mal instancié ? person ?


public class AddFrame extends javax.swing.JFrame {
private PersonsModel model = null;)
private Person person = null;)
.....
public AddFrame(PersonsModel personsTableModel, int mode) {

this.model = personsTableModel;
initComponents();
setVisible(true);
setMode(mode);
}

public AddFrame(PersonsModel personsTableModel, int mode, Person person) {

this(personsTableModel, mode);
this.person = person;
textFirstName.setText(person.getFirstName());
textLastName.setText(person.getLastName());
}

private void setMode(int mode) {
this.mode = mode;

if (mode == MODE_CREATE) {
this.setTitle("Create Mode");
} else {
this.setTitle("Update Mode");
}
}

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {

if ((textFirstName.getText().length() > 0) && (textLastName.getText().length() > 0)) {

if (mode == MODE_CREATE) {
Person newPerson = new Person();
newPerson.setFirstName(textFirstName.getText());
newPerson.setLastName(textLastName.getText());
this.model.addPerson(newPerson);

} else {

this.person.setFirstName(textFirstName.getText());
this.person.setLastName(textLastName.getText());
======================================================================
this.model.refreshPerson(person);<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
======================================================================
}

this.dispose();
}

======================================================================
la méthode refreshPerson

public void refreshPerson(Person person) {
int rowIndex = persons.indexOf(person);
fireTableRowsUpdated(rowIndex, rowIndex);
}
======================================================================

Est ce que tu y vois un peu plus claire ??
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 11:51
[Edit]
L'étude du stack trace ne dévoile pas une erreur de compil, mais une erreur d'exécution :P

Est ce que tu y vois un peu plus claire ??
Nan, pas du tout. Car tu ne postes toujours pas le morceau de code demandé. Regarde donc l'endroit où tu fais un new AddFrame(...) dans ton code et assure-toi de ne pas passer un paramètre non initialisé. Voilou ///
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 11:57
Je ne fais jamais de new AddFram()...
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 11:58
Et comment appelles-tu le constructeur, alors ? Par introspection ?
0
choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
19 nov. 2009 à 12:02
ici ?

this(personsTableModel, mode);
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 12:05
Le mieux c'est de vous montrer tout mon code et d'essayer de l'executer depuis votre poste

/*
* CrudApplication.java
*/

package crudapplication;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
* The main class of the application.
*/
public class CrudApplication extends SingleFrameApplication {

/**
* At startup create and show the main frame of the application.
*/
@Override protected void startup() {
show(new CrudFrame());
}

/**
* This method is to initialize the specified window by injecting resources.
* Windows shown in our application come fully initialized from the GUI
* builder, so this additional configuration is not needed.
*/
@Override protected void configureWindow(java.awt.Window root) {
}

/**
* A convenient static getter for the application instance.
* @return the instance of CrudApplication
*/
public static CrudApplication getApplication() {
return Application.getInstance(CrudApplication.class);
}

/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(CrudApplication.class, args);
}
}
======================================================================
======================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* CurdFrame.java
*
* Created on 8 nov. 2009, 14:16:50
*/
package crudapplication;

import crudapplication.actions.AddAction;
import crudapplication.actions.DelAction;
import crudapplication.actions.LoadAction;
import crudapplication.actions.SaveAction;
import crudapplication.actions.UpdateAction;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
*
* @author Administrateur
*/
public class CrudFrame extends javax.swing.JFrame {

PersonsModel tableModel = null;
private boolean a = true;
private boolean b = false;
private Action loadAction = null;
private Action addAction = null;
private DelAction delAction = null;
private UpdateAction updAction = null;
private Action saveAction = null;

/** Creates new form CurdFrame */
public CrudFrame() {

super("CRUD_FRAME_APPLICATION");
initComponents();

creationTable.setComponentPopupMenu(jPopupMenu1);

tableModel = new PersonsModel();
creationTable.setModel(tableModel);
creationTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

int[] selectedRows = creationTable.getSelectedRows();
for (int row = 0; row < selectedRows.length; row++) {
selectedRows[row] = creationTable.convertRowIndexToModel(row);
}
delAction.setSelectedRows(selectedRows);
updAction.setSelectedRows(selectedRows);
}
}); //int selectedRow = creationTable.convertRowIndexToModel(creationTable.getSelectedRow());
//==============================================================================
// ACTIONS
//==============================================================================
loadAction = new LoadAction(tableModel);
saveAction = new SaveAction(tableModel);
addAction = new AddAction(tableModel);
delAction = new DelAction(tableModel);
delAction.setEnabled(false);
updAction = new UpdateAction(tableModel);
updAction.setEnabled(false);

//==============================================================================
//Setting action
//==============================================================================

bLoad.setAction(loadAction);
loadMenuItem.setAction(loadAction);
loadMenuItem.setMnemonic(KeyEvent.VK_L);
loadMenuItem.setAccelerator(KeyStroke.getKeyStroke('L',InputEvent.CTRL_DOWN_MASK));

bSave.setAction(saveAction);
saveAsMenuItem.setAction(saveAction);
saveAsMenuItem.setMnemonic(KeyEvent.VK_S);
saveAsMenuItem.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_DOWN_MASK));

bAdd.setAction(addAction);

bDel.setAction(delAction);
deleteMenuItem.setAction(delAction);
deleteMenuItem.setMnemonic(KeyEvent.VK_DELETE);
//deleteMenuItem.setAccelerator(KeyStroke.getKeyStroke('Supprimer',InputEvent.

bUp.setAction(updAction);

jPopupMenu1.add(delAction);
jPopupMenu1.add(updAction);


}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPopupMenu1 = new javax.swing.JPopupMenu();
jScrollPane1 = new javax.swing.JScrollPane();
creationTable = new javax.swing.JTable();
bAdd = new javax.swing.JButton();
bDel = new javax.swing.JButton();
bUp = new javax.swing.JButton();
bLoad = new javax.swing.JButton();
bSave = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
loadMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
exitMenuItem = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
deleteMenuItem = new javax.swing.JMenuItem();

jPopupMenu1.setName("jPopupMenu1"); // NOI18N

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N

jScrollPane1.setName("jScrollPane1"); // NOI18N

creationTable.setAutoCreateRowSorter(true);
creationTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
creationTable.setName("creationTable"); // NOI18N
jScrollPane1.setViewportView(creationTable);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(crudapplication.CrudApplication.class).getContext().getResourceMap(CrudFrame.class);
creationTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("creationTable.columnModel.title0")); // NOI18N
creationTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("creationTable.columnModel.title1")); // NOI18N
creationTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("creationTable.columnModel.title2")); // NOI18N
creationTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("creationTable.columnModel.title3")); // NOI18N

bAdd.setText(resourceMap.getString("bAdd.text")); // NOI18N
bAdd.setName("bAdd"); // NOI18N

bDel.setText(resourceMap.getString("bDel.text")); // NOI18N
bDel.setEnabled(false);
bDel.setName("bDel"); // NOI18N

bUp.setText(resourceMap.getString("bUp.text")); // NOI18N
bUp.setEnabled(false);
bUp.setName("bUp"); // NOI18N

bLoad.setText(resourceMap.getString("bLoad.text")); // NOI18N
bLoad.setName("bLoad"); // NOI18N

bSave.setText(resourceMap.getString("bSave.text")); // NOI18N
bSave.setName("bSave"); // NOI18N

jMenuBar1.setName("jMenuBar1"); // NOI18N

jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
jMenu1.setName("jMenu1"); // NOI18N

loadMenuItem.setText(resourceMap.getString("loadMenuItem.text")); // NOI18N
loadMenuItem.setName("loadMenuItem"); // NOI18N
jMenu1.add(loadMenuItem);

saveAsMenuItem.setText(resourceMap.getString("saveAsMenuItem.text")); // NOI18N
saveAsMenuItem.setName("saveAsMenuItem"); // NOI18N
jMenu1.add(saveAsMenuItem);

jSeparator1.setName("jSeparator1"); // NOI18N
jMenu1.add(jSeparator1);

exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
jMenu1.add(exitMenuItem);

jMenuBar1.add(jMenu1);

jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
jMenu2.setName("jMenu2"); // NOI18N

deleteMenuItem.setText(resourceMap.getString("deleteMenuItem.text")); // NOI18N
deleteMenuItem.setName("deleteMenuItem"); // NOI18N
jMenu2.add(deleteMenuItem);

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(bAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bDel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bUp))
.addGroup(layout.createSequentialGroup()
.addComponent(bLoad)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bSave))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bLoad)
.addComponent(bSave))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bAdd)
.addComponent(bDel)
.addComponent(bUp))
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new CrudFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bAdd;
private javax.swing.JButton bDel;
private javax.swing.JButton bLoad;
private javax.swing.JButton bSave;
private javax.swing.JButton bUp;
private javax.swing.JTable creationTable;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JMenuItem loadMenuItem;
private javax.swing.JMenuItem saveAsMenuItem;
// End of variables declaration
}
======================================================================
======================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crudapplication;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
*
* @author Administrateur
*/
public class PersonsModel extends AbstractTableModel {

private static String[] columns = {"FirstName", "LastName"};
private List<Person> persons = new ArrayList<Person>();

public PersonsModel() {

/* Person person = new Person();
person.setFirstName("Rachid");
person.setLastName("FLICI");
persons.add(person);

Person person2 = new Person();
person2.setFirstName("Jean");
person2.setLastName("AUDIBERT");
persons.add(person2);

Person person3 = new Person();
person3.setFirstName("Saïd");
person3.setLastName("LAYADI");
persons.add(person3);*/
}

public void addPerson(Person person) {
persons.add(person);
int rowIndex = persons.indexOf(person);
this.fireTableRowsInserted(rowIndex, rowIndex);
}

public Person getPerson(int indexRowSelected) {
return persons.get(indexRowSelected);
}

public List<Person> getAll() {
return this.persons;
}

public void removePerson(int indexRowSelected) {
persons.remove(indexRowSelected);
this.fireTableRowsDeleted(indexRowSelected, indexRowSelected);
}

public void refreshPerson(Person person) {
int rowIndex = persons.indexOf(person);
fireTableRowsUpdated(rowIndex, rowIndex);
}

@Override
public String getColumnName(int column) {
try {
return columns[column];
} catch (ArrayIndexOutOfBoundsException e) {
return "N/A";
}
}

public int getRowCount() {
// throw new UnsupportedOperationException("Not supported yet.");
return persons.size();
}

public int getColumnCount() {
// throw new UnsupportedOperationException("Not supported yet.");
return 2;
}

public Object getValueAt(int rowIndex, int columnIndex) {
//throw new UnsupportedOperationException("Not supported yet.");
Object valeur;
Person person = persons.get(rowIndex);

switch (columnIndex) {
case 0:
valeur = person.getFirstName();
break;
case 1:
valeur = person.getLastName();
break;
default:
valeur = null;
}
return valeur;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Person person = this.persons.get(rowIndex);

switch (columnIndex) {
case 0:
person.setFirstName(aValue.toString());
break;
case 1:
person.setLastName(aValue.toString());
break;
}
}

public void removePerson(int[] selectedRows) {

List<Person> toDelete = new ArrayList<Person>(selectedRows.length);
for (int i = 0; i < selectedRows.length; i++) {
toDelete.add(persons.get(i));
}
for (Person person : toDelete) {
int index = persons.indexOf(person);
persons.remove(person);
this.fireTableRowsDeleted(index, index);
}
}
}
======================================================================
======================================================================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package crudapplication.actions;

import crudapplication.AddFrame;
import crudapplication.Person;
import crudapplication.PersonsModel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

/**
*
* @author FRWD9529
*/
public class UpdateAction extends AbstractAction {

private PersonsModel tableModel = null;
int selectedRow = -1;

public UpdateAction(PersonsModel tableModel) {
super("Update");
this.tableModel = tableModel;
}

public void setSelectedRows(int[] selectedRows) {

setEnabled(selectedRows.length == 1);

if (selectedRows.length == 1) {
this.selectedRow = selectedRows[0];
} else {
this.selectedRow = -1;
}

}

public void actionPerformed(ActionEvent e) {
if (this.selectedRow == -1) return;

Person person = tableModel.getPerson(selectedRow);
AddFrame updatePerson = new AddFrame(null, AddFrame.MODE_UPDATE, person);
}
}
============================================================================================================================================
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 12:13
Et la classe Person ?
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 12:14
new AddFrame(null, AddFrame.MODE_UPDATE, person);
T'avais raison j'ai mis null...trop nul
j'ai modifié un grand merci SANDUL
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
19 nov. 2009 à 12:14
De rien =)
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 12:41
J'ai oublié de t'indiquer ma méthode removePerson()

public void removePerson(int[] selectedRows) {

List<Person> toDelete = new ArrayList<Person>(selectedRows.length);
for (int i = 0; i < selectedRows.length; i++) {
toDelete.add(persons.get(i));
}
for (Person person : toDelete) {
int index = persons.indexOf(person);
persons.remove(person);
this.fireTableRowsDeleted(index, index);
}
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 12:38
Autre petit (grand?) pb, brievement mon appli s'ouvre avec une fenetre et de là g un bouton load qui charge un fichier texte (nom et prenom) et le met dans ma table ( 2 colonnes et ligne dynamiques...)
quand je clik et veux supprimer la ligne 3 par exemple il me supprime la ligne a l'indice 0 (ma 1ere ligne)
pourtant je pensais qu'en faisant " selectedRows[row] = creationTable.convertRowIndexToModel(row) " cela corrigerait mon pb...
encore une fois je fais appel à ta lumière SANDUL

========================================================================
public void valueChanged(ListSelectionEvent e) {

int[] selectedRows = creationTable.getSelectedRows();
for (int row = 0; row < selectedRows.length; row++) {
selectedRows[row] = creationTable.convertRowIndexToModel(row);<<<<<<<<<<<<<<
}

delAction.setSelectedRows(selectedRows);
updAction.setSelectedRows(selectedRows);
}
});
========================================================================
public class DelAction extends AbstractAction {

private int[] selectedRows = new int[0];
private PersonsModel model = null;

public DelAction(PersonsModel model) {
super("Delete…");
this.model = model;
}

public void setSelectedRows(int[] selectedRows) {
this.selectedRows = selectedRows;
setEnabled((this.selectedRows.length > 0));
}

public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "Voulez-vous vraiment confirmer votre choix ?",
"Confirmation de la suppression", JOptionPane.YES_NO_OPTION);
switch (result) {
case JOptionPane.YES_OPTION:
model.removePerson(selectedRows);
break;
case JOptionPane.NO_OPTION:
break;
}
}
}
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 13:00
Personne n'a une ptite idée ?
0
femeril1805 Messages postés 29 Date d'inscription jeudi 19 novembre 2009 Statut Membre Dernière intervention 20 janvier 2011
19 nov. 2009 à 13:41
allo...y a quelqu'un ??
0