dadadude
asked on
Null Object EXCEPTION
i will give you an example of my class because it is big:
public class test{
private Data data;
public test()[
}
public void createData(){
data = new data();
setData(data)
getData().createTable();
}
public void removeRow(){
// THE EXCEPTION IS HAPPENING IN HERE GETDATA() IS GIVING ME A NULL POINTER WHY?
// AND IT IS ALREADY INITIALIZED!!!
JTabee table = getData().gettable();
}
public void setData(data){
this.data = data;
}
public void getData(){
return this.data;
}
public class test{
private Data data;
public test()[
}
public void createData(){
data = new data();
setData(data)
getData().createTable();
}
public void removeRow(){
// THE EXCEPTION IS HAPPENING IN HERE GETDATA() IS GIVING ME A NULL POINTER WHY?
// AND IT IS ALREADY INITIALIZED!!!
JTabee table = getData().gettable();
}
public void setData(data){
this.data = data;
}
public void getData(){
return this.data;
}
To what do you assign 'table' ? - i see nothing
ASKER
i am assigning table to JTable table = getData().getTable();
public class Data{
private JTable table;
//code...
public setTable(table){
this.table = table;
}
public getTable(){
return this.table;
}
}
So i am getting Table in class test and assining table in this class to table from class Data.
public class Data{
private JTable table;
//code...
public setTable(table){
this.table = table;
}
public getTable(){
return this.table;
}
}
So i am getting Table in class test and assining table in this class to table from class Data.
ASKER
the problem is that once
public void createData(){
data = new data();
setData(data)
getData().createTable();
}
GetData() becomes NULL; why? it is initialized in this function and i used setData
public void createData(){
data = new data();
setData(data)
getData().createTable();
}
GetData() becomes NULL; why? it is initialized in this function and i used setData
>>
data = new data();
setData(data)
getData().createTable();
>>
>>JTabee table = getData().gettable();
Is what you posted earlier - no table get create and assigned that i can see
data = new data();
setData(data)
getData().createTable();
>>
>>JTabee table = getData().gettable();
Is what you posted earlier - no table get create and assigned that i can see
ASKER
once i leave this method createData() getData becomes null when i use it in other methods.
ASKER
JTabee table = getData().gettable();
I want to Get JTable table from Class Data.
and assign Table in class test to table of class Data.
what is the problem why it isn't working?
I want to Get JTable table from Class Data.
and assign Table in class test to table of class Data.
what is the problem why it isn't working?
ASKER
The Problem is not with Table is with getData() it is becoming Null each time
ASKER
i will make it more simple:
in class test
i have private Data data;
in method BLABLABLA(){
data = new Data();
setData(data);
getData();
// in here getData() is not null
}
once it is used in another method it becomes null!!!!
in class test
i have private Data data;
in method BLABLABLA(){
data = new Data();
setData(data);
getData();
// in here getData() is not null
}
once it is used in another method it becomes null!!!!
ASKER
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep tion
where is your main() method? do you call createData() before you call removeRow()?
ASKER
i am using Listeners
i have a button at first i click on like Open.
it calls method CreateData().
so data shouldn't be null cos it is intilializes in this method createData() correct?
once i click on removeRow() method and call getData() from the same class test.
i receive this Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep tion
if i do system.out.print(getData() ) i have NULL on the screen the object becomes null!
i have a button at first i click on like Open.
it calls method CreateData().
so data shouldn't be null cos it is intilializes in this method createData() correct?
once i click on removeRow() method and call getData() from the same class test.
i receive this Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep
if i do system.out.print(getData()
can i see your actionPerformed() method?
>>once it is used in another method it becomes null!!!!
It will become null if you replace the original reference to 'data' with another, local, one and then try to read it again once you're out of the local context (method)
It will become null if you replace the original reference to 'data' with another, local, one and then try to read it again once you're out of the local context (method)
ASKER
this is the class:
public class MenuOperation extends JFrame implements ActionListener{
private MenuTabs tabs;
private File f;
private Data data;
/** Creates a new instance of MenuOperation */
public MenuOperation(){
}
public void actionPerformed(ActionEven t e) {
String actionCommande = e.getActionCommand();
if(actionCommande.equals(" Importer Fichier..." )){
ouvrir();
}else if(actionCommande.equals(" Supprimer Colonne")){
selectionnerColonne();
}
// IN THIS METHOD I DO SETDATA()
private void ouvrir(){
JFileChooser jfc = new JFileChooser();
Container c=InfoInterface.getDonnees ().getC();
jfc.setCurrentDirectory(ne w File("."));
jfc.addChoosableFileFilter (new FiltreExtension("xls", "Fichier EXCEL"));
jfc.addChoosableFileFilter (new FiltreExtension("xml", "Fichier XML"));
jfc.addChoosableFileFilter (new FiltreExtension("tree", "Fichier Regress"));
int resultat = jfc.showOpenDialog(this);
if(resultat == JFileChooser.CANCEL_OPTION ){
return;
}else if(resultat == JFileChooser.APPROVE_OPTIO N){
f = jfc.getSelectedFile();
String extension = Extension.getExtension(f);
cd = new ChargementDonnees(f);
setData(new Data());
getData().creerTable(cd);
cn = new ChargementNature(getData() );
setNature(new Nature());
getNature().creerTable(cn) ;
tabs = new MenuTabs();
tabs.ajoutTab(getNature(), "nature");
PanelPrincipale panel = new PanelPrincipale(getData(), tabs);
c.removeAll();
c.add(panel);
c.validate();
}
}
// in this method getData() becomes null
private void selectionnerColonne() {
supprimerColonne(getData() .getTable( ),
getData().getTable().getCo lumnModel( ).getSelec tionModel( ).getLeadS electionIn dex());
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
public class MenuOperation extends JFrame implements ActionListener{
private MenuTabs tabs;
private File f;
private Data data;
/** Creates a new instance of MenuOperation */
public MenuOperation(){
}
public void actionPerformed(ActionEven
String actionCommande = e.getActionCommand();
if(actionCommande.equals("
ouvrir();
}else if(actionCommande.equals("
selectionnerColonne();
}
// IN THIS METHOD I DO SETDATA()
private void ouvrir(){
JFileChooser jfc = new JFileChooser();
Container c=InfoInterface.getDonnees
jfc.setCurrentDirectory(ne
jfc.addChoosableFileFilter
jfc.addChoosableFileFilter
jfc.addChoosableFileFilter
int resultat = jfc.showOpenDialog(this);
if(resultat == JFileChooser.CANCEL_OPTION
return;
}else if(resultat == JFileChooser.APPROVE_OPTIO
f = jfc.getSelectedFile();
String extension = Extension.getExtension(f);
cd = new ChargementDonnees(f);
setData(new Data());
getData().creerTable(cd);
cn = new ChargementNature(getData()
setNature(new Nature());
getNature().creerTable(cn)
tabs = new MenuTabs();
tabs.ajoutTab(getNature(),
PanelPrincipale panel = new PanelPrincipale(getData(),
c.removeAll();
c.add(panel);
c.validate();
}
}
// in this method getData() becomes null
private void selectionnerColonne() {
supprimerColonne(getData()
getData().getTable().getCo
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
ASKER
what should i do then if it becomes null outside i need to use it in all the methods
ASKER
data should be used in all the methods. i can't each time create a new one.
ASKER
can i use inheritence? is it possible to make it extends Data?
where is your object of class test? you to call setData() and getData from the same test object.
ASKER
ignore the last message.
YEs setData and getData are called from the same class test
YEs setData and getData are called from the same class test
they're in the same class, but i don't see a common object of that class that is calling both methods
ASKER
what do you mean? i don't understand you.
ASKER
setData(new Data());
it is initialized
in method createTable();
and in method supprimerColonne(){
// getData() becomes null
}
it is initialized
in method createTable();
and in method supprimerColonne(){
// getData() becomes null
}
ASKER
createTable(){
setData(new Data());
getData().creerTable(cd);
}
no problem in method createTable();
in the other methods it becomes null what should i do?
setData(new Data());
getData().creerTable(cd);
}
no problem in method createTable();
in the other methods it becomes null what should i do?
I mean that you should something like:
test t = new test();
... some code
t.setData(new Data());
... some code
JTable table = t.getData().gettable();
test t = new test();
... some code
t.setData(new Data());
... some code
JTable table = t.getData().gettable();
In the stack trace of the exception, you should see a line number. Please point to that line in the code
ASKER
Imagine this class:
class test{
private Data data;
public void initData(){
setData(new Data());
getData(); // fine in here
}
public void blabla(){
getData() = null;// why?
}
setData(){
bla bla}
getData{return data}
class test{
private Data data;
public void initData(){
setData(new Data());
getData(); // fine in here
}
public void blabla(){
getData() = null;// why?
}
setData(){
bla bla}
getData{return data}
ASKER
in class test i must create object test?
ASKER
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep tion
at Utilites.MenuOperation.sel ectionnerC olonne(Men uOperation .java:159)
at Utilites.MenuOperation.act ionPerform ed(MenuOpe ration.jav a:58)
i didn't give you the whole Code it is so big.
at Utilites.MenuOperation.sel
at Utilites.MenuOperation.act
i didn't give you the whole Code it is so big.
ASKER
setData and getData are in class Test is it possible to create object test in class test?
no, but you're using the methods in another class, right? so you must have a test object there.
ASKER
no no both setData and getData are in class test.
so i used getData() to get method getTable() from class Data
Both Methods are in the same class Test.
Look at that example please
class test{
private Data data;
public void initData(){
setData(new Data());
getData(); // fine in here
}
public void blabla(){
getData() = null;// why?
}
setData(){
bla bla}
getData{return data}
so i used getData() to get method getTable() from class Data
Both Methods are in the same class Test.
Look at that example please
class test{
private Data data;
public void initData(){
setData(new Data());
getData(); // fine in here
}
public void blabla(){
getData() = null;// why?
}
setData(){
bla bla}
getData{return data}
In selectionnerColonne, split those references up. You call the same method repeatedly unnecessarily anyway. Start with
Data data = getData(); //test this
JTable table = data.getTable(); // and this
Data data = getData(); //test this
JTable table = data.getTable(); // and this
never mind about what i was saying; i didn't notice that you also had getData() and setData() methods in class MenuOperation.
put a System.out.println() statement after this if statement in your code.
else if(resultat == JFileChooser.APPROVE_OPTIO N){
make sure that that if is evaluating true
put a System.out.println() statement after this if statement in your code.
else if(resultat == JFileChooser.APPROVE_OPTIO
make sure that that if is evaluating true
ASKER
private void selectionnerColonne() {
Data data = getData(); //test this
JTable table = data.getTable(); // and this
didn't work i am always getting an exception NullExceptionPointer
Data data = getData(); //test this
JTable table = data.getTable(); // and this
didn't work i am always getting an exception NullExceptionPointer
ASKER
else if(resultat == JFileChooser.APPROVE_OPTIO N){
f = jfc.getSelectedFile();
String extension = Extension.getExtension(f);
cd = new ChargementDonnees(f);
setData(new Data());
getData().creerTable(cd);
cn = new ChargementNature(getData() );
setNature(new Nature());
getNature().creerTable(cn) ;
tabs = new MenuTabs();
tabs.ajoutTab(getNature(), "nature");
PanelPrincipale panel = new PanelPrincipale(getData(), tabs);
c.removeAll();
c.add(panel);
c.validate();
System.out.println("test") ;
}
it worked i have test on the screen
f = jfc.getSelectedFile();
String extension = Extension.getExtension(f);
cd = new ChargementDonnees(f);
setData(new Data());
getData().creerTable(cd);
cn = new ChargementNature(getData()
setNature(new Nature());
getNature().creerTable(cn)
tabs = new MenuTabs();
tabs.ajoutTab(getNature(),
PanelPrincipale panel = new PanelPrincipale(getData(),
c.removeAll();
c.add(panel);
c.validate();
System.out.println("test")
}
it worked i have test on the screen
ASKER
data is in the class and it is initialized what is the problem? mmm weird
try changing your setData() method as follows
public void setData(Data d) {
data = d;
}
public void setData(Data d) {
data = d;
}
ASKER
i did but it is the same anyway it wont change.
But is it possible that an object becomes null after it has been created?
But is it possible that an object becomes null after it has been created?
>>didn't work i am always getting an exception NullExceptionPointer
It's not intended as a solution - it's to isolate the problem. Which object is null?
It's not intended as a solution - it's to isolate the problem. Which object is null?
also, can i see how you add your ActionListeners?
ASKER
JTable table = data.getTable(); // and this
in here data is null;
in here data is null;
ASKER
well they are called from another class
where i do like that:
private MenuOperation menuOperation = new MenuOperation();
menuItem.addActionListener (menuOpera tion);
where i do like that:
private MenuOperation menuOperation = new MenuOperation();
menuItem.addActionListener
ASKER
i don't think that it is the problem.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
looks more likely that your problem is is in your Data class, can u post that.
check that it contains a correct table instance.
You may be creating a new Data instance and not setting its table
check that it contains a correct table instance.
You may be creating a new Data instance and not setting its table
ASKER
ok
public class Data extends JPanel{
private JTable table;
private TableModel model;
private static boolean availabe;
private ChargementDonnees donnees;
/**
* Copy constructeur
* @param _donnees Donnees est un parametre envoyer par la classe ChargementDonnees
* qui prend les donnees d'un fichier Excel.
*/
public Data(){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
}
public void creerTable(ChargementDonne es donnees){
setAvailabe(true);
this.setDonnees(donnees);
setTable(new JTable(getDonnees()));
table.setRowSelectionAllow ed(false);
table.setColumnSelectionAl lowed(fals e);
TableColumnModel tcm = table.getColumnModel();
JScrollPane jsp = new JScrollPane(getTable());
JViewport jvp = new JViewport();
jvp.setView(new RowNumberHeader(getTable() ));
jsp.setRowHeader(jvp);
add(jsp,BorderLayout.CENTE R);
}
/**
* retourne un objet JTable
* @return retourne table
*/
public JTable getTable() {
return table;
}
/**
* ajouter un objet Table
* @param aTable pour ajouter une table
*/
public static boolean isAvailabe() {
return availabe;
}
public static void setAvailabe(boolean aAvailabe) {
availabe = aAvailabe;
}
public ChargementDonnees getDonnees() {
return donnees;
}
public void setDonnees(ChargementDonne es donnees) {
this.donnees = donnees;
}
public void setTable(JTable table) {
this.table = table;
}
}
public class Data extends JPanel{
private JTable table;
private TableModel model;
private static boolean availabe;
private ChargementDonnees donnees;
/**
* Copy constructeur
* @param _donnees Donnees est un parametre envoyer par la classe ChargementDonnees
* qui prend les donnees d'un fichier Excel.
*/
public Data(){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
}
public void creerTable(ChargementDonne
setAvailabe(true);
this.setDonnees(donnees);
setTable(new JTable(getDonnees()));
table.setRowSelectionAllow
table.setColumnSelectionAl
TableColumnModel tcm = table.getColumnModel();
JScrollPane jsp = new JScrollPane(getTable());
JViewport jvp = new JViewport();
jvp.setView(new RowNumberHeader(getTable()
jsp.setRowHeader(jvp);
add(jsp,BorderLayout.CENTE
}
/**
* retourne un objet JTable
* @return retourne table
*/
public JTable getTable() {
return table;
}
/**
* ajouter un objet Table
* @param aTable pour ajouter une table
*/
public static boolean isAvailabe() {
return availabe;
}
public static void setAvailabe(boolean aAvailabe) {
availabe = aAvailabe;
}
public ChargementDonnees getDonnees() {
return donnees;
}
public void setDonnees(ChargementDonne
this.donnees = donnees;
}
public void setTable(JTable table) {
this.table = table;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes i always pass the same object.
ASKER
Well because class MenuOperation is supposed to handle the actions. this is why
> getData().getTable().getCo lumnModel( ).getSelec tionModel( ).getLeadS electionIn dex());
u could also possible have no selection, try the following so you can see whats happening
System.out.println("data=" +getData() );
System.out.println("table= "getData() .getTable( ));
System.out.println("column s="+getDat a().getTab le().getCo lumnModel( ));
System.out.println("select ion="+getD ata().getT able().get ColumnMode l().getSel ectionMode l);
getData().getTable().getCo lumnModel( ).getSelec tionModel( ).getLeadS electionIn dex());
u could also possible have no selection, try the following so you can see whats happening
System.out.println("data="
System.out.println("table=
System.out.println("column
System.out.println("select
getData().getTable().getCo
can you post the whole stack trace?
System.out.println("data=" +getData() );
System.out.println("table= "getData() .getTable( ));
System.out.println("column s="+getDat a().getTab le().getCo lumnModel( ));
System.out.println("select ion="+getD ata().getT able().get ColumnMode l().getSel ectionMode l);
supprimerColonne(getData() .getTable( ),
getData().getTable().getCo lumnModel( ).getSelec tionModel( ).getLeadS electionIn dex());
System.out.println("table=
System.out.println("column
System.out.println("select
supprimerColonne(getData()
getData().getTable().getCo
ASKER
private void selectionnerColonne() {
System.out.println("data=" +getData() );
System.out.println("table= "+getData( ).getTable ());
System.out.println("column s="+getDat a().getTab le().getCo lumnModel( ));
System.out.println("select ion="+getD ata().getT able().get ColumnMode l().getSel ectionMode l());
Data data = this.getData();
JTable table = data.getTable();
supprimerColonne(table,
table.getColumnModel().get SelectionM odel().get LeadSelect ionIndex() );
}
stackTrace:
compile:
data=null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep tion
at Utilites.MenuOperation.sel ectionnerC olonne(Men uOperation .java:161)
at Utilites.MenuOperation.act ionPerform ed(MenuOpe ration.jav a:58)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :1849)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Abstr actButton. java:2169)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del.java:4 20)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 258)
at javax.swing.AbstractButton .doClick(A bstractBut ton.java:3 02)
at javax.swing.plaf.basic.Bas icMenuItem UI.doClick (BasicMenu ItemUI.jav a:1000)
at javax.swing.plaf.basic.Bas icMenuItem UI$Handler .mouseRele ased(Basic MenuItemUI .java:1041 )
at java.awt.Component.process MouseEvent (Component .java:5488 )
at javax.swing.JComponent.pro cessMouseE vent(JComp onent.java :3126)
at java.awt.Component.process Event(Comp onent.java :5253)
at java.awt.Container.process Event(Cont ainer.java :1966)
at java.awt.Component.dispatc hEventImpl (Component .java:3955 )
at java.awt.Container.dispatc hEventImpl (Container .java:2024 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3803)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:4 212)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:38 92)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3822)
at java.awt.Container.dispatc hEventImpl (Container .java:2010 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 1778)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3803)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:463)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hThread.ja va:242)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hread.java :163)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 57)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 49)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:110)
debug:
BUILD SUCCESSFUL (total time: 12 seconds)
System.out.println("data="
System.out.println("table=
System.out.println("column
System.out.println("select
Data data = this.getData();
JTable table = data.getTable();
supprimerColonne(table,
table.getColumnModel().get
}
stackTrace:
compile:
data=null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExcep
at Utilites.MenuOperation.sel
at Utilites.MenuOperation.act
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
at javax.swing.AbstractButton
at javax.swing.plaf.basic.Bas
at javax.swing.plaf.basic.Bas
at java.awt.Component.process
at javax.swing.JComponent.pro
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
debug:
BUILD SUCCESSFUL (total time: 12 seconds)
ASKER
it is not passing data
And are you sure ouvrir() gets called *before* actionPerformed()
sorry meaqnt, before selectionnerColonne()
ie. that the chooser action runs first
ie. that the chooser action runs first
Make sure the reference to a valid instance is not getting lost with your gui when you remove stuff from it
ASKER
well yes first i call ouvrir then i call selectionnerColonne()
it is possible that the error is in the design?
it is possible that the error is in the design?
> data=null
that suggestes your ouvrir() has not got executed beforehand as I mentioned above
to safeguard against that use:
if (getData()!=null) {
supprimerColonne(getData() .getTable( ),
getData().getTable().getCo lumnModel( ).getSelec tionModel( ).getLeadS electionIn dex());
}
that suggestes your ouvrir() has not got executed beforehand as I mentioned above
to safeguard against that use:
if (getData()!=null) {
supprimerColonne(getData()
getData().getTable().getCo
}
ASKER
but i mean when i call ouvrir() get the table in front of me
do you think that inserrercolonne() is being executed before it?
do you think that inserrercolonne() is being executed before it?
Add the follwoing to check its getting called:
System.out.println("Openin g dialog");
int resultat = jfc.showOpenDialog(this);
System.out.println("ooptio n="+result at);
if(resultat == JFileChooser.CANCEL_OPTION ){
return;
}else if(resultat == JFileChooser.APPROVE_OPTIO N){
System.out.println("APPROV ED");
...
System.out.println("Openin
int resultat = jfc.showOpenDialog(this);
System.out.println("ooptio
if(resultat == JFileChooser.CANCEL_OPTION
return;
}else if(resultat == JFileChooser.APPROVE_OPTIO
System.out.println("APPROV
...
> do you think that inserrercolonne() is being executed before it?
what you have posted suggests that,
*or* you are creating two instances on MenuOperation.
what you have posted suggests that,
*or* you are creating two instances on MenuOperation.
ASKER
compile:
Opening dialog
ooption=0
APPROVED
Opening dialog
ooption=0
APPROVED
ASKER
option = 0
if result = 0 is that a problem?
if result = 0 is that a problem?
it can't be, because it printed "APPROVED"
ASKER
i think that i will have to design the application again.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is there a way to post my application so that you can check it?
post the whole code
post the whole code
can you post your gui class?
ASKER
yes well i will explain it. i created ItemMenu and MenuOutils to be able to create Menu items
dynamically i think that this is the problem.
// This is the main class
public class Donnees extends JFrame {
// private JFrame frame = new JFrame();
private JMenuBar menuBar;
private Container c;
// Constructeur de la classe Donnees
public Donnees(){
super("Regression:Menu Principal");
setC(getContentPane());
}
// Methode pour initialize la fenetre principale
public void initialization(){
// Appelle WindowListener pour fermer la Fenetre
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
// Creation de la barre Menu
menuBar = new JMenuBar();
// Menu Principale
MenuOutils m;
ItemMenu mi;
//Menu Fichier
m = new MenuOutils("Fichier",'f');
menuBar.add(m.initialize() );
mi = new ItemMenu("Importer Fichier...");
m.ajouter(mi.initializer() );
m.Separateur();
mi = new ItemMenu("Enregistrer sous...");
m.ajouter(mi.initializer() );
m.Separateur();
mi = new ItemMenu("Nouveau...");
m.ajouter(mi.initializer() );
mi = new ItemMenu("Quitter",'q',Key Stroke.get KeyStroke(
KeyEvent.VK_Q,ActionEvent. CTRL_MASK
));
m.ajouter(mi.initializer() );
// Menu Edition
m = new MenuOutils("Edition",'e');
menuBar.add(m.initialize() );
mi = new ItemMenu("Inserrer Colonne");
m.ajouter(mi.initializer() );
mi = new ItemMenu("Supprimer Colonne");
m.ajouter(mi.initializer() );
m.Separateur();
mi= new ItemMenu("Inserrer Ligne");
m.ajouter(mi.initializer() );
mi = new ItemMenu("Supprimer Ligne");
m.ajouter(mi.initializer() );
m.Separateur();
mi = new ItemMenu("Copier",'o',KeyS troke.getK eyStroke(
KeyEvent.VK_C,ActionEvent. CTRL_MASK
));
m.ajouter(mi.initializer() );
mi = new ItemMenu("Couper",'c',KeyS troke.getK eyStroke(
KeyEvent.VK_X,ActionEvent. CTRL_MASK
));
m.ajouter(mi.initializer() );
mi = new ItemMenu("Coller",'o',KeyS troke.getK eyStroke(
KeyEvent.VK_V,ActionEvent. CTRL_MASK
));
m.ajouter(mi.initializer() );
// Menu Analyse
m = new MenuOutils("Analyse",'a');
menuBar.add(m.initialize() );
mi = new ItemMenu("Stat Description");
m.ajouter(mi.initializer() );
m.Separateur();
mi = new ItemMenu("Methode 1");
m.ajouter(mi.initializer() );
mi = new ItemMenu("Methode 2");
m.ajouter(mi.initializer() );
// Menu Aide
m = new MenuOutils("?",'?');
menuBar.add(m.initialize() );
mi = new ItemMenu("Version");
m.ajouter(mi.initializer() );
m.Separateur();
mi = new ItemMenu("Aide");
m.ajouter(mi.initializer() );
setJMenuBar(menuBar);
}
// Methode pour quitter l'application
private void exitForm(WindowEvent e) {
int reponse = JOptionPane.showConfirmDia log(null,
"Voulez vous quitter?", "quitter", JOptionPane.YES_NO_OPTION) ;
if(reponse== JOptionPane.YES_OPTION) {
dispose();
System.exit(0);
}else{
return;
}
}
public Container getC() {
return c;
}
public void setC(Container c) {
this.c = c;
}
}
and this is where i assign an action listener:
public class ItemMenu extends JMenuItem {
private String nom;
private int nemonic;
private KeyStroke key;
private JMenuItem menuItem;
private MenuOperation menuOperation = new MenuOperation();
/**
* Creates a new instance of ItemMenu
*/
public ItemMenu(String _nom,int _nemonic,KeyStroke _key) {
this.setNom(_nom);
this.setNemonic(_nemonic);
this.setKey(_key);
}
public ItemMenu(String _nom) {
this.setNom(_nom);
}
public JMenuItem initializer(){
menuItem = new JMenuItem(getNom());
menuItem.setMnemonic(Integ er.valueOf (getNemoni c()));
menuItem.setAccelerator(ge tKey());
menuItem.addActionListener (menuOpera tion);
return menuItem;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public int getNemonic() {
return nemonic;
}
public void setNemonic(int nemonic) {
this.nemonic = nemonic;
}
public KeyStroke getKey() {
return key;
}
public void setKey(KeyStroke key) {
this.key = key;
}
}
so i created an object in class Donnees like mi and give it an actionListener
dynamically i think that this is the problem.
// This is the main class
public class Donnees extends JFrame {
// private JFrame frame = new JFrame();
private JMenuBar menuBar;
private Container c;
// Constructeur de la classe Donnees
public Donnees(){
super("Regression:Menu Principal");
setC(getContentPane());
}
// Methode pour initialize la fenetre principale
public void initialization(){
// Appelle WindowListener pour fermer la Fenetre
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
// Creation de la barre Menu
menuBar = new JMenuBar();
// Menu Principale
MenuOutils m;
ItemMenu mi;
//Menu Fichier
m = new MenuOutils("Fichier",'f');
menuBar.add(m.initialize()
mi = new ItemMenu("Importer Fichier...");
m.ajouter(mi.initializer()
m.Separateur();
mi = new ItemMenu("Enregistrer sous...");
m.ajouter(mi.initializer()
m.Separateur();
mi = new ItemMenu("Nouveau...");
m.ajouter(mi.initializer()
mi = new ItemMenu("Quitter",'q',Key
KeyEvent.VK_Q,ActionEvent.
));
m.ajouter(mi.initializer()
// Menu Edition
m = new MenuOutils("Edition",'e');
menuBar.add(m.initialize()
mi = new ItemMenu("Inserrer Colonne");
m.ajouter(mi.initializer()
mi = new ItemMenu("Supprimer Colonne");
m.ajouter(mi.initializer()
m.Separateur();
mi= new ItemMenu("Inserrer Ligne");
m.ajouter(mi.initializer()
mi = new ItemMenu("Supprimer Ligne");
m.ajouter(mi.initializer()
m.Separateur();
mi = new ItemMenu("Copier",'o',KeyS
KeyEvent.VK_C,ActionEvent.
));
m.ajouter(mi.initializer()
mi = new ItemMenu("Couper",'c',KeyS
KeyEvent.VK_X,ActionEvent.
));
m.ajouter(mi.initializer()
mi = new ItemMenu("Coller",'o',KeyS
KeyEvent.VK_V,ActionEvent.
));
m.ajouter(mi.initializer()
// Menu Analyse
m = new MenuOutils("Analyse",'a');
menuBar.add(m.initialize()
mi = new ItemMenu("Stat Description");
m.ajouter(mi.initializer()
m.Separateur();
mi = new ItemMenu("Methode 1");
m.ajouter(mi.initializer()
mi = new ItemMenu("Methode 2");
m.ajouter(mi.initializer()
// Menu Aide
m = new MenuOutils("?",'?');
menuBar.add(m.initialize()
mi = new ItemMenu("Version");
m.ajouter(mi.initializer()
m.Separateur();
mi = new ItemMenu("Aide");
m.ajouter(mi.initializer()
setJMenuBar(menuBar);
}
// Methode pour quitter l'application
private void exitForm(WindowEvent e) {
int reponse = JOptionPane.showConfirmDia
"Voulez vous quitter?", "quitter", JOptionPane.YES_NO_OPTION)
if(reponse== JOptionPane.YES_OPTION) {
dispose();
System.exit(0);
}else{
return;
}
}
public Container getC() {
return c;
}
public void setC(Container c) {
this.c = c;
}
}
and this is where i assign an action listener:
public class ItemMenu extends JMenuItem {
private String nom;
private int nemonic;
private KeyStroke key;
private JMenuItem menuItem;
private MenuOperation menuOperation = new MenuOperation();
/**
* Creates a new instance of ItemMenu
*/
public ItemMenu(String _nom,int _nemonic,KeyStroke _key) {
this.setNom(_nom);
this.setNemonic(_nemonic);
this.setKey(_key);
}
public ItemMenu(String _nom) {
this.setNom(_nom);
}
public JMenuItem initializer(){
menuItem = new JMenuItem(getNom());
menuItem.setMnemonic(Integ
menuItem.setAccelerator(ge
menuItem.addActionListener
return menuItem;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public int getNemonic() {
return nemonic;
}
public void setNemonic(int nemonic) {
this.nemonic = nemonic;
}
public KeyStroke getKey() {
return key;
}
public void setKey(KeyStroke key) {
this.key = key;
}
}
so i created an object in class Donnees like mi and give it an actionListener
ASKER
i wanted menu items to be added dynamically. it is a nice way but you are right the problem is there i think.
you are create multiple instance of MenuOperation, that's the problem
ASKER
please can you explain more where do you mean i am creating them?
please. i am lost
please. i am lost
move these statements out into your gui class:
MenuOperation menuoperation = new MenuOperation();
menuitem.addActionListener (menuopera tion);
MenuOperation menuoperation = new MenuOperation();
menuitem.addActionListener
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok i will try it. it is late now it is 1:30 AM working all day.
it is possible to post tomorrow what i tried ?
so that you can comment please?
it is possible to post tomorrow what i tried ?
so that you can comment please?
yes, of course.
ASKER
IT WORKED!!!!!!!!!!!!!!!! YOU ARE GREAT PEOPLE THANK YOU SOO MUCH!!!!!!!!!!
ASKER
thank you all!!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mm guess that you all deserve gee mmmmmmmmmmmm
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
don't know how to split the points you all deserve 500