Reputation: 11
I'm new at Java and following a beginners course right now. Very nice, i'm trying all kinds of stuff but now i'm stuck. This piece of code doesn't work. It should output the input in reverse order (by words).
The code to flip it works in a piece of code i wrote without the GUI and now i'm trying to get it to work in a GUI with fixed buttons, labels etc. For that i've copied an example from the internet and trying to change it in such a way it'll work. But it doesn't seem to find the variables i use in actionPerformed and are setup in AddComponentsToPane. It has to do something with static and non-static, which i can't seem to get really clear
Any help will greatly appreciated.
Heres the code.
package flipit;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*; //ArrayList;
import javax.swing.*;
public class FlipIt extends JFrame implements ActionListener {
public static void addComponentsToPane(Container pane) {
pane.setLayout(null);
JLabel greetingLabel = new JLabel("Enter your array ");
JTextField inField = new JTextField();
JTextField commentaryField = new JTextField();
JTextField strLen = new JTextField();
JButton button = new JButton("FlipIt");
pane.add(greetingLabel);
pane.add(inField);
pane.add(commentaryField);
pane.add(button);
Insets insets = pane.getInsets();
Dimension size = button.getPreferredSize();
greetingLabel.setBounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height);
inField.setBounds (120 + insets.left, 33 + insets.top,size.width + 200, size.height);
//size = commentaryField.getPreferredSize();
commentaryField.setBounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height);
size = button.getPreferredSize();
button.setBounds ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Reverse the input string.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(500 + insets.left + insets.right,
425 + insets.top + insets.bottom);
frame.setVisible(true);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent event) {
String InputStr = inField.getText();
int length = InputStr.length();
if (InputStr.compareTo("") == 0 || InputStr == null) {
commentaryField.setText("Please enter some data, this array is empty");
}
else {
// Convert string variable to an ArrayList.
List<String> list = new ArrayList<String>(Arrays.asList(InputStr.split(" ")));
// Converting ArrayList to a String Array
String [] InputList = list.toArray(new String[list.size()]);
int i = 0 ;
String ReverseOrder = "" ;
// starting for loop to print the array in reversed order.
for (i=InputList.length-1;i>=0; i--)
{ReverseOrder = ReverseOrder + " " + InputList[i] ;
}
// print result.
commentaryField.setText(ReverseOrder);
strLen.setText("Lengte string : "+ length);
}
}
}
Thanks, Rob.
Upvotes: 1
Views: 437
Reputation: 31
It might have worked if the button was tied to an ActionListner, don't you think so guys :-)
Just add
button.addActionListener(this);
just before the closing curly brace in the addComponentsToPane method of the last code proposed by Gerrod and it would make it works !
Regards
Upvotes: 0
Reputation: 6637
The primary problem is an issue with scoping. Since you're declaring the widgets inside the addComponentsToPane
method, they're not visible from outside of the method.
Try moving your widget declarations to outside of the addComponentstoPane
method:
JLabel greetingLabel = new JLabel("Enter your array ");
JTextField inField = new JTextField();
JTextField commentaryField = new JTextField();
JTextField strLen = new JTextField();
JButton button = new JButton("FlipIt");
public static void addComponentsToPane(Container pane) {
pane.setLayout(null);
pane.add(greetingLabel);
pane.add(inField);
pane.add(commentaryField);
pane.add(button);
// etc
}
As you've pointed out though (sorry, my bad!) your static method will no longer have access to the widgets (since they're now part of a class instance).
The easy way to think about static vs. non-static is that if you declare something as static, you do not need a class instance in order to access it. Hence in your code why you can do this:
public void run() {
createAndShowGUI();
}
Effectively, that's would be the same as doing this:
public void run() {
FlipIt.createAndShowGUI();
}
Note that you haven't created an instance of the FlipIt class; you don't need to, since the createAndShowGUI
method is static
. If however, it was not static, then you would have to create a new class instance, as follows:
public void createAndShowGUI() {
// do your thing - note NO LONGER STATIC
}
public void run() {
// Create instance
FlipIt flipper = new FlipIt();
// Invoke method against class instance
flipper.createAndShowGUI();
}
So - in order to get your code working, the best solution is to make everything non-static (except of course for the main
method, which must be static).
Here's the entire code sample all put together - note that you may need to make the createAndShowGUI
method public
- but I don't think so. It's been a while since I coded in java so I can't be certain.
package flipit;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*; //ArrayList;
import javax.swing.*;
public class FlipIt extends JFrame implements ActionListener {
JLabel greetingLabel = new JLabel("Enter your array ");
JTextField inField = new JTextField();
JTextField commentaryField = new JTextField();
JTextField strLen = new JTextField();
JButton button = new JButton("FlipIt");
public void addComponentsToPane(Container pane) {
pane.setLayout(null);
pane.add(greetingLabel);
pane.add(inField);
pane.add(commentaryField);
pane.add(button);
Insets insets = pane.getInsets();
Dimension size = button.getPreferredSize();
greetingLabel.setBounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height);
inField.setBounds (120 + insets.left, 33 + insets.top,size.width + 200, size.height);
//size = commentaryField.getPreferredSize();
commentaryField.setBounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height);
size = button.getPreferredSize();
button.setBounds ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height);
}
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Reverse the input string.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(500 + insets.left + insets.right,
425 + insets.top + insets.bottom);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String InputStr = inField.getText();
int length = InputStr.length();
if (InputStr.compareTo("") == 0 || InputStr == null) {
commentaryField.setText("Please enter some data, this array is empty");
} else {
// Convert string variable to an ArrayList.
List<String> list = new ArrayList<String>(Arrays.asList(InputStr.split(" ")));
// Converting ArrayList to a String Array
String [] InputList = list.toArray(new String[list.size()]);
int i = 0 ;
String ReverseOrder = "" ;
// starting for loop to print the array in reversed order.
for (i=InputList.length-1;i>=0; i--) {
ReverseOrder = ReverseOrder + " " + InputList[i] ;
}
// print result.
commentaryField.setText(ReverseOrder);
strLen.setText("Lengte string : "+ length);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
FlipIt flipper = new FlipIt();
flipper.createAndShowGUI();
}
});
}
}
}
Hope that helps!
Upvotes: 3