Hello all, I'm trying to add a multiple JTextFields onto a single JPanel that has been already initialized thanks to the Design tool from the JFrame. I'm pretty sure that I already added them and I'm not getting any errors but they aren't showing up when I execute the program. my code is:

initComponents();
for (int i = 0; i < MemoryCell.length; i ++){
    MemoryCell[i] = new javax.swing.JTextField();
    MemoryCellLocation.add(MemoryCell[i]);
}

//MemoryCellLocation is the JPanel and MemoryCell is the JTextFields

Edited 3 Years Ago by pritaeas: Fixed formatting

Have you added the panel to the JFrame? Is the JFrame set to "setVisible(true)"? Can you post the rest of the code please?

Edited 7 Years Ago by majestic0110: n/a

The JPanel is on the frame, but again I made everything with the Design tool of Java, I'll post what was pre generated but I don't think that matters.

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new GUI().setVisible(true);
        }
    });
}

private void initComponents() {

    CPU_Label = new javax.swing.JLabel();
    Memory_Label = new javax.swing.JLabel();
    Output_Label = new javax.swing.JLabel();
    Input_Label = new javax.swing.JLabel();
    AC_Label = new javax.swing.JLabel();
    IR_Label = new javax.swing.JLabel();
    IR_Label1 = new javax.swing.JLabel();
    AC_Display = new javax.swing.JTextField();
    IR_Display = new javax.swing.JTextField();
    Program_Counter = new javax.swing.JTextField();
    MemoryCellLocation = new javax.swing.JPanel();
    MenuBar = new javax.swing.JMenuBar();
    File = new javax.swing.JMenu();
    New = new javax.swing.JMenuItem();
    Open = new javax.swing.JMenuItem();
    Save = new javax.swing.JMenuItem();
    SaveAs = new javax.swing.JMenuItem();
    Delete = new javax.swing.JMenuItem();
    Exit = new javax.swing.JMenuItem();
    Edit = new javax.swing.JMenu();
    Undo = new javax.swing.JMenuItem();
    Cut = new javax.swing.JMenuItem();
    Copy = new javax.swing.JMenuItem();
    Paste = new javax.swing.JMenuItem();
    Insert = new javax.swing.JMenuItem();
    Clear_All = new javax.swing.JMenuItem();
    Clear_Memory = new javax.swing.JMenuItem();
    Clear_Input = new javax.swing.JMenuItem();
    Clear_Output = new javax.swing.JMenuItem();
    Clear_CPU = new javax.swing.JMenuItem();
    RunMenuOption = new javax.swing.JMenu();
    Run = new javax.swing.JMenuItem();
    Trace = new javax.swing.JCheckBoxMenuItem();
    Step = new javax.swing.JCheckBoxMenuItem();
    Help = new javax.swing.JMenu();
    Index = new javax.swing.JMenuItem();
    ShortOpCodes = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Steven Kawafuchi's SC Emulator");

    CPU_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    CPU_Label.setText("CPU");

    Memory_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    Memory_Label.setText("Memory");

    Output_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    Output_Label.setText("Output");

    Input_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    Input_Label.setText("Input");

    AC_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    AC_Label.setText("AC");

    IR_Label.setFont(new java.awt.Font("Smudger LET", 0, 24));
    IR_Label.setText("IR");

    IR_Label1.setFont(new java.awt.Font("Smudger LET", 0, 24));
    IR_Label1.setText("PC");

    AC_Display.setEditable(false);

    IR_Display.setEditable(false);

    Program_Counter.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            Program_CounterKeyTyped(evt);
        }
    });

    org.jdesktop.layout.GroupLayout MemoryCellLocationLayout = new org.jdesktop.layout.GroupLayout(MemoryCellLocation);
    MemoryCellLocation.setLayout(MemoryCellLocationLayout);
    MemoryCellLocationLayout.setHorizontalGroup(
        MemoryCellLocationLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 521, Short.MAX_VALUE)
    );
    MemoryCellLocationLayout.setVerticalGroup(
        MemoryCellLocationLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 378, Short.MAX_VALUE)
    );

    File.setText("File");

    New.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
    New.setText("New");
    New.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            NewActionPerformed(evt);
        }
    });
    File.add(New);

    Open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
    Open.setText("Open...");
    Open.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            OpenActionPerformed(evt);
        }
    });
    File.add(Open);

    Save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
    Save.setText("Save");
    Save.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            SaveActionPerformed(evt);
        }
    });
    File.add(Save);

    SaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
    SaveAs.setText("Save As...");
    SaveAs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            SaveAsActionPerformed(evt);
        }
    });
    File.add(SaveAs);

    Delete.setText("Delete...");
    Delete.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            DeleteActionPerformed(evt);
        }
    });
    File.add(Delete);

    Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
    Exit.setText("Exit");
    Exit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ExitActionPerformed(evt);
        }
    });
    File.add(Exit);

    MenuBar.add(File);

    Edit.setText("Edit");

    Undo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
    Undo.setText("Undo");
    Undo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            UndoActionPerformed(evt);
        }
    });
    Edit.add(Undo);

    Cut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
    Cut.setText("Cut");
    Cut.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CutActionPerformed(evt);
        }
    });
    Edit.add(Cut);

    Copy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
    Copy.setText("Copy");
    Copy.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CopyActionPerformed(evt);
        }
    });
    Edit.add(Copy);

    Paste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
    Paste.setText("Paste");
    Paste.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            PasteActionPerformed(evt);
        }
    });
    Edit.add(Paste);

    Insert.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_INSERT, 0));
    Insert.setText("Insert");
    Insert.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            InsertActionPerformed(evt);
        }
    });
    Edit.add(Insert);

    Clear_All.setText("Clear All");
    Clear_All.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Clear_AllActionPerformed(evt);
        }
    });
    Edit.add(Clear_All);

    Clear_Memory.setText("Clear Memory");
    Clear_Memory.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Clear_MemoryActionPerformed(evt);
        }
    });
    Edit.add(Clear_Memory);

    Clear_Input.setText("Clear Input");
    Clear_Input.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Clear_InputActionPerformed(evt);
        }
    });
    Edit.add(Clear_Input);

    Clear_Output.setText("Clear Output");
    Clear_Output.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Clear_OutputActionPerformed(evt);
        }
    });
    Edit.add(Clear_Output);

    Clear_CPU.setText("Clear CPU");
    Clear_CPU.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            Clear_CPUActionPerformed(evt);
        }
    });
    Edit.add(Clear_CPU);

    MenuBar.add(Edit);

    RunMenuOption.setText("Run");

    Run.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
    Run.setText("Run");
    Run.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            RunActionPerformed(evt);
        }
    });
    RunMenuOption.add(Run);

    Trace.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
    Trace.setSelected(false);
    Trace.setText("Trace");
    Trace.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            TraceActionPerformed(evt);
        }
    });
    RunMenuOption.add(Trace);

    Step.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
    Step.setSelected(false);
    Step.setText("Step");
    Step.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StepActionPerformed(evt);
        }
    });
    RunMenuOption.add(Step);

    MenuBar.add(RunMenuOption);

    Help.setText("Help");

    Index.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
    Index.setText("Index");
    Index.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            IndexActionPerformed(evt);
        }
    });
    Help.add(Index);

    ShortOpCodes.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
    ShortOpCodes.setText("Short OpCodes");
    ShortOpCodes.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ShortOpCodesActionPerformed(evt);
        }
    });
    Help.add(ShortOpCodes);

    MenuBar.add(Help);

    setJMenuBar(MenuBar);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(103, 103, 103)
                    .add(CPU_Label)
                    .add(375, 375, 375)
                    .add(Memory_Label))
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(33, 33, 33)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(AC_Label)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(AC_Display, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(10, 10, 10)
                            .add(IR_Label)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(IR_Display, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(Input_Label)
                            .add(18, 18, 18)
                            .add(Output_Label)))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(IR_Label1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(Program_Counter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(66, 66, 66)
                    .add(MemoryCellLocation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(10, 10, 10)
                    .add(Memory_Label))
                .add(CPU_Label))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(MemoryCellLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(AC_Label)
                        .add(IR_Label)
                        .add(AC_Display, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(IR_Display, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(IR_Label1)
                        .add(Program_Counter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(98, 98, 98)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(Input_Label)
                        .add(Output_Label))))
            .addContainerGap())
    );

    pack();
}// </editor-fold>

