Quoted Text Here Hi, anyone can help pass values of JRadioButton to another JFrame? For example, if "twoway" Jradiobutton is selected, its value (a char i.e. "2-way face foundation") will be shown in another JFrame. Thanks in advanced..

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class prod extends JFrame
implements ActionListener

{
public prod()
{
super("Products | Princezz");

pm = new JPanel();
menup = new JPanel();
psp = new JPanel();
fp = new JPanel();
pf_1 = new JPanel();
pf_2 = new JPanel();
pf_3 = new JPanel();
pf_4 = new JPanel();
pf_5 = new JPanel();
f1p = new JPanel();
f2p = new JPanel();
f3p = new JPanel();
f4p = new JPanel();
f5p = new JPanel();

pe_1 = new JPanel();
pe_2 = new JPanel();
pe_3 = new JPanel();
pe_4 = new JPanel();
e1p = new JPanel();
e2p = new JPanel();
e3p = new JPanel();
e4p = new JPanel();

pl_1 = new JPanel();
pl_2 = new JPanel();
pl_3 = new JPanel();
pl_4 = new JPanel();
pl_5 = new JPanel();
l1p = new JPanel();
l2p = new JPanel();
l3p = new JPanel();
l4p = new JPanel();
l5p = new JPanel();

twoway = new JRadioButton("2-Way Face Foundation / S$25");
twoway.setBackground(Color.black);
twoway.addActionListener(this);

oilfree = new JRadioButton("Oil Free Beige Foundation / S$15");
oilfree.setBackground(Color.black);
oilfree.addActionListener(this);

watcre = new JRadioButton("Water cream foundation / S$30");
watcre.setBackground(Color.black);
watcre.addActionListener(this);

baspow = new JRadioButton("Basic Powder / S$12");
baspow.setBackground(Color.black);
baspow.addActionListener(this);

loopow = new JRadioButton("Loose Powder / S$17");
loopow.setBackground(Color.black);
loopow.addActionListener(this);

geleye = new JRadioButton("Gel eye liner / S$25");
geleye.setBackground(Color.black);
geleye.addActionListener(this);

mulpal = new JRadioButton("Multi colors shadows palette / S$55");
mulpal.setBackground(Color.black);
mulpal.addActionListener(this);

volmas = new JRadioButton("Volumize mascara / S$18");
volmas.setBackground(Color.black);
volmas.addActionListener(this);

earsha = new JRadioButton("Earth shadows / S$24");
earsha.setBackground(Color.black);
earsha.addActionListener(this);

lippal = new JRadioButton("Lipstick palette / S$30");
lippal.setBackground(Color.black);
lippal.addActionListener(this);

matlip = new JRadioButton("Long lasting matte lipstick / S$25");
matlip.setBackground(Color.black);
matlip.addActionListener(this);

redlip = new JRadioButton("Water proof hot red lipstick / S$30");
redlip.setBackground(Color.black);
redlip.addActionListener(this);

youlip = new JRadioButton("Youthful look lipgloss / S$9");
youlip.setBackground(Color.black);
youlip.addActionListener(this);

penlip = new JRadioButton("Pencil Lip liner / S$13");
penlip.setBackground(Color.black);
penlip.addActionListener(this);

pm.setLayout(new FlowLayout());
pm.setBackground(Color.black);

menup.setLayout(new GridLayout(1,0));
menup.setBackground(Color.black);

fp.setLayout(new GridLayout(1,0));
fp.setBackground(Color.black);

psp.setLayout( new BorderLayout() );
getContentPane().add( psp );

pf_1.setLayout(new GridLayout(0,1));
pf_1.setBackground(Color.black);

pf_2.setLayout(new GridLayout(0,1));
pf_2.setBackground(Color.black);

pf_3.setLayout(new GridLayout(0,1));
pf_3.setBackground(Color.black);

pf_4.setLayout(new GridLayout(0,1));
pf_4.setBackground(Color.black);

pf_5.setLayout(new GridLayout(0,1));
pf_5.setBackground(Color.black);

pe_1.setLayout(new GridLayout(0,1));
pe_1.setBackground(Color.black);

pe_2.setLayout(new GridLayout(0,1));
pe_2.setBackground(Color.black);

pe_3.setLayout(new GridLayout(0,1));
pe_3.setBackground(Color.black);

pe_4.setLayout(new GridLayout(0,1));
pe_4.setBackground(Color.black);

pl_1.setLayout(new GridLayout(0,1));
pl_1.setBackground(Color.black);

pl_2.setLayout(new GridLayout(0,1));
pl_2.setBackground(Color.black);

pl_3.setLayout(new GridLayout(0,1));
pl_3.setBackground(Color.black);

pl_4.setLayout(new GridLayout(0,1));
pl_4.setBackground(Color.black);

pl_5.setLayout(new GridLayout(0,1));
pl_5.setBackground(Color.black);

f1p.setLayout(new FlowLayout());
f1p.setBackground(Color.black);

f2p.setLayout(new FlowLayout());
f2p.setBackground(Color.black);

f3p.setLayout(new FlowLayout());
f3p.setBackground(Color.black);

f4p.setLayout(new FlowLayout());
f4p.setBackground(Color.black);

f5p.setLayout(new FlowLayout());
f5p.setBackground(Color.black);

e1p.setLayout(new FlowLayout());
e1p.setBackground(Color.black);

e2p.setLayout(new FlowLayout());
e2p.setBackground(Color.black);

e3p.setLayout(new FlowLayout());
e3p.setBackground(Color.black);

e4p.setLayout(new FlowLayout());
e4p.setBackground(Color.black);

l1p.setLayout(new FlowLayout());
l1p.setBackground(Color.black);

l2p.setLayout(new FlowLayout());
l2p.setBackground(Color.black);

l3p.setLayout(new FlowLayout());
l3p.setBackground(Color.black);

l4p.setLayout(new FlowLayout());
l4p.setBackground(Color.black);

l5p.setLayout(new FlowLayout());
l5p.setBackground(Color.black);

j_space01 = new JLabel(" ");
j_space01.setForeground(Color.black);

JLabel logo = new JLabel("Princezz",JLabel.LEFT);
logo.setForeground(Color.pink);
logo.setFont(new Font("Bradley Hand ITC",Font.ITALIC,33));

prod = new JButton ("PRODUCTS");
getContentPane().add(prod);

campaign= new JButton("CAMPAIGN");
campaign.addActionListener(this);
getContentPane().add(campaign);

mlogin = new JButton("Login | ");
mlogin.addActionListener(this);
getContentPane().add(mlogin, "East");

reg = new JButton("Join Us");
reg.addActionListener(this);
getContentPane().add(reg);

addb = new JButton("Add to cart");
addb.addActionListener(this);
getContentPane().add(addb);

refresh = new JButton("Refresh");
refresh.addActionListener(this);
getContentPane().add(refresh);

ImageIcon l1 = new ImageIcon("Flbl.jpg");
JLabel f_lbl = new JLabel(l1);

ImageIcon l2 = new ImageIcon("Elbl.jpg");
JLabel e_lbl = new JLabel(l2);

ImageIcon l3 = new ImageIcon("Llbl.jpg");
JLabel l_lbl = new JLabel(l3);

ImageIcon fa1 = new ImageIcon("f1.jpg");
JLabel f1 = new JLabel(fa1);

ImageIcon fa2 = new ImageIcon("f2.jpg");
JLabel f2 = new JLabel(fa2);

ImageIcon fa3 = new ImageIcon("f3.jpg");
JLabel f3 = new JLabel(fa3);

ImageIcon fa4 = new ImageIcon("f4.jpg");
JLabel f4 = new JLabel(fa4);

ImageIcon fa5 = new ImageIcon("f5.jpg");
JLabel f5 = new JLabel(fa5);

ImageIcon ea1 = new ImageIcon("e1.jpg");
JLabel e1 = new JLabel(ea1);

ImageIcon ea2 = new ImageIcon("e2.jpg");
JLabel e2 = new JLabel(ea2);

ImageIcon ea3 = new ImageIcon("e3.jpg");
JLabel e3 = new JLabel(ea3);

ImageIcon ea4 = new ImageIcon("e4.jpg");
JLabel e4 = new JLabel(ea4);

ImageIcon la1 = new ImageIcon("l1.jpg");
JLabel li1 = new JLabel(la1);

ImageIcon la2 = new ImageIcon("l2.jpg");
JLabel li2 = new JLabel(la2);

ImageIcon la3 = new ImageIcon("l3.jpg");
JLabel li3 = new JLabel(la3);

ImageIcon la4 = new ImageIcon("l4.jpg");
JLabel li4 = new JLabel(la4);

ImageIcon la5 = new ImageIcon("l5.jpg");
JLabel li5 = new JLabel(la5);

j_f1 = new JLabel("Quantity:");
j_f2 = new JLabel("Quantity:");
j_f3 = new JLabel("Quantity:");
j_f4 = new JLabel("Quantity:");
j_f5 = new JLabel("Quantity:");
j_e1 = new JLabel("Quantity:");
j_e2 = new JLabel("Quantity:");
j_e3 = new JLabel("Quantity:");
j_e4 = new JLabel("Quantity:");
j_l1 = new JLabel("Quantity:");
j_l2 = new JLabel("Quantity:");
j_l3 = new JLabel("Quantity:");
j_l4 = new JLabel("Quantity:");
j_l5 = new JLabel("Quantity:");

t_f1 = new JTextField(2);
t_f2 = new JTextField(2);
t_f3 = new JTextField(2);
t_f4 = new JTextField(2);
t_f5 = new JTextField(2);

t_e1 = new JTextField(2);
t_e2 = new JTextField(2);
t_e3 = new JTextField(2);
t_e4 = new JTextField(2);

t_l1 = new JTextField(2);
t_l2 = new JTextField(2);
t_l3 = new JTextField(2);
t_l4 = new JTextField(2);
t_l5 = new JTextField(2);

f1p.add(j_f1);
f1p.add(t_f1);

f2p.add(j_f2);
f2p.add(t_f2);

f3p.add(j_f3);
f3p.add(t_f3);

f4p.add(j_f4);
f4p.add(t_f4);

f5p.add(j_f5);
f5p.add(t_f5);

e1p.add(j_e1);
e1p.add(t_e1);

e2p.add(j_e2);
e2p.add(t_e2);

e3p.add(j_e3);
e3p.add(t_e3);

e4p.add(j_e4);
e4p.add(t_e4);

l1p.add(j_l1);
l1p.add(t_l1);

l2p.add(j_l2);
l2p.add(t_l2);

l3p.add(j_l3);
l3p.add(t_l3);

l4p.add(j_l4);
l4p.add(t_l4);

l5p.add(j_l5);
l5p.add(t_l5);

l5p.add(addb);
l5p.add(refresh);

menup.add(logo);
menup.add(j_space01);
menup.add(prod);
menup.add(campaign);
menup.add(mlogin);
menup.add(reg);

pm.add(menup);

pf_1.add(f1);
pf_1.add(twoway);
pf_1.add(f1p);

pf_2.add(f2);
pf_2.add(oilfree);
pf_2.add(f2p);

pf_3.add(f3);
pf_3.add(watcre);
pf_3.add(f3p);

pf_4.add(f4);
pf_4.add(baspow);
pf_4.add(f4p);

pf_5.add(f5);
pf_5.add(loopow);
pf_5.add(f5p);

pe_1.add(e1);
pe_1.add(geleye);
pe_1.add(e1p);

pe_2.add(e2);
pe_2.add(mulpal);
pe_2.add(e2p);

pe_3.add(e3);
pe_3.add(volmas);
pe_3.add(e3p);

pe_4.add(e4);
pe_4.add(earsha);
pe_4.add(e4p);

pl_1.add(li1);
pl_1.add(lippal);
pl_1.add(l1p);

pl_2.add(li2);
pl_2.add(matlip);
pl_2.add(l2p);

pl_3.add(li3);
pl_3.add(redlip);
pl_3.add(l3p);

pl_4.add(li4);
pl_4.add(youlip);
pl_4.add(l4p);

pl_5.add(li5);
pl_5.add(penlip);
pl_5.add(l5p);

fp.add(f_lbl);
fp.add(pf_1);
fp.add(pf_2);
fp.add(pf_3);
fp.add(pf_4);
fp.add(pf_5);
fp.add(e_lbl);
fp.add(pe_1);
fp.add(pe_2);
fp.add(pe_3);
fp.add(pe_4);
fp.add(l_lbl);
fp.add(pl_1);
fp.add(pl_2);
fp.add(pl_3);
fp.add(pl_4);
fp.add(pl_5);

    scrollPane = new JScrollPane();
    scrollPane.getViewport().add( fp );
    psp.add( scrollPane, BorderLayout.CENTER );

getContentPane().add(pm,"North");
getContentPane().add(psp);
setVisible( true );
setSize(1000,700);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == reg)
f = new reg();
this.hide();

if(e.getSource() == mlogin)
g = new ml();
this.hide();

        }   

