How do i display the radiobutton and label in the same row? - java

private String[] gender = {"Male","Female"};
private JComboBox jco = new JComboBox();
private JRadioButton[] jrbGender;
private ButtonGroup buttonGroup = new ButtonGroup();
private JButton jbtAdd = new JButton("Create");
private JButton jbtRetrieve = new JButton("Retrieve");
private JButton jbtUpdate = new JButton("Update");
private JButton jbtDelete = new JButton("Delete");
public RegistrationForMembership(){
JPanel jp1 = new JPanel(new GridLayout(6,2));
JPanel jp2 = new JPanel(new FlowLayout());
JPanel jp3 = new JPanel(new GridLayout(1,1));
jco = new JComboBox(membership);
jrbGender = new JRadioButton[gender.length];
add(jp1);
jp1.add(new JLabel("Member ID"));
jp1.add(jtfID);
jp1.add(new JLabel("Member Name"));
jp1.add(jtfName);
jp1.add(new JLabel("Member IC"));
jp1.add(jtfIC);
jp1.add(new JLabel("Address"));
jp1.add(jtfAddress);
jp1.add(new JLabel("Gender"));
for(int i =0; i<gender.length;++i){
jrbGender[i] = new JRadioButton(gender[i]);
buttonGroup.add(jrbGender[i]);
jp1.add(jrbGender[i]);
}
add(jp1);
The one of the radio button will go to the next line , how do i let the radio button on the same line with the label ?

Add the JRadioButtons to a new JPanel and add that one to jp1.
JPanel radios = new JPanel();
for (int i = 0; i < gender.length; ++i){
jrbGender[i] = new JRadioButton(gender[i]);
buttonGroup.add(jrbGender[i]);
radios.add(jrbGender[i]);
}
jp1.add(radios);
Also, it looks like jp1 should have 5 rows, not 6.
JPanel jp1 = new JPanel(new GridLayout(5,2));

Related

Java Swing GridLayout not looking like I expect it to

I have a GUI that is a grid layout with 6 rows and 4 columns, the order should go:
Label - RadioButton - RadioButton - ComboBox
This is successful for the first row. However each row afterwards is messed up. Please refer to the image of the GUI.
Please disregard the commented code as I am going to be setting the visibility later once I complete the GUI layout. Thanks.
JFrame frame = new JFrame("Zoho DNC List Updater");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(height, width);
// creating menu bar
JMenuBar mb = new JMenuBar();
// mb.setLayout(new FlowLayout());
JMenu m1 = new JMenu("File");
JMenu m2 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
JMenuItem contact = new JMenuItem("Contact Us");
JMenuItem help = new JMenuItem("Help");
JMenuItem openFile = new JMenuItem("Open File");
JMenuItem saveFile = new JMenuItem("Save File");
JMenuItem newWindow = new JMenuItem("New window");
m2.add(contact);
m2.add(help);
m1.add(openFile);
m1.add(saveFile);
m1.add(newWindow);
JPanel panel = new JPanel(); // the panel is not visible in output
JLabel label = new JLabel("Update Records");
// JTextField tf = new JTextField(10); // accepts upto 10 characters
JButton send = new JButton("Update");
JButton reset = new JButton("Quit");
panel.add(label); // Components Added using Flow Layout
// panel.add(tf);
panel.add(send);
panel.add(reset);
JPanel panel2 = new JPanel(new GridLayout(6, 4));
JRadioButton radio1 = new JRadioButton("Yes");
JRadioButton radio2 = new JRadioButton("No");
JLabel custom = new JLabel("Customize Call Report?");
ButtonGroup btnGrp = new ButtonGroup();
radio1.setBounds(120, 30, 120, 50);
radio2.setBounds(120, 30, 120, 50);
custom.setBounds(120, 30, 120, 50);
btnGrp.add(radio1);
btnGrp.add(radio2);
JComboBox campsList = new JComboBox(camps);
campsList.setSelectedIndex(5);
campsList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r){
JComboBox cb = (JComboBox)r.getSource();
String campName = (String)cb.getSelectedItem();
}
});
JComboBox inboundsList = new JComboBox(inbounds);
inboundsList.setSelectedIndex(5);
inboundsList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r){
JComboBox cb = (JComboBox)r.getSource();
String campName = (String)cb.getSelectedItem();
}
});
JComboBox statList = new JComboBox(stat);
statList.setSelectedIndex(5);
statList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r){
JComboBox cb = (JComboBox)r.getSource();
String campName = (String)cb.getSelectedItem();
}
});
JComboBox groupList = new JComboBox(groups);
groupList.setSelectedIndex(5);
groupList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r){
JComboBox cb = (JComboBox)r.getSource();
String campName = (String)cb.getSelectedItem();
}
});
JComboBox lisList = new JComboBox(lis);
lisList.setSelectedIndex(5);
lisList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r){
JComboBox cb = (JComboBox)r.getSource();
String campName = (String)cb.getSelectedItem();
}
});
ButtonGroup btnGrp1 = new ButtonGroup();
JRadioButton campaignY = new JRadioButton("Yes");
JRadioButton campaignN = new JRadioButton("No");
JLabel campaign = new JLabel("Select all campaigns?");
campaignY.setBounds(120, 30, 120, 50);
campaignN.setBounds(120, 30, 120, 50);
btnGrp1.add(campaignY);
btnGrp1.add(campaignN);
ButtonGroup btnGrp2 = new ButtonGroup();
JRadioButton inboundY = new JRadioButton("Yes");
JRadioButton inboundN = new JRadioButton("No");
JLabel inbound = new JLabel("Select all inbound groups?");
btnGrp2.add(inboundY);
btnGrp2.add(inboundN);
ButtonGroup btnGrp3 = new ButtonGroup();
JRadioButton statusY = new JRadioButton("Yes");
JRadioButton statusN = new JRadioButton("No");
JLabel status = new JLabel("Select all call statuses?");
btnGrp3.add(statusY);
btnGrp3.add(statusN);
statusY.setBounds(120, 30, 120, 50);
statusN.setBounds(120, 30, 120, 50);
ButtonGroup btnGrp4 = new ButtonGroup();
JRadioButton userGroupY = new JRadioButton("Yes");
JRadioButton userGroupN = new JRadioButton("No");
JLabel userGroup = new JLabel("Select all user groups?");
btnGrp4.add(userGroupY);
btnGrp4.add(userGroupN);
userGroupY.setBounds(120, 30, 120, 50);
userGroupN.setBounds(120, 30, 120, 50);
ButtonGroup btnGrp5 = new ButtonGroup();
JRadioButton listsY = new JRadioButton("Yes");
JRadioButton listsN = new JRadioButton("No");
JLabel lists = new JLabel("Select all lists?");
btnGrp5.add(listsY);
btnGrp5.add(listsN);
panel2.add(custom);
panel2.add(radio1);
panel2.add(radio2);
campsList.setVisible(true);
panel2.add(campsList);
panel2.add(campaign);
panel2.add(campaignY);
panel2.add(campaignN);
inboundsList.setVisible(true);
panel2.add(inboundsList);
// campaign.setVisible(false);
// campaignY.setVisible(false);
// campaignN.setVisible(false);
panel2.add(status);
panel2.add(statusY);
panel2.add(statusN);
panel2.add(statList);
// status.setVisible(false);
// statusY.setVisible(false);
// statusN.setVisible(false);
panel2.add(inbound);
panel2.add(inboundY);
panel2.add(inboundN);
panel2.add(inboundsList);
// inbound.setVisible(false);
// inboundN.setVisible(false);
// inboundY.setVisible(false);
panel2.add(userGroup);
panel2.add(userGroupY);
panel2.add(userGroupN);
panel2.add(groupList);
// userGroup.setVisible(false);
// userGroupN.setVisible(false);
// userGroupY.setVisible(false);
panel2.add(lists);
panel2.add(listsY);
panel2.add(listsN);
panel2.add(lisList);
// lists.setVisible(false);
// listsN.setVisible(false);
// listsY.setVisible(false);
So, there are two issues.
First, when both the column and row properties are specified, the column value is ignore, as you can find in the JavaDocs
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout.
So, in your case, I changed it to JPanel panel2 = new JPanel(new GridLayout(0, 4));
Second, an instance component may only reside in a single parent, once.
panel2.add(custom);
panel2.add(radio1);
panel2.add(radio2);
panel2.add(campsList);
panel2.add(campaign);
panel2.add(campaignY);
panel2.add(campaignN);
panel2.add(inboundsList);
//...
panel2.add(inbound);
panel2.add(inboundY);
panel2.add(inboundN);
panel2.add(inboundsList);
You're adding inboundsList twice, this has the effect of first removing it from the container and then re-adding it at the new position.
You will need to create a new instance of the of the JComoboBox with the same data model