Edited 3 Years Ago by pritaeas: Fixed formatting

GUI() {
        initComponents();
    }

that won't do anything, are you just suggesting that I get rid of my jTextFields and initialize them inside the initcomponents()????

show me constructor of your class or indicate place where you invoke

initComponents();

inside this class

I don't think I understand, I already showed you where I invoked it, and all of the code inside the method....

static  JTextField[] MemoryCell = new JTextField[5];

    GUI() {
        initComponents();
        for (int i = 0; i < MemoryCell.length; i++) {
            MemoryCell[i] = new javax.swing.JTextField("any        text");//////
            MemoryCellLocation.add(MemoryCell[i]);
        }
    }
static  JTextField[] MemoryCell = new JTextField[5];

    GUI() {
        initComponents();
        for (int i = 0; i < MemoryCell.length; i++) {
            MemoryCell[i] = new javax.swing.JTextField("any        text");//////
            MemoryCellLocation.add(MemoryCell[i]);
        }
    }

nope. that didn't do anything

If you add components after your GUI is initialized (which it looks like you're doing) I think you need to call revalidate() or validate() on the JPanel or JFrame. If you read the revalidate method documentation it might make it clear which one needs to happen, if at all. Or you could just experiment.

org.jdesktop.layout.GroupLayout - usage

GUI() {
        initComponents();
        for (int i = 0; i < MemoryCell.length; i++) {
            MemoryCell[i] = new javax.swing.JTextField() {

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, super.getPreferredSize().height);
                }
            };
            MemoryCellLocation.add(MemoryCell[i]);
        }
        pack(); // all components added then pack()
    }

!!! I forgot. I commented whole MemoryCellLocationLayout org.jdesktop.layout.

org.jdesktop.layout.GroupLayout have two groups:HorizontalGroup and VerticalGroup
- layout.setHorizontalGroup
- layout.setVerticalGroup
where same components are added to two groups: Horizontal and Vertical.

Make new PanelForm inside NetBeans, set org.jdesktop.layout.GroupLayout add some JTextFields and look to machine generated code.

I have not experience with this layout
http://blogninja.com/doc/libswing-layout-java-doc/api/index.html?org/jdesktop/layout/package-tree.html

Edited 7 Years Ago by quuba: n/a

If you add components after your GUI is initialized (which it looks like you're doing) I think you need to call revalidate() or validate() on the JPanel or JFrame. If you read the revalidate method documentation it might make it clear which one needs to happen, if at all. Or you could just experiment.

Unfortunately that didn't do anything either

Once more: wrong usage of org.jdesktop.layout.GroupLayout

An example of JTextFieldPanel. You can extend this for your needs.
Number of items, setVisible(...), setEnabled(...), ... or write own.

Edited 7 Years Ago by quuba: n/a

This article has been dead for over six months. Start a new discussion instead.