67367

Java Swing - How to get the value of text in a TextField from another class

Question:

Okay, beginner Java coder here. I'm trying to make a multi-purpose math utility application with Java Swing. One of the things I want it to do is be able to solve basic logarithms. I have the logic all down, but I'm having trouble with the outputting itself. I have a class (named "LogTab") and within it is a nested static class (named "LogPanel") for the actual input area. The button is outside the LogPanel class, and when I press it I want it to be able to take the values of the TextFields inside the LogTab (named "logBase" and "logNum"), calculate them, and send them to an output class. I have everything fine and working except for <strong>the part where I take the values from the TextFields</strong>.

Here's my code.

public class LogTab extends JPanel { @SuppressWarnings("serial") static class LogInput extends JComponent { public LogInput() { JComponent logInputPanel = new JPanel(); setLayout(new GridBagLayout()); JTextField logLbl = new JTextField(); logLbl.setText("Log"); logLbl.setEditable(false); JTextField logBase = new JTextField(1); JTextField logNum = new JTextField(5); GridBagConstraints lgc = new GridBagConstraints(); lgc.weightx = 0.5; lgc.weighty = 0.5; lgc.gridx = 0; lgc.gridy = 0; add(logLbl,lgc); lgc.gridx = 1; lgc.gridy = 1; add(logBase,lgc); lgc.gridx = 2; lgc.gridy = 0; add(logNum,lgc); } } public LogTab() { // Set Layout setLayout(new GridBagLayout()); // Create components JLabel promptLabel = new JLabel("Enter Logarithm: "); JButton solveButton = new JButton("Solve"); final LogInput logInput = new LogInput(); solveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { double base = Double.valueOf(logInput.logBase.getText()); double num = Double.valueOf(logInput.logNum.getText()); OutputPanel.outputField.setText(String.valueOf(solve(base,num))); } finally { } } }); // Code that adds the components, bla bla bla } public double solve(double base, double num) { return (Math.log(base)/Math.log(num)); } }

When I try to compile this (through Eclipse, by the way), I get an error saying that "logBase/logNum can not be resolved or is not a field". How would I change this so that my ActionListener can get the text from the TextFields?

Thanks

P.S. This is my first question on Stack Overflow, so if I messed something up, tell me :)

Answer1:

Make logBase and logNum instance fields...

static class LogInput extends JComponent { private JTextField logBase; private JTextField logNum; public LogInput() { JComponent logInputPanel = new JPanel(); setLayout(new GridBagLayout()); JLabel logLbl = new JLabel("Log"); logBase = new JTextField(1); logNum = new JTextField(5);

Now add some getters...

public double getBase() { String text = logBase.getText(); if (text.trim().isEmpty()) { text = "0"; } return Double.parseDouble(text); } public double getNumber() { String text = logNum.getText(); if (text.trim().isEmpty()) { text = "0"; } return Double.parseDouble(text); }

Now you can access the values of logBase and logNum from any instance of LogInput

About this point, I'm thinking that either a JSpinner or JTextField would be a better idea, as they have the ability to validate the input themselves. See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html" rel="nofollow">How to Use Spinners</a> and <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html" rel="nofollow">How to Use Formatted Text Fields</a> for more details

Answer2:

The issue is that the scope of logBase and logNum is limited only to the constructor, and therefore is not accessible by other methods.

Make logBase and logNum fields of LogInput:

static class LogInput extends JComponent { JTextField logBase, logNum;

And in the constructor, remove the JTextField identifier:

logBase = new JTextField(1); logNum = new JTextField(5);

Recommend

  • Set JFrame background transparent but show contents
  • Resize JFrame to JPanels inside JLayeredPane
  • Counting line breaks in a JTextArea
  • Java 2 JPanel's in one JFrame layout
  • Add a button over image
  • JavaFX - how to recognize the position of ScrollBar in a TableView
  • Java: Disable all JToggleButtons after Submission — setEnabled(false);
  • Wrong value in EditText when changing screen orientation
  • Jackson Parser can't read backslash quotation marks in String
  • Java GUI Problems
  • Android ListView with toggle button
  • Find/replace Highlight replaced words Netbeans/Java
  • How to get styled text from TextView
  • Issues with unchecked generics
  • Setting UILabel background color causes app to crash
  • list view image is getting changed when scrolling
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • Android MVVM with Programmatic UI Customization
  • Display charecters in textview with time delay - android
  • Using Java to develop cross-platform, fonts scaling differently on different platforms
  • DirectoryChooser Javafx buged?
  • Fixing the size of JEditorPane Content
  • How to test method of JavaFX controller?
  • How to make a modal JFrame or another JComponent?
  • Spannable text is too wide for TextView
  • PhoneStateListener doesn't call
  • Border Layout not working
  • Textview values does not update when data received from Arduino
  • How to hide or deactivate a TextField and a Label with javafx
  • GridBagLayout padding
  • Why is this code not working? Hangman
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • UIScrollView setContentOffset: animated: not working
  • How do you remove the JComboBox 'click and see dropdown' functionality?
  • QLineEdit password safety
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • JTable with a ScrollPane misbehaving