BoxLayout Left Alignment

I'm trying to left align the class and neutral buttons so they are in line with the left most card button. For some reason, setAlignmentX only shifts the buttons half way. Here is the code. Is there away to align the buttons?
private String[] listEntries = {"a","a","a","a","a"};
private JButton remove = new JButton("Remove");
private JList list;
private JButton b1 = new JButton("Class");
private JButton b2 = new JButton("Neutral");
private JPanel page = new JPanel(new CardLayout());
private DefaultListModel listModel = new DefaultListModel();
public Main () {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel title = new JLabel("Deck Constructor", SwingConstants.CENTER);
title.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Set up Deck List
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(150, 80));
JLabel listTitle = new JLabel("List");
listTitle.setLabelFor(list);
listScroller.setAlignmentX(LEFT_ALIGNMENT);
rightPanel.add(listTitle);
rightPanel.add(listScroller);
rightPanel.add(remove);
//Set up Card Selection
JPanel buttonPanel = new JPanel();
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
b1.setActionCommand("Class");
b2.setActionCommand("Neutral");
b1.addActionListener(this);
b2.addActionListener(this);
buttonPanel.add(b1);
buttonPanel.add(b2);
JPanel classCards = new JPanel(new GridLayout(2,3, 10, 10));
JButton card1 = new JButton("Card 1");
card1.addActionListener(this);
card1.setActionCommand("addCard");
JButton card2 = new JButton("Card 2");
JButton card3 = new JButton("Card 3");
JButton card4 = new JButton("Card 4");
JButton card5 = new JButton("Card 5");
JButton card6 = new JButton("Card 6");
classCards.add(card1);
classCards.add(card2);
classCards.add(card3);
classCards.add(card4);
classCards.add(card5);
classCards.add(card6);
JPanel neutral = new JPanel();
neutral.setBackground(Color.BLUE);
page.add(classCards, "Class");
page.add(neutral, "Neutral");
leftPanel.add(buttonPanel);
leftPanel.add(page);
setPreferredSize(new Dimension(640,640/12*9));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(leftPanel,BorderLayout.CENTER);
getContentPane().add(rightPanel,BorderLayout.EAST);
getContentPane().add(title,BorderLayout.NORTH);
pack();
setVisible(true);
}
It is not perfect solution, but you can use for example:
If you want to keep buttons default size:
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,1,2));
and delete:
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
If you want to fill buttonPanel with buttons:
JPanel buttonPanel = new JPanel(new GridLayout(1,2,2,2));
buttonPanel.setBorder(new EmptyBorder(2,1,2,1));
In (FlowLayout.LEADING,1,2) and in EmptyBorder(2,1,2,1))the 1,2 values are added to match buttonPanel and classCard hgap and vgap.