public static void main(String args[])
{prod x = new prod();

}  

}

Dani AI

Generated

Short answer: treat product labels as Strings, collect the selected JRadioButtons when the user clicks the "view cart" button, then pass that list of Strings to the new frame (either via the constructor or a setter). is right to suggest calling a method on the other class; that can be done by creating the cart frame with the data or by calling a method on an existing cart frame instance. Also make a small test program (SSCCE) while you work — it keeps the problem clear.

A compact pattern you can use: give each radio an action command (or use the radio text), gather the selected radios into a List, then open the cart frame with that list:

JRadioButton[] radios = { twoway, oilfree, watcre /* add the rest */ };
List<String> selected = new ArrayList<>();
for (JRadioButton r : radios) {
    if (r.isSelected()) selected.add(
        r.getActionCommand() != null ? r.getActionCommand() : r.getText()
    );
}
new CartFrame(selected).setVisible(true);

And a very small CartFrame that shows the items:

public class CartFrame extends JFrame {
  public CartFrame(List<String> items) {
    JTextArea ta = new JTextArea();
    items.forEach(s -> ta.append(s + System.lineSeparator()));
    add(new JScrollPane(ta));
    pack();
    setLocationRelativeTo(null);
  }
}

Notes and cautions: if you want only one choice per category use a ButtonGroup for that category; if you want independent multi-selection, JCheckBox is usually clearer than JRadioButton. Use setActionCommand when the displayed label also contains price and you want to pass a clean product id/name. Avoid this.hide(); prefer setVisible(false) or dispose(). Make sure UI creation runs on the EDT (SwingUtilities.invokeLater). Keep the logic for building the cart in the view-cart button listener rather than trying to push state from every radio button.

