Results 1 to 8 of 8
  1. #1
    JavaStarter is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default User input for frame

    Hello,
    I would like to know if it's possible to do the following. I want to take two JTextFields, and a JButton. One of the TextFields will take a number from 1-5 for an amount of rows, and the other will do the same for amount of columns. Then after the information is accepted and the button is clicked, the frame will produce numerous textfields based on the amount of rows and columns specified. Is this possible to do? The way that I am doing it right now compiles fine but when I use the button I a flood of errors. Can anyone help me out?

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, it is quite possible to do. And most here would be happy to try to help you out, but you'll need to provide a bit more information.

  3. #3
    JavaStarter is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Not a problem, that was a quick response!

    I have source code and I hope it's remotely close to what I tried to accomplish.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class frame extends JFrame
    {
    public frame(){

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(5,1));


    JButton jbt = new JButton("Enter");
    JLabel lbl = new JLabel("Rows");
    JLabel lbl2 = new JLabel("Columns");
    final JTextField row = new JTextField(1);
    final JTextField col = new JTextField(1);
    p1.add(jbt);
    p1.add(lbl);
    p1.add(row);
    p1.add(lbl2);
    p1.add(col);


    final JPanel p2 = new JPanel();

    final JPanel main = new JPanel();
    add(main);
    main.setLayout(new BorderLayout());
    main.add(p1, BorderLayout.WEST);

    jbt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){

    String c = String.valueOf(row);
    int d = Integer.parseInt(c);
    String f = String.valueOf(col);
    int g = Integer.parseInt(f);
    int k=d*g;
    p2.setLayout(new GridLayout(d,g));
    JTextField[] farray = new JTextField[k];
    int i;


    for (i=0;i<=k;i++)
    {
    p2.add(farray[i] = new JTextField(1));
    }

    main.add(p2, BorderLayout.EAST);
    }
    });







    pack();
    setVisible(true);
    setLocationRelativeTo(null);
    }
    public static void main(String[] args)
    {
    new frame();
    }
    }
    So like, 3 Panels, one for the button, labels and txt fields, another for the user inputted layout and the third one to hold them all. Any suggestions on why this isn't the correct way?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Couple of problems here:

    1) You need to read the Sun Swing tutorials (this is where most of us, including me, learned how to code Swing) about several issues but for one how to get text out of a JTextField. You don't do String.valueOf(myTextField) -- do a println on this to see what it contains. you do it by myTextField.getText().

    2) Arrays are 0 based. you need to loop them from i = 0 to i < array.length. Don't loop until i <= array.length or you will get out of bounds a lot.

    3) when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    good luck.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Other suggestions:

    1) Use descriptive names. tFieldHolderPanel is much easier to understand than "p2".
    2) Add this panel to your app in the frame constructor.
    3) in the actionperformed method, remove all from this panel.
    4) at the end of the actionperformed method, call pack() again on your frame. You'll need to do it like so:
    Java Code:
    frame.this.pack();
    This is because this is being called within an anonymous inner class, and inside here, "this" by itself refers to the anonymous inner class object itself.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Well, I guess you've solved it...
    I'm off to bed. Best of luck to you.

  7. #7
    JavaStarter is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    I have, thank you very much. I appreciate it!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cool. Now that you've solved it, here's my version of your code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class frame extends JFrame
    {
      public frame()
      {
    
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5, 1));
    
        JButton jbt = new JButton("Enter");
        JLabel lbl = new JLabel("Rows");
        JLabel lbl2 = new JLabel("Columns");
        final JTextField row = new JTextField(1);
        final JTextField col = new JTextField(1);
        p1.add(jbt);
        p1.add(lbl);
        p1.add(row);
        p1.add(lbl2);
        p1.add(col);
    
        final JPanel p2 = new JPanel();
    
        final JPanel main = new JPanel();
        add(main);
        main.setLayout(new BorderLayout());
        main.add(p1, BorderLayout.WEST);
        
        // add  your field-holding panel at creation.
        main.add(p2, BorderLayout.EAST);
    
        jbt.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            // first clear the field-holding panel
            p2.removeAll();
            
            // you'll probably want to do your parsing in a 
            // try / catch (NumberFormatException nfe) block
            
            //String c = String.valueOf(row);
            int d = Integer.parseInt(row.getText());
            //String f = String.valueOf(col);
            int g = Integer.parseInt(col.getText());
            int k = d * g;
            p2.setLayout(new GridLayout(d, g));
            JTextField[] farray = new JTextField[k];
            int i;
    
            //for (i = 0; i <= k; i++)
            for (i = 0; i < k; i++)
            {
              p2.add(farray[i] = new JTextField(1));
            }
    
            // already added above
            //main.add(p2, BorderLayout.EAST);
            
            // repack the JFrame to get it to resize and show the JTextFields
            frame.this.pack();
          }
        });
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
      }
    
      public static void main(String[] args)
      {
        new frame();
      }
    }
    and my own version:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class TextFieldArray
    {
      private JPanel mainPanel = new JPanel();
      private JPanel textFieldHolderPanel = new JPanel();
      private JTextField tFields[][];
      private JTextField rowField = new JTextField(10);
      private JTextField columnField = new JTextField(10);
    
      public TextFieldArray()
      {
        JButton createTextFieldBtn = new JButton("Add TextFields");
        createTextFieldBtn.addActionListener(new CreateTextFieldListener());
        
        JPanel controlPanel = new JPanel();
        controlPanel.add(new JLabel("Rows:"));
        controlPanel.add(rowField);
        controlPanel.add(Box.createHorizontalStrut(15));
        controlPanel.add(new JLabel("Columns:"));
        controlPanel.add(columnField);
        controlPanel.add(Box.createHorizontalStrut(15));
        controlPanel.add(createTextFieldBtn);
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(controlPanel, BorderLayout.NORTH);
        mainPanel.add(new JScrollPane(textFieldHolderPanel), BorderLayout.CENTER);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class CreateTextFieldListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          try
          {
            int rows = Integer.parseInt(rowField.getText());
            int cols = Integer.parseInt(columnField.getText());
            if (rows > 0 && cols > 0)
            {
              textFieldHolderPanel.removeAll();
              textFieldHolderPanel.setLayout(new GridLayout(rows, cols, 5, 5));
              tFields = new JTextField[rows][cols];
              for (int i = 0; i < tFields.length; i++)
              {
                for (int j = 0; j < tFields[i].length; j++)
                {
                  tFields[i][j] = new JTextField(5);
                  textFieldHolderPanel.add(tFields[i][j]);
                }
              }
              Window  window = SwingUtilities.getWindowAncestor(mainPanel);
              window.pack();
              window.setLocationRelativeTo(null);
            }
          }
          catch(NumberFormatException nfe)
          {
            rowField.setText("");
            columnField.setText("");
          }
        }
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("TextFieldArray");
        frame.getContentPane().add(new TextFieldArray().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  2. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  3. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  4. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

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