Setting the size of JPanel that contains JRadioButton

I have some problem , i want my jpanel to look like this
but after codING i had this view and i didnt find the solution ,
i need some helps please.
i put the radio button inside jScrollPan
//listmateriel is a list (vector) of radio button ----------
//imagebox is jpanel contains jradio buttons and the image ---------
//gridlayoutPanel2 is Jpanel contains the two text fields and jlabel and a JComboBox
package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......
public class Frame extends JFrame implements ActionListener {
private static final int Haut = 550;
private static final int Larg = 720;
private final JPanel utilisateur;
private final JButton recherche;
private final JRadioButton radioButton,radioButton2;
private Vector listmateriel;
private final JLabel label;
public Frame()
{
super("Gestion des materiels");
setSize(Larg,Haut);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane menu = new JTabbedPane();
JPanel emprunt = new JPanel();
emprunt.setPreferredSize(new Dimension(Larg, Haut ));
emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));
JPanel emp1 = new JPanel();
emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
emp1.setLayout(new BorderLayout());
JLabel title2 = new JLabel("Nouvel emprunt:");
title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));
JPanel box2 = new JPanel();
box2.setPreferredSize(new Dimension(Larg, 40 ));
box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
box2.add(title2);
box2.add(Box.createHorizontalStrut(650));
emprunt.add(box2);
JLabel label3 = new JLabel("test");
JButton dispo = new JButton("Disponibilité");
dispo.addActionListener((ActionListener) this);
dispo.setPreferredSize(new Dimension(200, 30));
JPanel box1 = new JPanel();
box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
box1.add(dispo);
box1.add(Box.createHorizontalStrut(450));
box1.add(label3);
emp1.add(box1,BorderLayout.SOUTH);
listmateriel = new Vector();
for(int k =0 ;k <10 ; k++)
listmateriel .add(new JRadioButton(res.getString("nom"+k)));
......
ImageIcon newIcon = new ImageIcon(bi);
JLabel picture = new JLabel(newIcon);
picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
for(int i = 0 ; i < listmateriel.size() ;i++)
{
jplRadio.add((Component) listmateriel.elementAt(i));
}
JPanel imagebox = new JPanel();
imagebox.setLayout(new BorderLayout());
JScrollPane scroll3 = new JScrollPane(jplRadio);
scroll3.setPreferredSize(new Dimension(200, 30));
imagebox.add(scroll3, BorderLayout.WEST);
imagebox.add(picture, BorderLayout.CENTER);
emp1.add(imagebox,BorderLayout.CENTER);
JPanel box3 = new JPanel();
box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
JLabel title3 = new JLabel("Remplir les champs suivants:");
box3.add(title3);
box3.add(Box.createHorizontalStrut(650));
JPanel gridlayoutPanel2 = new JPanel();
........
JPanel util5 = new JPanel();
util5.setLayout(new GridLayout(0,1));
util5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel resul2 = new JLabel("bsfgbsdg");
JScrollPane scroll2 = new JScrollPane();
util5.add(resul2);
emprunt.add(emp1);
emprunt.add(box3);
emprunt.add(gridlayoutPanel2);
emprunt.add(util5);
menu.add("emprunts",emprunt);
//---------------------
getContentPane().add(menu);
pack();
setVisible(true);
}
}
GridLayout(0, 1) instead of BoxLayout is the solution thank you #trashgod

BoxLayout can't be shared. Trying to add multiple labels from an array into a BoxLayout Panel

I know there are plenty of BoxLayout questions on here, however I can't find one that fixes my problem. I need my scoreDescPanel to show each label directly below each other (like a list) however I'm having problems with BoxLayout. The priblem occurs on the line scoreDescPanel.add(lblScoreDesc[i]); at the bottom.
private JFrame frame;
private JPanel panel;
private JPanel dicePanel;
private JButton btnRoll;
private JButton[] btnDice = new JButton[5];
private JPanel mainPanel;
private JPanel scoreDescPanel;
private JPanel scoreBtnPanel;
private JLabel[] lblScoreDesc = new JLabel[20];
private JButton[] btnScore = new JButton[20];
private Yahtzee y = new Yahtzee();
public YahtzeeGUI(){
createWindow();
addButtonRoll();
addButtonDice();
addMainPanel();
addScoreDesc();
//addScoreCardUpper();
//addScoreCardLower();
frame.add(panel);
frame.setVisible(true);
}
public void createWindow(){
frame = new JFrame();
frame.setTitle("Yahtzee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,700);
panel = new JPanel(new BorderLayout());
dicePanel = new JPanel();
mainPanel = new JPanel();
scoreDescPanel = new JPanel();
}
public void addButtonRoll(){
btnRoll = new JButton ("Roll the Dice");
btnRoll.addActionListener(new RollHandler());
dicePanel.add (btnRoll);
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addButtonDice(){
for (int i = 0; i < btnDice.length; i++){
btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
btnDice[i].addActionListener(new HoldHandler());
dicePanel.add (btnDice[i]);
}
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addMainPanel(){
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.red);
panel.add(mainPanel, BorderLayout.CENTER);
}
public void addScoreDesc(){
lblScoreDesc[0] = new JLabel("UPPER SECTION");
lblScoreDesc[1] = new JLabel("Aces");
lblScoreDesc[2] = new JLabel("Twos");
lblScoreDesc[3] = new JLabel("Threes");
lblScoreDesc[4] = new JLabel("Fours");
lblScoreDesc[5] = new JLabel("Fives");
lblScoreDesc[6] = new JLabel("Sixes");
lblScoreDesc[7] = new JLabel("TOTAL SCORE");
lblScoreDesc[8] = new JLabel("BONUS");
lblScoreDesc[9] = new JLabel("TOTAL UPPER");
lblScoreDesc[10] = new JLabel("LOWER SECTION");
lblScoreDesc[11] = new JLabel("3 of a Kind");
lblScoreDesc[12] = new JLabel("4 of a Kind");
lblScoreDesc[13] = new JLabel("Full House");
lblScoreDesc[14] = new JLabel("Small Straight");
lblScoreDesc[15] = new JLabel("Large Straight");
lblScoreDesc[16] = new JLabel("Yahtzee!");
lblScoreDesc[17] = new JLabel("Chance");
lblScoreDesc[18] = new JLabel("TOTAL LOWER");
lblScoreDesc[19] = new JLabel("GRAND TOTAL");
mainPanel.add(scoreDescPanel, BorderLayout.WEST);
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
for(int i = 0; i < lblScoreDesc.length; i++){
scoreDescPanel.add(lblScoreDesc[i]);
}
}
BoxLayout doesn't allow a different target container from that on which the layout is being set, i.e.
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
should be
scoreDescPanel.setLayout(new BoxLayout(scoreDescPanel, BoxLayout.Y_AXIS));
Read: How to Use BoxLayout

How to call actionPerformed() within actionPerfomed()

I have a problem with my code below.
There are three menu items (Customer, Merchandize and Employee) in the Add Information Menu. Clicking on them (using addActionListener) should show various text fields/radio buttons/combo-boxes (which are required to fill in information) and a submit button.
After submitting the required information and clicking the submit button it should print the information to a Pop-up window.
I am stuck at the last point where it should call the actionPerformed method again and print the values to a Pop-up window. Can anyone help?
EDITED##I have edited my code. My problem starts from line no. 216 to line no. 225. When I click on the button "submit3" of the Customer menuitem, the pop-up appears but does not show the contents of the string that contains contents of "txt1". How do I pass the values of my components to actionPerformed so that it can print them in a new pop-up window?
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Retail extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();
public Retail()
{
super();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("Select Menu"));
p1.setPreferredSize(new Dimension(500, 100));
contentPane.add(p1,BorderLayout.NORTH);
p2.setBorder(new TitledBorder("Entry Screen"));
p2.setPreferredSize(new Dimension(500,500));
contentPane.add(p2,BorderLayout.CENTER);
p2.setLayout(new BorderLayout());
p1.add(menuBar);
menuBar.add(addmenu);
menuBar.add(delmenu);
menuBar.add(savemenu);
addmenu.add(emp);
addmenu.addSeparator();
addmenu.add(merc);
addmenu.addSeparator();
addmenu.add(cust);
addmenu.addSeparator();
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JButton submit1 = new JButton("Submit Employee Information");
JButton submit2 = new JButton("Submit Merchandise Information");
JButton submit3 = new JButton("Submit Customer Information");
if(e.getSource() == emp)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender:");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit1);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
submit1.addActionListener(this);
}
if(e.getSource() == merc)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit2);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
submit2.addActionListener(this);
}
if(e.getSource() == cust)
{
p2.removeAll();
p2.updateUI();
String[] states={"MA","AZ","CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8,1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1,2));
JLabel lb7= new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8,1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit3);
p2.add(p3,BorderLayout.WEST);
p2.add(p5,BorderLayout.EAST);
final String s;
s = txt1.getText();
submit3.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent args0)
{
JOptionPane.showMessageDialog(rootPane,s);
}
});
}
}
public static void main(String[] args)
{
Retail frame = new Retail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Retail Information");
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}
}
This last test:
if(e.getSource()==submit1)
will never succeed because submit1 is a JButton that you just constructed at the start of actionPerformed and thus cannot be the source for the current event.
Instead of constructing new layout components like this, I suggest that you use a CardLayout for p2 and just flip to the appropriate card in your action handler. That way you can register listeners for all the buttons once and you will get notified of all events properly.
Also, instead of having one giant actionPerformed that tests for the source, you should register separate ActionListeners for each UI component. That keeps the logic (and the code) a lot cleaner.
EDIT
For instance, instead of this:
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
you could do this:
emp.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// logic for click on emp button
}
});
merc.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// logic for click on merc button
}
});
// etc.
You then don't need implements ActionListener for your main class.
Then, if you use a CardLayout for p2, you can, at the start of your program, attach action listeners to every one of the interface elements. The logic for responding to any particular action then becomes much simpler--merely updating the appropriate UI elements and switching which "card" to show in p2. See the docs for CardLayout for more info on this last part.
You can either call .doClick() of appropriate next menu item
OR
define separete methods (e.g. doEmpAction() and doCustAction() ) which are called from the appropriate actionPerformed() methods and call them one from another. So doCustAction just call doEmpAction.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Retail extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();
public Retail() {
super();
contentPane.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setBorder(new TitledBorder("Select Menu"));
p1.setPreferredSize(new Dimension(500, 100));
contentPane.add(p1, BorderLayout.NORTH);
p2.setBorder(new TitledBorder("Entry Screen"));
p2.setPreferredSize(new Dimension(500, 500));
contentPane.add(p2, BorderLayout.CENTER);
p2.setLayout(new BorderLayout());
p1.add(menuBar);
menuBar.add(addmenu);
menuBar.add(delmenu);
menuBar.add(savemenu);
addmenu.add(emp);
addmenu.addSeparator();
addmenu.add(merc);
addmenu.addSeparator();
addmenu.add(cust);
addmenu.addSeparator();
emp.addActionListener(this);
merc.addActionListener(this);
cust.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
JButton submit1 = new JButton("Submit Employee Information");
JButton submit2 = new JButton("Submit Merchandise Information");
JButton submit3 = new JButton("Submit Customer Information");
if (e.getSource() == emp) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender:");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit1);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
submit1.addActionListener(this);//instead of this line use next line to add actionlistener.
}
if (e.getSource() == merc) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit2);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
submit2.addActionListener(this);//instead of this line use next line to add actionlistener.
}
if (e.getSource() == cust) {
p2.removeAll();
p2.updateUI();
String[] states = {"MA", "AZ", "CA"};
JLabel lb1 = new JLabel("First Name:");
final JTextField txt1 = new JTextField(12);
JLabel lb2 = new JLabel("Last Name:");
JTextField txt2 = new JTextField(12);
JLabel lb3 = new JLabel("Address:");
JTextField txt3 = new JTextField(12);
JLabel lb4 = new JLabel("City:");
JTextField txt4 = new JTextField(12);
JLabel lb5 = new JLabel("State");
JComboBox cb1 = new JComboBox(states);
JLabel lb6 = new JLabel("ZipCode");
JTextField txt5 = new JTextField(12);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(8, 1));
JPanel p4 = new JPanel();
p4.setLayout(new GridLayout(1, 2));
JLabel lb7 = new JLabel("Gender");
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
JLabel lb8 = new JLabel("Submit Information:");
JPanel p5 = new JPanel();
p5.setLayout(new GridLayout(8, 1));
p3.add(lb1);
p3.add(lb2);
p3.add(lb3);
p3.add(lb4);
p3.add(lb5);
p3.add(lb6);
p3.add(lb7);
p3.add(lb8);
p5.add(txt1);
p5.add(txt2);
p5.add(txt3);
p5.add(txt4);
p4.add(rb1);
p4.add(rb2);
p5.add(cb1);
p5.add(txt5);
p5.add(p4);
p5.add(submit3);
p2.add(p3, BorderLayout.WEST);
p2.add(p5, BorderLayout.EAST);
//submit3.addActionListener(this);//instead of this line use next line to add actionlistener.Commment this line.
submit3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("------>" + txt1.getText());
//JOptionPane.showMessageDialog(rootPane, txt1.getText());
new MyDialog(txt1.getText());
}
});
// submit3.addActionListener(new SubmitActionListener(txt1.getText()));//out action listener
}
if (e.getSource() == submit1) {
JOptionPane.showMessageDialog(rootPane, " button is clicked");
}
}
class MyDialog extends JDialog {
public MyDialog(String textbox1) {
JLabel label = new JLabel(textbox1);
add(label);
setModalityType(ModalityType.APPLICATION_MODAL);
setTitle("Info");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 200);
setVisible(true);
}
}
public static void main(String[] args) {
Retail frame = new Retail();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Retail Information");
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
}
}
This works for submit3 button.Modify others as your need.

Categories