Swing get JTextField upon JButton press












0















Apparently my Google-fu skills are bit lacklustre and I can't figure out how to get JTextField when pressing a JButton.



Please note that I've removed some parts of the code for ease of reading.



If you see some variable that's not defined assume that it was part of that code.



As it stands, the code works fine.



public final class Main {
// Some removed code was here
private void prepareGUI() {

// Top right stuff
JPanel topRightPanel = new JPanel();
topRightPanel.setLayout(new FlowLayout());
JLabel topRightLabel = new JLabel("Address");
JTextField topRightTextField = new JTextField("", 15);
topRightTextField.setName("add_address");
JButton topRightButton = new JButton("Add");
topRightButton.setName("add_btn");

topRightPanel.add(topRightLabel);
topRightPanel.add(topRightTextField);
topRightPanel.add(topRightButton);
mainFrame.add(topRightPanel);

// The button in question. Very suggestive name, I know.
topRightButton.addActionListener(new GenericButtonListener());

genericButtonListener.setKernel(kernel);

// some other non relevant stuff here

mainFrame.setVisible(true);
}

}




public class GenericButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String btnName = btn.getName();

if(btnName.toLowerCase().contains("add_btn")) {
addBtn(btn);
}
}

public void addBtn(JButton button){
SshFileIO sshFileIO = kernel.getFileIO();
// Get field text here
}
}


My current dilemma is how to get said textfield value inside GenericButtonListener.



I realize that I can use getText to get the text field value, however I don't have access to that variable inside the actionPerformed function.



I suppose this is more of a scoping problem rather than anything else.



I just need some pointing in the right direction, no hand holding required.



It's painfully obvious that I'm very new to Java.










share|improve this question























  • What IDE and tools you are using for development?

    – Laxminarayan
    Nov 26 '18 at 8:09











  • Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

    – Arnaud
    Nov 26 '18 at 8:09











  • @Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

    – Andrei
    Nov 26 '18 at 8:12











  • @Arnaud Yes, that's the one.

    – Andrei
    Nov 26 '18 at 8:12











  • @Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

    – Laxminarayan
    Nov 26 '18 at 8:13


















0















Apparently my Google-fu skills are bit lacklustre and I can't figure out how to get JTextField when pressing a JButton.



Please note that I've removed some parts of the code for ease of reading.



If you see some variable that's not defined assume that it was part of that code.



As it stands, the code works fine.



public final class Main {
// Some removed code was here
private void prepareGUI() {

// Top right stuff
JPanel topRightPanel = new JPanel();
topRightPanel.setLayout(new FlowLayout());
JLabel topRightLabel = new JLabel("Address");
JTextField topRightTextField = new JTextField("", 15);
topRightTextField.setName("add_address");
JButton topRightButton = new JButton("Add");
topRightButton.setName("add_btn");

topRightPanel.add(topRightLabel);
topRightPanel.add(topRightTextField);
topRightPanel.add(topRightButton);
mainFrame.add(topRightPanel);

// The button in question. Very suggestive name, I know.
topRightButton.addActionListener(new GenericButtonListener());

genericButtonListener.setKernel(kernel);

// some other non relevant stuff here

mainFrame.setVisible(true);
}

}




public class GenericButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String btnName = btn.getName();

if(btnName.toLowerCase().contains("add_btn")) {
addBtn(btn);
}
}

public void addBtn(JButton button){
SshFileIO sshFileIO = kernel.getFileIO();
// Get field text here
}
}


My current dilemma is how to get said textfield value inside GenericButtonListener.



I realize that I can use getText to get the text field value, however I don't have access to that variable inside the actionPerformed function.



I suppose this is more of a scoping problem rather than anything else.



I just need some pointing in the right direction, no hand holding required.



It's painfully obvious that I'm very new to Java.










share|improve this question























  • What IDE and tools you are using for development?

    – Laxminarayan
    Nov 26 '18 at 8:09











  • Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

    – Arnaud
    Nov 26 '18 at 8:09











  • @Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

    – Andrei
    Nov 26 '18 at 8:12











  • @Arnaud Yes, that's the one.

    – Andrei
    Nov 26 '18 at 8:12











  • @Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

    – Laxminarayan
    Nov 26 '18 at 8:13
















0












0








0








Apparently my Google-fu skills are bit lacklustre and I can't figure out how to get JTextField when pressing a JButton.



Please note that I've removed some parts of the code for ease of reading.



If you see some variable that's not defined assume that it was part of that code.



As it stands, the code works fine.



public final class Main {
// Some removed code was here
private void prepareGUI() {

// Top right stuff
JPanel topRightPanel = new JPanel();
topRightPanel.setLayout(new FlowLayout());
JLabel topRightLabel = new JLabel("Address");
JTextField topRightTextField = new JTextField("", 15);
topRightTextField.setName("add_address");
JButton topRightButton = new JButton("Add");
topRightButton.setName("add_btn");

topRightPanel.add(topRightLabel);
topRightPanel.add(topRightTextField);
topRightPanel.add(topRightButton);
mainFrame.add(topRightPanel);

// The button in question. Very suggestive name, I know.
topRightButton.addActionListener(new GenericButtonListener());

genericButtonListener.setKernel(kernel);

// some other non relevant stuff here

mainFrame.setVisible(true);
}

}




public class GenericButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String btnName = btn.getName();

if(btnName.toLowerCase().contains("add_btn")) {
addBtn(btn);
}
}

public void addBtn(JButton button){
SshFileIO sshFileIO = kernel.getFileIO();
// Get field text here
}
}


My current dilemma is how to get said textfield value inside GenericButtonListener.



I realize that I can use getText to get the text field value, however I don't have access to that variable inside the actionPerformed function.



I suppose this is more of a scoping problem rather than anything else.



I just need some pointing in the right direction, no hand holding required.



It's painfully obvious that I'm very new to Java.










share|improve this question














Apparently my Google-fu skills are bit lacklustre and I can't figure out how to get JTextField when pressing a JButton.



Please note that I've removed some parts of the code for ease of reading.



If you see some variable that's not defined assume that it was part of that code.



As it stands, the code works fine.



public final class Main {
// Some removed code was here
private void prepareGUI() {

// Top right stuff
JPanel topRightPanel = new JPanel();
topRightPanel.setLayout(new FlowLayout());
JLabel topRightLabel = new JLabel("Address");
JTextField topRightTextField = new JTextField("", 15);
topRightTextField.setName("add_address");
JButton topRightButton = new JButton("Add");
topRightButton.setName("add_btn");

topRightPanel.add(topRightLabel);
topRightPanel.add(topRightTextField);
topRightPanel.add(topRightButton);
mainFrame.add(topRightPanel);

// The button in question. Very suggestive name, I know.
topRightButton.addActionListener(new GenericButtonListener());

genericButtonListener.setKernel(kernel);

// some other non relevant stuff here

mainFrame.setVisible(true);
}

}




public class GenericButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String btnName = btn.getName();

if(btnName.toLowerCase().contains("add_btn")) {
addBtn(btn);
}
}

public void addBtn(JButton button){
SshFileIO sshFileIO = kernel.getFileIO();
// Get field text here
}
}


My current dilemma is how to get said textfield value inside GenericButtonListener.



I realize that I can use getText to get the text field value, however I don't have access to that variable inside the actionPerformed function.



I suppose this is more of a scoping problem rather than anything else.



I just need some pointing in the right direction, no hand holding required.



It's painfully obvious that I'm very new to Java.







java swing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 8:05









AndreiAndrei

2,03031223




2,03031223













  • What IDE and tools you are using for development?

    – Laxminarayan
    Nov 26 '18 at 8:09











  • Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

    – Arnaud
    Nov 26 '18 at 8:09











  • @Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

    – Andrei
    Nov 26 '18 at 8:12











  • @Arnaud Yes, that's the one.

    – Andrei
    Nov 26 '18 at 8:12











  • @Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

    – Laxminarayan
    Nov 26 '18 at 8:13





















  • What IDE and tools you are using for development?

    – Laxminarayan
    Nov 26 '18 at 8:09











  • Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

    – Arnaud
    Nov 26 '18 at 8:09











  • @Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

    – Andrei
    Nov 26 '18 at 8:12











  • @Arnaud Yes, that's the one.

    – Andrei
    Nov 26 '18 at 8:12











  • @Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

    – Laxminarayan
    Nov 26 '18 at 8:13



















