Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0

    Default using List interface to create multiple textfields through for loop

    Using the below code, I'm trying to use List interface for creating multiple textfields through a for loop SINCE I'm going to use them to assign the values of a 2D array into these later on. However, no textfields are displayed as output. Please consider I'm an absolute beginner and doing the codings step by step.

    Any help would be appreciated in advance

    Java Code:
    public class SudokuGUI extends JFrame 
    {
    
    private JPanel contentPane;
    private JTextField textField;
    
    public static void main(String[] args) 
    {
        EventQueue.invokeLater(new Runnable() 
    {
            public void run() 
            {
                try 
                {
                    SudokuGUI frame = new SudokuGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    
    public SudokuGUI() 
       {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);
    
        JLabel lblNewLabel = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 1;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);
    
        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 0;
        gbc_textField.gridy = 2;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);
    
    
    
        List<JComponent> labelList = new ArrayList<JComponent>();
        List<JComponent> textFieldList = new ArrayList<JComponent>();
        for (int i = 0; i < 10; i++) 
        {
            labelList.add(new JLabel(" test " + Integer.toString(i+1)));
            textFieldList.add(new JTextField());
        }
    }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    388
    Rep Power
    4

    Default Re: using List interface to create multiple textfields through for loop

    You still have to add each label and textfield to the contentpane. You might also want to set their location on the contentpane.

Similar Threads

  1. Create linked list using loop
    By RozenKristal in forum New To Java
    Replies: 1
    Last Post: 03-09-2013, 04:41 PM
  2. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  3. Help with GUI interface Multiple checkout
    By jav4newbie in forum AWT / Swing
    Replies: 8
    Last Post: 11-30-2011, 11:35 PM
  4. Multiple GUI Interface Instances
    By mutagen in forum AWT / Swing
    Replies: 2
    Last Post: 03-19-2011, 03:06 PM
  5. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 04:30 PM

Tags for this Thread

Posting Permissions

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