You need to create a form with 2 columns (label and input field). The user must be able to add fields (for example, add multiple names with the same label). Further, these data must be read and passed to another class. The problem is that it is not known in advance how much this data will be. I understand that this should be an array, but I do not understand how to add this data to it. How best to implement this (swing, javafx)? How to enter data so that they can be read later without knowing the quantity initially? I tried javafx with a GridPane, but then I didn’t get the data by cell number from it.

  • one
    Look in the direction of creating your own panel inherited from JPanel, which will encapsulate the logic, that is, show JLabel and JTextField and have the "Add field" button. The panel itself will know about all the fields that it has, respectively, you can add a method that receives all the information. - ezhov_da
  • I haven't dealt with a swing before. those. maybe it will read data from certain fields (I only need JTextField) in any volume, into an array of strings, for example? - Ksenia
  • JavaFX continued development of swing, feel free to use it. You need to add event handlers (listeners) on the condition of adding fields and add links to the fields in your collection (for example in ArrayList), from which you can get fields if necessary and the values ​​from them are DaysLikeThis

1 answer 1

I do not fully understand whether I understood you correctly, but I threw a simple example on Swing with the dynamic addition / deletion of fields and the output of the result.

The event processing of interest to you occurs in the buttonShawAllText button listener.

import javax.swing.*; import java.awt.*; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; public class TestSwing { private static final Logger LOG = Logger.getLogger(TestSwing.class.getName()); public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new PanelCommon()); frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); }); } } class PanelTextField extends JPanel { private JTextField textField; private JButton buttonRemove; private JPanel panelParent; public PanelTextField(JPanel panelParent) { setLayout(new BorderLayout()); this.panelParent = panelParent; textField = new JTextField(); buttonRemove = new JButton("X"); buttonRemove.addActionListener(e -> { SwingUtilities.invokeLater(() -> { PanelTextField.this.panelParent.remove(PanelTextField.this); PanelTextField.this.panelParent.revalidate(); PanelTextField.this.panelParent.repaint(); }); }); add(textField, BorderLayout.CENTER); add(buttonRemove, BorderLayout.EAST); } public String getText() { return textField.getText(); } } class PanelCommon extends JPanel { private JLabel label; private List<PanelTextField> panelTextFieldList = new ArrayList(); private JPanel panelContainer; private JButton buttonAdd; private JButton buttonShawAllText; public PanelCommon() { setLayout(new BorderLayout()); label = new JLabel("Тестовый заголовок"); add(label, BorderLayout.NORTH); panelContainer = new JPanel() { @Override public void remove(Component comp) { panelTextFieldList.remove(comp); super.remove(comp); } }; panelContainer.setLayout(new BoxLayout(panelContainer, BoxLayout.Y_AXIS)); add(new JScrollPane(panelContainer), BorderLayout.CENTER); buttonAdd = new JButton("Добавить новое поле"); buttonAdd.addActionListener(e -> { SwingUtilities.invokeLater(() -> { PanelTextField panelTextField = new PanelTextField(panelContainer); panelTextFieldList.add(panelTextField); panelContainer.add(panelTextField); panelContainer.revalidate(); PanelCommon.this.revalidate(); }); }); buttonShawAllText = new JButton("Показать весь текст"); buttonShawAllText.addActionListener(e -> { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < panelTextFieldList.size(); i++) { PanelTextField panelTextField = panelTextFieldList.get(i); String text = panelTextField.getText(); if (text != null && !"".equals(text)) { stringBuilder.append((i + 1) + " - " + panelTextField.getText()); stringBuilder.append("\n"); } } JOptionPane.showMessageDialog(PanelCommon.this, stringBuilder.toString()); }); JPanel panel = new JPanel(); panel.add(buttonAdd); panel.add(buttonShawAllText); add(panel, BorderLayout.SOUTH); } }