Hydra
Hydra

Reputation: 373

Accessing a variable from a class object apart of a JPanel

I have a class which is an extended JPanel with different swing components like text fields, checkboxes etc.

I'm trying to access the constructor's parameters from the class objects I have instantiated and added to the secondPanel JPannel. For example the cost, name, etc.

secondPanel.add(new ProductDesign("GPU: RTX 2070",649.99,"src/resources/rtx_card_2070.png"));
secondPanel.add(new ProductDesign("CPU: Intel i7-8700k",469.99,"src/resources/i7-8700k.png"));
secondPanel.add(new ProductDesign("CPU: Intel i5-9600k",309.99,"src/resources/i5_9600k.png"));

I iterate through the second pannel and I am able to get the state of some of the swing components, in this case I can get values of the selectBox with the method getAccessibleChild(3).

for (Component secondPanel : secondPanel.getComponents()) {
    ProductDesign.detect(secondPanel.getAccessibleContext().getAccessibleChild(3));
}

However, I'd also like to be able to get the values that each class object has. For example the cost, or name, from the class constructor. Is there a way to do this with this setup?

package main;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import javax.accessibility.Accessible;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ProductDesign extends JPanel{

    private static final long serialVersionUID = -8719763672439672064L;

    final static int checkX = 130;
    final static int checkY = 58;

    ProductDesign(String name, double cost, String img){
        JLabel productIcon = new JLabel();
        productIcon.setBounds(10, 10, 100, 80);
        productIcon.setIcon(new ImageIcon(img));
        this.add(productIcon);

        JLabel nameLabel = new JLabel(name);
        nameLabel.setBounds(130,-30,400,100);
        nameLabel.setForeground(Color.WHITE);
        nameLabel.setFont(new Font(name, Font.BOLD, 20));
        this.add(nameLabel);

        JLabel priceTag = new JLabel("$"+cost);
        priceTag.setBounds(130,28,100,40);
        priceTag.setForeground(Color.white);
        this.add(priceTag);

        JCheckBox confirmItem = new JCheckBox();
        confirmItem.setBounds(130,58,25,25);
        this.add(confirmItem);

        JLabel quantityText = new JLabel("Qty:");
        quantityText.setForeground(Color.LIGHT_GRAY);
        quantityText.setBounds(160,60,60,20);
        this.add(quantityText);

        JTextArea productQuantity = new JTextArea();
        productQuantity.setBounds(190, 60, 60, 20);
        this.add(productQuantity);

        this.setLayout(null);
        this.setBackground(new Color(100,100,110));
        this.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(3.0f), new Color(110,110,120)));
    }

    public static void detect(Accessible accessible) {
        if (((AbstractButton) accessible).isSelected()) {
            ((AbstractButton) accessible).setLocation(checkX-5, checkY);

        }
        else {
            ((AbstractButton) accessible).setLocation(checkX, checkY);
        }

    }
}

EDITS:

I added an action listener which is now within the ProductDesign class

package main;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.accessibility.Accessible;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class ProductDesign extends JPanel{

    private static final long serialVersionUID = -8719763672439672064L;

    final static int checkX = 130;
    final static int checkY = 58;

    String name;
    Double cost;
    int count;



    ProductDesign(String name, double cost, String img){
        JLabel productIcon = new JLabel();
        productIcon.setBounds(10, 10, 100, 80);
        productIcon.setIcon(new ImageIcon(img));
        this.add(productIcon);

        JLabel nameLabel = new JLabel(name);
        nameLabel.setBounds(130,-30,400,100);
        nameLabel.setForeground(Color.WHITE);
        nameLabel.setFont(new Font(name, Font.BOLD, 20));
        this.add(nameLabel);

        JLabel priceTag = new JLabel("$"+cost);
        priceTag.setBounds(130,28,100,40);
        priceTag.setForeground(Color.white);
        this.add(priceTag);

        JCheckBox confirmItem = new JCheckBox();
        confirmItem.setBounds(130,58,25,25);
        this.add(confirmItem);

        confirmItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (confirmItem.isSelected()) {
                    confirmItem.setLocation(checkX-5, checkY);
                }
                else {
                    confirmItem.setLocation(checkX, checkY);
                }
            }
        });

        JLabel quantityText = new JLabel("Qty:");
        quantityText.setForeground(Color.LIGHT_GRAY);
        quantityText.setBounds(160,60,60,20);
        this.add(quantityText);

        JTextArea productQuantity = new JTextArea();
        productQuantity.setBounds(190, 60, 60, 20);
        this.add(productQuantity);

        this.setLayout(null);
        this.setBackground(new Color(100,100,110));
        this.setBorder(BorderFactory.createStrokeBorder(new BasicStroke(3.0f), new Color(110,110,120)));

        this.name = name;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public double getCost() {
        return cost;
    }
}

From this action listener, what is the correct syntax to use those non-static methods to get the cost/name and count of the specific instance of the checkbox that was selected?

Upvotes: 0

Views: 76

Answers (1)

camickr
camickr

Reputation: 324137

  1. Create instance variables for the parameters
  2. Save the parameters in your instance variables
  3. Create getName(), getCost() methods as needed to access the data.

At the start of your class:

private String name;
private int cost;
...

In the constructor:

this.name = name;
this.cost = cost;
...

Custom methods in your class:

public String getName()
{
    return name;
}

...

Edit:

The code in the ActionListener for the "confirmItem" checkbox might be something like:

JCheckBox checkBox = (JCheckBoox)event.getSource();
ProductDesign pd = (ProductDesign)checkbox.getParent();
String name = pd.getName();

Upvotes: 2

Related Questions