Reputation: 171
I know its easy but I am freaking doing it for about 2 hours now and I can't seems figure out why I can't pass the JTextArea
value from a variable to the other java files because I separated my ActionEvent
code to another File from my Object (specifically the JTextArea
), guys find out what went wrong on my code.
actionlistener
code:
public class ButtonAction{
public static class AddInv implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton inv = (AbstractButton)e.getSource();
AddInventory addInv = new AddInventory();
if(inv.getActionCommand().equals("SAVE")){
invName = addInv.areaName.getText();
JOptionPane.showMessageDialog(null, invName);
}
}
}
}
Here's my Button and textarea object code from another java file, this is my class AddInventory
:
ActionListener add = new ButtonAction.AddInv();
areaName = new JTextArea(2, 35);
//my TextArea
JButton buttonSave = new JButton("SAVE");
buttonSave.addActionListener(add);
Guys can you try this code and tell me if it is working in your computer guys. because what I'm planning to do is save this text area value to my database.
I'm already connected the oracle database and I just need to insert some records.
Upvotes: 0
Views: 1250
Reputation: 347184
I'd start by examining the purpose of
addInv = new AddInventory();
s1 = addInv.areaName.getText();
To me, this says, create me a new AddInventory
and give me the default value of it's areaName
text field...which is probably nothing...
UPDATE
Still the same problem...
AddInventory addInv = new AddInventory();
if(inv.getActionCommand().equals("SAVE")){
invName = addInv.areaName.getText();
JOptionPane.showMessageDialog(null, invName);
}
Some how, you either need to pass the reference to the text area to the action...
UPDATE EXAMPLE
Ideally you want some kind of controller/model to take care of this, but as an example...
areaName = new JTextArea(2, 35);
ActionListener add = new ButtonAction.AddInv(areaName);
//my TextArea
JButton buttonSave = new JButton("SAVE");
buttonSave.addActionListener(add);
And you action class...
public class ButtonAction{
public static class AddInv implements ActionListener{
private JTextArea text;
public AddInv(JTextArea text) {
this.text = text;
}
public void actionPerformed(ActionEvent e){
AbstractButton inv = (AbstractButton)e.getSource();
if(inv.getActionCommand().equals("SAVE")){
invName = text.getText();
JOptionPane.showMessageDialog(null, invName);
}
}
}
}
Upvotes: 1