Pregunta

In my GUI class I have an array of textfields, which I initialized in this class constructor. The code is as follows

JTextField[] arrayOne= new JTextField [10];
        for(int a = 0;a < 10;a++)
        {
            arrayOne[a] = new JTextField(String.valueOf(0));
            arrayOne[a].setBounds(189 + a * 50, 156, 39, 23);
            arrayOne[a].setVisible(true);

        }

The problem is, that when I run my GUI, it does not show these text fields. Why not?

¿Fue útil?

Solución

You should add the JTextField in the layout.

You should use add method of the container/or the class if you extend JFrame inside the for.

Read this.

Adding Components to a Container

When you add components to a panel or content pane, the arguments you specify to the add method depend on the layout manager that the panel or content pane is using. In fact, some layout managers do not even require you to add the component explicitly; for example, GroupLayout. For example, BorderLayout requires that you specify the area to which the component should be added (using one of the constants defined in BorderLayout) using code like this:

pane.add(aComponent, BorderLayout.PAGE_START); The how-to section for each layout manager has details on what, if any, arguments you need to specify to the add method. Some layout managers, such as GridBagLayout and SpringLayout, require elaborate setup procedures. Many layout managers, however, simply place components based on the order they were added to their container.

Swing containers other than JPanel and content panes generally provide API that you should use instead of the add method. For example, instead of adding a component directly to a scroll pane (or, actually, to its viewport), you either specify the component in the JScrollPane constructor or use setViewportView. Because of specialized API like this, you do not need to know which layout manager (if any) many Swing containers use. (For the curious: scroll panes happen to use a layout manager named ScrollPaneLayout.)

For information about how to add components to a specific container, see the how-to page for the container. You can find the component how-to pages using How to Use Various Components.

Otros consejos

There is one more case for non visibility.

    f.setVisible(true);
    f.setSize(1000,1000);//Where f is the JFrame reference

Add the above statements after adding the JTextFields to the required Frame.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top