What IDE and tools you are using for development?

– Laxminarayan
Nov 26 '18 at 8:09





What IDE and tools you are using for development?

– Laxminarayan
Nov 26 '18 at 8:09













Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

– Arnaud
Nov 26 '18 at 8:09





Did you mean to do topRightButton.addActionListener(genericButtonListener) ?

– Arnaud
Nov 26 '18 at 8:09













@Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

– Andrei
Nov 26 '18 at 8:12





@Laxminarayan Intellij Idea CE as in IDE and as the tools go, just the ones that come with the IDE, haven't gotten too far into setting up any sort of decent environment.

– Andrei
Nov 26 '18 at 8:12













@Arnaud Yes, that's the one.

– Andrei
Nov 26 '18 at 8:12





@Arnaud Yes, that's the one.

– Andrei
Nov 26 '18 at 8:12













@Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

– Laxminarayan
Nov 26 '18 at 8:13







@Andrei, if you use NetBeans it will give you more flexibility and to code it right. and btnName.getText() is a right way to get text field value. Maybe you are not getting the reference to the textField. Make it class level so you will get anywhere in the class.

– Laxminarayan
Nov 26 '18 at 8:13














1 Answer
1






active

oldest

votes


















1














Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. Store as property of the class and use it inside actionPerformed.



Change this one:



 topRightButton.addActionListener(new GenericButtonListener());


To this:



 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));


And inside class GenericButtonListener add field:



private JTextField topRightTextField;// set it in the constructor


And then use it inside of your method actionPerformed.



Have a nice coding and good luck!






share|improve this answer


























  • Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

    – Andrei
    Nov 26 '18 at 8:23












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476871%2fswing-get-jtextfield-upon-jbutton-press%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. Store as property of the class and use it inside actionPerformed.



Change this one:



 topRightButton.addActionListener(new GenericButtonListener());


To this:



 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));


And inside class GenericButtonListener add field:



private JTextField topRightTextField;// set it in the constructor


And then use it inside of your method actionPerformed.



Have a nice coding and good luck!






share|improve this answer


























  • Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

    – Andrei
    Nov 26 '18 at 8:23
















1














Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. Store as property of the class and use it inside actionPerformed.



Change this one:



 topRightButton.addActionListener(new GenericButtonListener());


To this:



 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));


And inside class GenericButtonListener add field:



private JTextField topRightTextField;// set it in the constructor


And then use it inside of your method actionPerformed.



Have a nice coding and good luck!






share|improve this answer


























  • Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

    – Andrei
    Nov 26 '18 at 8:23














1












1








1







Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. Store as property of the class and use it inside actionPerformed.



Change this one:



 topRightButton.addActionListener(new GenericButtonListener());


To this:



 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));


And inside class GenericButtonListener add field:



private JTextField topRightTextField;// set it in the constructor


And then use it inside of your method actionPerformed.



Have a nice coding and good luck!






share|improve this answer















Please try to get a reference of topRightTextField with the constructor of GenericButtonListener. Store as property of the class and use it inside actionPerformed.



Change this one:



 topRightButton.addActionListener(new GenericButtonListener());


To this:



 topRightButton.addActionListener(new GenericButtonListener(topRightTextField));


And inside class GenericButtonListener add field:



private JTextField topRightTextField;// set it in the constructor


And then use it inside of your method actionPerformed.



Have a nice coding and good luck!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 8:17

























answered Nov 26 '18 at 8:12









PulszarPulszar

49929




49929













  • Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

    – Andrei
    Nov 26 '18 at 8:23



















  • Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

    – Andrei
    Nov 26 '18 at 8:23

















Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

– Andrei
Nov 26 '18 at 8:23





Thanks for the answer. I ended up using a container class with a hashmap for the various components. Same idea, just different execution, I guess I'll just accept your answer.

– Andrei
Nov 26 '18 at 8:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476871%2fswing-get-jtextfield-upon-jbutton-press%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft