how do you find the text of anonymous jtextfield?

This is a mock solution (only meant to show how to update qty label and clear fields using action listeners)

public class MockFrame {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel framePanel = new JPanel();
        ProductPanel bananaPanel = new ProductPanel("268", "25", "Bananas");
        ProductPanel sugarPanel = new ProductPanel("321", "200", "Sugar");
        
        JPanel buttonPanel = new JPanel();
        JButton update = new JButton("Update");
        JButton cancel = new JButton("Cancel");
        
        buttonPanel.setSize(400, 30);
        update.setSize(50, 20);
        cancel.setSize(50, 20);
        buttonPanel.add(update);
        buttonPanel.add(cancel);
        
        update.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                bananaPanel.setNewQty();
                sugarPanel.setNewQty();
            }
        });

        cancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                bananaPanel.clearField();
                sugarPanel.clearField();
            }
        });
        
        frame.setSize(400, 400);
        framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.PAGE_AXIS));
        framePanel.add(bananaPanel);
        framePanel.add(sugarPanel);
        framePanel.add(buttonPanel);
        frame.add(framePanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    private static class ProductPanel extends JPanel {
        private JLabel productId = new JLabel();
        private JLabel qty = new JLabel();
        private JLabel name = new JLabel();
        private JTextField field = new JTextField();
        
        public ProductPanel(String id, String amount, String itemName) {
            this.setSize(400, 30);
            this.field.setSize(100, 20);
            this.field.setColumns(5);
            
            productId.setText(id);
            qty.setText(amount);
            name.setText(itemName);
            
            this.add(productId);
            this.add(qty);
            this.add(name);
            this.add(field);
        }

        public void clearField() {
            field.setText("");
        }
        
        public void setNewQty() {
            String newQty = field.getText();
            if (newQty != null && !newQty.isBlank()) {
                qty.setText(newQty);
            }
        }
    }
}

Main points of this mocked solution:

  1. Use a JPanel to encapsulate a product line item. This will make it easier if you need to remove and/or add product rows.
  2. The product panel contains a method to update qty or clear the fields that will be invokable by the frame buttons (depending on which is clicked).
  3. Simplicity of design – Creating a generic panel for the product eliminate repetitive code.

Obviously, you would have to modify this so that you use the proper layout manager or use absolute positioning to properly aligned components to your liking. Also, you would need to create a Panel for the table header and add the remaining of your products. Also, you may want to break this into public classes and even maybe create a separate class for your frame.

The action listeners could also have a “for-each” loop to update each ProductPanel instead of hard coding each panel individually. That should look something like this:

        public void actionPerformed(ActionEvent e) {
            JPanel panel = (JPanel)((JButton) e.getSource()).getParent().getParent();
            Component[] components = panel.getComponents();
            for (Component c : components) {
                if (c instanceof ProductPanel) {
                    ((ProductPanel)c).setNewQty();
                }
            }
        }
    });

Obviously, your solution will depend on how you decide to encapsulate your components in containers. For this mock, the product panels are inside the frame panel which contains the panels where the buttons were placed. Therefore, I need to get the “grandparent” container for the update and cancel buttons to take advantage of calling the appropriate methods to update and clear in a more dynamic way.

Lastly, you may want to do something more elegant for creating your product panels. For example, you may want to add some factory method to create your product panel instead of having hard-coded product panels like my mock solution. Anyway, I think I demonstrated the solution you were looking for.

UPDATE: If you don’t follow Andrew Thompson’s recommendation of not using text fields for numeric values, the panel’s getNewQty method would need to validate the text obtained to make sure it contains a valid numeric value (which was his point). I would STRONGLY recommend you follow his advice.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top