Results 1 to 4 of 4

Thread: help please

  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default help please

    Hello again people, okay so here is what i am trying to do here. I am learning still a newbie if you will. I am going through "Learning Java" published by O'Reily and i have made it to the section on loops. the books gives examples of loops that print in the command line. i wish to use a gui to demonstrate the results of running the loops. i want to start them by pressing a button. let them run and them print the out put into a textfield[]. so far i have made a basic four panel frame with a label button and textfield[] in each. i think i have written the code for the loop to correctly print to the textfield. i am not sure if i have an enabled the button or not yet because it will not let me compile because it says i can not add a textfield to the j panel. i have tried both with flowlayout and gridlayout for the first panel. also i have not added the other loop examples yet until i get this first one situated. it gives me this message

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JPanel.add
    at com.blue2525989.DifferentLoops.<init>(DifferentLoo ps.java:94)
    at com.blue2525989.DifferentLoops.main(DifferentLoops .java:131)
    C:\Users\skinny\AppData\Local\NetBeans\Cache\8.1\e xecutor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 2 seconds)

    Java Code:
    package com.blue2525989;
    
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class DifferentLoops extends JFrame implements ActionListener {
        
                    void loop1() {
                        for (int i = 0; i < 100; i++) {
                System.out.print(i);
                int j = i;
                loop1TF[j] = new JTextField();
                
            }
        }
                    
                    void loop2() {
                        
                    }
                    
                    void loop3() {
                        
                    }
                    
                    void loop4() {
                        
                    }
                    
        
        //loop1
        JPanel loop1 = new JPanel();
        JLabel loop1L = new JLabel("Loop 1");
        
        JButton loop1B = new JButton("Loop 1");
       
            
        public void actionPerformed(ActionEvent event){
            String command = event.getActionCommand();
            if (command.equals("Loop 1")) {
                loop1();
                if (command.equals("Loop 2")) {
                    loop2();
                    if (command.equals("loop 3")) {
                        loop3();
                        if (command.equals("Loop 4")) {
                            loop4();
                        }
                    }
                }
        }
    }
        JTextField[] loop1TF = new JTextField[6];
        //loop2
        JPanel loop2 = new JPanel();
        JLabel loop2L = new JLabel("Loop 2");
        
        JButton loop2B = new JButton("Loop 2");
        
        JTextField loop2TF = new JTextField("",20);
        //loop3
        JPanel loop3 = new JPanel();
        JLabel loop3L = new JLabel("Loop 3");
        JButton loop3B = new JButton("Loop 3");
        JTextField loop3TF = new JTextField("",20);
        //loop4
        JPanel loop4 = new JPanel();
        JLabel loop4L = new JLabel("Loop 4");
        JButton loop4B = new JButton("Loop 4");
        JTextField loop4TF = new JTextField("",20);
        
        
        //sets up graphic user interface
        public DifferentLoops() {
            super("Different Loops Test Application");
            
            setSize(500,700);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout layout = new GridLayout(4,2,20,20);
            setLayout(layout);
            
            //add listeners once i set up buttons
            /*
            loop1B.addActionListener();
            loop2B.addActionListener();
            loop3B.addActionListener();
            loop4B.addActionListener();
            */
            
            GridLayout layout1 = new GridLayout (3,1,15,15);
            loop1.setLayout(layout1);
            loop1.add(loop1L);
            loop1.add(loop1B);
            loop1.add(loop1TF);
            add(loop1);
            
            FlowLayout layout2 = new FlowLayout (FlowLayout.LEFT);
            loop2.add(loop2L);
            loop2.add(loop2B);
            loop2.add(loop2TF);
            add(loop2);
            
            FlowLayout layout3 = new FlowLayout (FlowLayout.RIGHT);
            loop3.add(loop3L);
            loop3.add(loop3B);
            loop3.add(loop3TF);
            add(loop3);
            
            FlowLayout layout4 = new FlowLayout ();
            loop4.add(loop4L);
            loop4.add(loop4B);
            loop4.add(loop4TF);
            add(loop4);
            
            setVisible(true);
            
        }
        
        private static void setLookAndFeel() {
            try {
                UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (Exception exc) {
                //ignore the error
            }
        }
        
        public static void main(String[] args) {
            
            DifferentLoops.setLookAndFeel();
            DifferentLoops frame = new DifferentLoops();
            
            
          /*  for (int i = 0; i < 100; i++) {
                System.out.print(i);
                int j = i;
              */  
            }
    
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: help please

    You are adding an array to a JPanel on line 94 (loop1TF). If you want to add all the textfields, loop over the array and add them individually :
    Java Code:
    for( JTextFIeld tf: loop1TF) {
       lop1.add( tf);
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: help please

    A second observation is the creation of your array of JTextField. You are assuming that declaring an array also creates the instances but that is not the case. All the elements of loop1TF wil be null. You have to initialize the individual elements, which can be as simple as this:
    Java Code:
    JTextField[] loop1TF = new JTextField[6];
    for( int i = 0; i < loop1TF.length, i++) {
       loop1TF[i] = new JTextField();
    }
    Edit: ignore the post above. You are actually initializing the array. It's the strange order of the fields and methods that confused me. Still a nitpick then: you don't have to reassign the i to j:
    Java Code:
    void loop1() {
      for (int i = 0; i < 100; i++) {
         System.out.print(i);
         int j = i; //<-------- why? Just use i directly
         loop1TF[i] = new JTextField();
    }
    Last edited by SurfMan; 10-07-2016 at 03:08 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help please

    Yes i know it is in a goofy order im still new to all this and was messing around trying to make things work so its not organized yet. As far as switching i to j i did that because at first i was having difficulties adding a int to textfield till i figured out what i was doing wrong. I just left that in. I agree it could be deleted. I am not wanting to add all the textfields to this specific loop. There were more examples in the book i was going to add for each of the four panels. I just havent got to that point yet because i was trying to make this work forst so then i can look to it when i went and plugged in the other three. Then i was going to tweak them from there so i can better learn how they work. But it my head i figured i wanted to get my first example working before i proceeded foward and got myself really confused.
    Last edited by Blue2525989; 10-07-2016 at 04:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •