setlayout when dynamic adding textField

fanky

I am adding dynamic JTextField and JLabel in panel1 but I am not able to set the layout of JTextField and JLabel. I need to add JTextfield and JLabel to panel1 and I add panel1 to panel. I need to add JTextFields and JLabels in a Top-Bottom manner and set layout. panel1 and panel are instances of JPanel.

My code :

public class MakeScrollablePanel extends JFrame implements ActionListener
{

    static JButton jButton11,jButton12;
    static JPanel panel,panel1;
    static JTextField jTextFields;
    static JLabel label;
    static JComboBox<String> jComboBox;
    static Dimension dime,dime1,dime2,dime3,dime4,dime5;
    static JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();  
    BoxLayout bx=null;//  @jve:decl-index=0:

    int count=1,i=0;

    public MakeScrollablePanel() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show(); 

        add(jButton11);
        add(scroll);
        dime=new Dimension(600,550);
        setSize(dime);
        setTitle("Priyank Panel");
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);

    }
     private void Show()
     {
         jButton11=new JButton("Add Designation");
         panel=new JPanel(); 
         bx=new BoxLayout(panel,BoxLayout.Y_AXIS);

         scroll=new JScrollPane(panel);
         dime1=new Dimension(500,3000);
         dime5=new Dimension(500,450);
         panelConstraints = new GridBagConstraints();
         scroll.setPreferredSize(dime5);
         scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         panel.setLayout(bx);
         panel.add(Box.createHorizontalBox());
         panel.setBorder(LineBorder.createBlackLineBorder());
         panel.setBackground(new Color(204, 230 , 255));
        jButton11.addActionListener(this);

     }
     public void actionPerformed(ActionEvent event) 
        {
            if(event.getSource()==jButton11)
             {
                label=new JLabel("Add Designation "+count  +" :-");
                jTextFields=new JTextField(30);

                panel1=new JPanel();
                panel1.setBackground(new Color(204, 230 , 255));
                panel1.add(label);
                panel1.add(jTextFields); 
                    panel.add(panel1);
                    panel1.revalidate();
                                panel.revalidate();
                     panel.updateUI();
                count++;
                i++;
             }
        }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MakeScrollablePanel();

            }
        });

    }

}
mKorbel

still not working

still I can't see thre any problem with (depsite fact that there is used BoxLayout instead of GridLayout, but result could be very similair in the case that is used many JTextFields)

enter image description here

.

enter image description here

.

enter image description here

from (little bit) modifyied OPs code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class MakeScrollablePanel extends JFrame implements ActionListener {

    private JButton jButton11, jButton12;
    private JPanel panel, panel1;
    private JTextField jTextFields;
    private JLabel label;
    private JComboBox<String> jComboBox;
    private Dimension dime, dime1, dime2, dime3, dime4, dime5;
    private JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();
    private BoxLayout bx = null;//  @jve:decl-index=0:
    private int count = 1, i = 0;

    public MakeScrollablePanel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show();
        add(jButton11, BorderLayout.NORTH);
        add(scroll);
        setTitle("Priyank Panel");
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(true);
    }

    private void Show() {
        jButton11 = new JButton("Add Designation");
        panel = new JPanel();
        bx = new BoxLayout(panel, BoxLayout.Y_AXIS);
        scroll = new JScrollPane(panel);
        dime5 = new Dimension(500, 150);
        panelConstraints = new GridBagConstraints();
        scroll.setPreferredSize(dime5);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel.setLayout(bx);
        panel.add(Box.createHorizontalBox());
        panel.setBorder(LineBorder.createBlackLineBorder());
        panel.setBackground(new Color(204, 230, 255));
        jButton11.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == jButton11) {
            label = new JLabel("Add Designation " + count + " :-");
            jTextFields = new JTextField(30);
            panel1 = new JPanel();
            panel1.setBackground(new Color(204, 230, 255));
            panel1.add(label);
            panel1.add(jTextFields);
            panel.add(panel1);
            panel.revalidate();
            panel.repaint();
            count++;
            i++;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MakeScrollablePanel();
            }
        });
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error when converting an object to TextField, and adding restrict

From Java

Error when adding input view to textfield iOS 8

From Dev

Adding space between textfield character when typing in text filed

From Dev

Css class priority when Adding Dynamic Class

From Dev

Css class priority when Adding Dynamic Class

From Dev

Adding a listener on a Wicket TextField

From Dev

Vaadin: Adding placeholder to TextField

From Dev

Vaadin: Adding placeholder to TextField

From Dev

Adding JavaFX TextField to JPanel

From Dev

Adding value from textfield

From Dev

Why I lost visual when I add setLayout(null)?

From Dev

Qt - Compiler complains when invoking setLayout() on my MainWindow

From Dev

Why I lost visual when I add setLayout(null)?

From Dev

Adding dynamic text to a tag when sorting by price, ascending and descending

From Dev

Dynamic column sum total when adding a row with JS

From Dev

Error when adding another record to dynamic memory database

From Java

Flutter adding text into TextField controller

From Dev

Adding TextField and its listerner in LibGdx

From Dev

ExtJs - Textfield with dynamic closeable labels

From Dev

Remove a character from a dynamic textfield

From Dev

Problems with dynamic TextField and decimal Number

From Dev

Adding dynamic buttons in WPF

From Dev

Adding to dynamic array

From Dev

Adding values to dynamic array

From Dev

Adding dynamic buttons in WPF

From Dev

Creating and adding to dynamic arrays

From Dev

Adding dynamic views in a fragment

From Dev

Dynamic adding of fields with jquery

From Dev

Adding dynamic title to Wordpress