Recommended Answers

All 4 Replies

You pass values between classes and methods by getting a reference to the class with the method you want to call and calling a method in that class.

TheClass aRef = <get a reference to the classs somehow>
aRef.someMethod(<The value to pass>); // call the method in the class and pass it the value

To work on the problem, please make a small, simple program the compiles and executes and shows the problem.
Don't post all of your code because most of it is not related to the problem.
Make a small simple program for testing this problem. What is called a SSCCE

Sorry, I will shorten the code.

Here it is..

twoway = new JRadioButton("2-Way Face Foundation / S$25");
twoway.setBackground(Color.black);
twoway.setForeground(Color.white);
java.awt.Font font00 = new java.awt.Font("Arial",Font.BOLD,12);
twoway.setFont(font00);
twoway.addActionListener(this);

oilfree = new JRadioButton("Oil Free Beige Foundation / S$15");
oilfree.setBackground(Color.black);
oilfree.setForeground(Color.white);
java.awt.Font font01 = new java.awt.Font("Arial",Font.BOLD,12);
oilfree.setFont(font01);
oilfree.addActionListener(this);

watcre = new JRadioButton("Water cream foundation / S$30");
watcre.setBackground(Color.black);
watcre.setForeground(Color.white);
java.awt.Font font02 = new java.awt.Font("Arial",Font.BOLD,12);
watcre.setFont(font02);
watcre.addActionListener(this);

vc = new JButton("STEP 3) VIEW YOUR CART");
vc.setBackground(Color.black);
vc.setForeground(Color.pink);
java.awt.Font font20 = new java.awt.Font("Arial",Font.BOLD,12);
vc.setFont(font20);
vc.addActionListener(this);
getContentPane().add(vc);

public void actionPerformed(ActionEvent e) {
if(e.getSource() == vc)
{

//what code should i input here?
I need to display the values of the 3 selected jradiobuttons on the next page??

}

The testing code needs to compile, execute and show the problem. That means a complete class with main() method etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.