Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setlayout when dynamic adding textField

Tags:

java

swing

jtable

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();

            }
        });

    }

}
like image 339
fanky Avatar asked Mar 27 '14 08:03

fanky


People also ask

Can you use basic editing operations when entering text in JTextField or JTextArea?

By default, JTextField and JTextArea are editable; you can type and edit in both text components.

What is difference between JTextArea and JTextField?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.


1 Answers

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();
            }
        });
    }
}
like image 65
mKorbel Avatar answered Sep 29 '22 14:09

mKorbel