0

Hi I am working on an innovation in my company to handle the hits from the server so for that user has to add the service names in my application which is of monitoring application. So i have kept a button which will give the jtextfields counting to 9 so during submit button, how will i get the values from all the textboxes, below is my code,

package com.Lawrence;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Sample2 implements ActionListener
{
    JFrame mainFrame;
    static int count = 0;
    static int width_textfield = 10;
    static int height_textfield = 40;
    static int height = 0;
    JButton addTextField,submit;
    JTextField virtualDirectories;
    JLabel virtualDirectoriesName;
    ArrayList<String> texts = new ArrayList<String>();

    public Sample2()
    {
        mainFrame = new JFrame("Add Virtual Directory");
        mainFrame.setSize(640,640);
        mainFrame.setResizable(false);
        mainFrame.setLayout(null);

        addTextField = new JButton();
        addTextField.setText("Add Virtual directory");
        addTextField.setBounds(10, 10, 200, 25);
        addTextField.addActionListener(this);
        mainFrame.add(addTextField);

        submit = new JButton();
        submit.setText("Submit");
        submit.setBounds(180, 560, 100, 25);
        submit.addActionListener(this);
        mainFrame.add(submit);

        mainFrame.setVisible(true);

        height = mainFrame.getHeight();
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getActionCommand()== "Add Virtual directory")
        {
            if(height_textfield <= height-80)
            {
                virtualDirectoriesName = new JLabel("Virtual Directory"+"\t"+":");
                virtualDirectoriesName.setBounds(10,height_textfield,200, 25);
                mainFrame.add(virtualDirectoriesName);

                virtualDirectories = new JTextField();
                virtualDirectories.setBounds(150,height_textfield,200,25);
                mainFrame.add(virtualDirectories);

                texts.add(virtualDirectories.getText());

                count++;
                //width_textfield++;
                height_textfield = height_textfield+60;

                mainFrame.revalidate();
                mainFrame.repaint();

                //http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
            }
            else
            {
                JOptionPane.showMessageDialog(mainFrame, "can only add"+count+"virtual Directories");
            }
        }
        if(e.getActionCommand() == "Submit")
        {
            ArrayList<String> texts = new ArrayList<String>();
            for(int i = 0; i< count;i++)
            {
                texts.add(virtualDirectories.getText());
            }

            System.out.println(texts.size());
            System.out.println(texts.toString());
        }
    }
}

So i need to get the values from those textboxes and add it to an arraylist and then processing it to enter into my server for parsing the log files.So please explain me how to do it

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1791442
  • 11
  • 1
  • 4
  • 3
    *"So please explain me how to do it"* What have you tried? Where did you get stuck? SO is not a site to find tutors, so don't expect someone to offer. `mainFrame.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 12 '15 at 12:43

1 Answers1

2

You could store every textfield into an arraylist, just like you did with the texts. Also, please take a look at how to use layout managers.

ArrayList<JTextField> fields = new ArrayList<JTextField>();


fields.add(virtualDirectories);


for (int i = 0; i < count; i++) {
    texts.add(fields.get(i).getText());
}

Edit:

This is a version of your code using layout managers. (plus the lines above, of course)

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Sample2 implements ActionListener {
    JFrame mainFrame;

    JPanel bottom;
    JPanel center;
    JPanel centerPanel1;
    JPanel centerPanel2;

    static int count = 0;
    static int width_textfield = 10;
    static int height_textfield = 40;
    static int height = 0;

    JButton addTextField, submit;
    JTextField virtualDirectories;
    JLabel virtualDirectoriesName;

    ArrayList<JTextField> fields = new ArrayList<JTextField>();
    ArrayList<String> texts = new ArrayList<String>();

    int maxFields = 10;

    public Sample2() {
        mainFrame = new JFrame("Add Virtual Directory");
        mainFrame.setSize(640, 640);
        mainFrame.setResizable(false);

        addTextField = new JButton();
        addTextField.setText("Add Virtual directory");
        addTextField.setBounds(10, 10, 200, 25);
        addTextField.addActionListener(this);

        submit = new JButton();
        submit.setText("Submit");
        submit.setBounds(180, 560, 100, 25);
        submit.addActionListener(this);

        center = new JPanel(new GridLayout(1, 2));

        centerPanel1 = new JPanel(new GridLayout(maxFields, 1, 0, 20));
        centerPanel2 = new JPanel();

        center.add(centerPanel1);
        center.add(centerPanel2);

        bottom = new JPanel(new FlowLayout());
        bottom.add(addTextField);
        bottom.add(submit);

        mainFrame.getContentPane().add(bottom, BorderLayout.SOUTH);
        mainFrame.getContentPane().add(center, BorderLayout.CENTER);
        mainFrame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "Add Virtual directory") {

            if (count < maxFields) {

                JPanel p = new JPanel(new GridLayout(1, 2));

                virtualDirectoriesName = new JLabel("Virtual Directory" + "\t" + ":");
                virtualDirectories = new JTextField();

                p.add(virtualDirectoriesName);
                p.add(virtualDirectories);

                centerPanel1.add(p);

                texts.add(virtualDirectories.getText());
                fields.add(virtualDirectories);

                count++;
                // width_textfield++;
                height_textfield = height_textfield + 60;

                mainFrame.revalidate();
                mainFrame.repaint();

                // http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
            } else {
                JOptionPane.showMessageDialog(mainFrame, "can only add " + maxFields + " virtual Directories");
            }

        }
        if (e.getActionCommand() == "Submit") {
            ArrayList<String> texts = new ArrayList<String>();
            for (int i = 0; i < count; i++) {
                texts.add(fields.get(i).getText());
            }

            System.out.println(texts.size());
            System.out.println(texts.toString());
        }
    }

    public static void main(String[] args) {
        new Sample2();
    }

}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35