Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The primary problem is an issue with scoping. Since you're declaring the widgets <em>inside</em> the <code>addComponentsToPane</code> method, they're not visible from outside of the method.</p> <p>Try moving your widget declarations to outside of the <code>addComponentstoPane</code> method:</p> <pre><code>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 } </code></pre> <p>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 <em>instance</em>). </p> <p>The easy way to think about static vs. non-static is that if you declare something as static, you <em>do not</em> need a class instance in order to access it. Hence in your code why you can do this:</p> <pre><code>public void run() { createAndShowGUI(); } </code></pre> <p>Effectively, that's would be the same as doing this:</p> <pre><code>public void run() { FlipIt.createAndShowGUI(); } </code></pre> <p>Note that you haven't created an <em>instance</em> of the FlipIt class; you don't need to, since the <code>createAndShowGUI</code> method is <code>static</code>. If however, it was <em>not</em> static, then you would have to create a new class instance, as follows:</p> <pre><code>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(); } </code></pre> <p>So - in order to get your code working, the best solution is to make everything non-static (except of course for the <code>main</code> method, which <strong>must</strong> be static).</p> <p>Here's the entire code sample all put together - note that you may need to make the <code>createAndShowGUI</code> method <code>public</code> - but I don't think so. It's been a while since I coded in java so I can't be certain.</p> <pre><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 { 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&lt;String&gt; list = new ArrayList&lt;String&gt;(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&gt;=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(); } }); } } } </code></pre> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload