Related
I want to remove labell10 and combo box - level when I change from programing book item in combo box category. I already done similar coding and they are working. But in this case form remain lower parts of l10 and level. I just want to display JTextField agegroup and change text of l8 to age group when item art book selected in combo box category. And similarly l8 must be language and slanguage combo box must be display and text field agegroup must be removed when category is story book. Also need text field planguage and combo box level add if category is programing box. And when we change from one category to another previous displayed items must be removed.
public static void add() {
JFrame f1 = new JFrame();
JLabel l1 = new JLabel("Add Book");
l1.setBounds(10, 10, 400, 25);
Font f = new Font("TimesRoman", Font.BOLD, 25);
l1.setFont(f);
f1.setSize(475, 700);
f1.setVisible(true);
f1.setLayout(null);
f1.add(l1);
JLabel l2 = new JLabel("Referance Number:");
l2.setBounds(10, 45, 300, 25);
f1.add(l2);
JTextField RNo = new JTextField();
RNo.setBounds(130, 45, 200, 25);
f1.add(RNo);
JLabel l3 = new JLabel("Title :");
l3.setBounds(10, 80, 350, 25);
f1.add(l3);
JTextField Title = new JTextField();
Title.setBounds(130, 80, 300, 25);
f1.add(Title);
JLabel l4 = new JLabel("Actual unit price:");
l4.setBounds(10, 115, 100, 25);
f1.add(l4);
JTextField AUPrice = new JTextField();
AUPrice.setBounds(130, 115, 60, 25);
f1.add(AUPrice);
JLabel l5 = new JLabel("QTY:");
l5.setBounds(230, 115, 60, 25);
f1.add(l5);
JTextField QTY = new JTextField();
QTY.setBounds(300, 115, 60, 25);
f1.add(QTY);
JLabel l6 = new JLabel("Description:");
l6.setBounds(10, 150, 100, 25);
f1.add(l6);
JTextArea Des = new JTextArea();
Des.setBounds(130, 185, 300, 200);
f1.add(Des);
JLabel l9 = new JLabel("Author:");
l9.setBounds(10, 395, 100, 25);
f1.add(l9);
JTextField Author = new JTextField();
Author.setBounds(130, 395, 300, 25);
f1.add(Author);
JLabel l7 = new JLabel("Category:");
l7.setBounds(10, 430, 100, 25);
f1.add(l7);
JComboBox booktype = new JComboBox();
booktype.setBounds(130, 430, 200, 25);
booktype.addItem("Story Book");
booktype.addItem("Programing Book");
booktype.addItem("Art Book");
f1.add(booktype);
JLabel l8 = new JLabel("language:");
l8.setBounds(10, 465, 200, 25);
f1.add(l8);
JTextField Agegroup = new JTextField();
Agegroup.setBounds(130, 465, 200, 25);
JComboBox slanguage = new JComboBox();
slanguage.addItem("sinhala");
slanguage.addItem("English");
slanguage.addItem("Tamil");
slanguage.setBounds(130, 465, 200, 25);
f1.add(slanguage);
JLabel l10 = new JLabel("Type:");
l10.setBounds(10, 500, 200, 25);
JComboBox level = new JComboBox();
level.addItem("Advance");
level.addItem("Medium");
level.addItem("Entry");
level.setBounds(130, 500, 200, 25);
JTextField planguage = new JTextField();
planguage.setBounds(130, 465, 200, 25);
booktype.addItemListener((ItemEvent e) -> {
if (e.getItem() == "Art Book") {
f1.remove(slanguage);
f1.remove(level);
f1.remove(l10);
f1.remove(planguage);
l8.setText("Age group");
f1.add(Agegroup);
} else if (e.getItem() == "Programing Book") {
l8.setText("language:");
f1.remove(Agegroup);
f1.remove(slanguage);
f1.add(l10);
f1.add(planguage);
f1.add(level);
} else {
f1.remove(Agegroup);
f1.remove(planguage);
f1.remove(level);
f1.remove(l10);
f1.add(slanguage);
l8.setText("language:");
}
});
}
Ans:-added repaint() method
public static void add() {
JFrame f1 = new JFrame();
JLabel l1 = new JLabel("Add Book");
l1.setBounds(10, 10, 400, 25);
Font f = new Font("TimesRoman", Font.BOLD, 25);
l1.setFont(f);
f1.setSize(475, 700);
f1.setLayout(null);
f1.add(l1);
JLabel l2 = new JLabel("Referance Number:");
l2.setBounds(10, 45, 300, 25);
f1.add(l2);
JTextField RNo = new JTextField();
RNo.setBounds(130, 45, 200, 25);
f1.add(RNo);
JLabel l3 = new JLabel("Title :");
l3.setBounds(10, 80, 350, 25);
f1.add(l3);
JTextField Title = new JTextField();
Title.setBounds(130, 80, 300, 25);
f1.add(Title);
JLabel l4 = new JLabel("Actual unit price:");
l4.setBounds(10, 115, 100, 25);
f1.add(l4);
JTextField AUPrice = new JTextField();
AUPrice.setBounds(130, 115, 60, 25);
f1.add(AUPrice);
JLabel l5 = new JLabel("QTY:");
l5.setBounds(230, 115, 60, 25);
f1.add(l5);
JTextField QTY = new JTextField();
QTY.setBounds(300, 115, 60, 25);
f1.add(QTY);
JLabel l6 = new JLabel("Description:");
l6.setBounds(10, 150, 100, 25);
f1.add(l6);
JTextArea Des = new JTextArea();
Des.setBounds(130, 185, 300, 200);
f1.add(Des);
JLabel l9 = new JLabel("Author:");
l9.setBounds(10, 395, 100, 25);
f1.add(l9);
JTextField Author = new JTextField();
Author.setBounds(130, 395, 300, 25);
f1.add(Author);
JLabel l7 = new JLabel("Category:");
l7.setBounds(10, 430, 100, 25);
f1.add(l7);
JComboBox booktype = new JComboBox();
booktype.setBounds(130, 430, 200, 25);
booktype.addItem("Story Book");
booktype.addItem("Programing Book");
booktype.addItem("Art Book");
f1.add(booktype);
JLabel l8 = new JLabel("language:");
l8.setBounds(10, 465, 200, 25);
f1.add(l8);
JTextField Agegroup = new JTextField();
Agegroup.setBounds(130, 465, 200, 25);
JComboBox slanguage = new JComboBox();
slanguage.addItem("sinhala");
slanguage.addItem("English");
slanguage.addItem("Tamil");
slanguage.setBounds(130, 465, 200, 25);
f1.add(slanguage);
JLabel l10 = new JLabel("Type:");
l10.setBounds(10, 500, 200, 25);
JComboBox level = new JComboBox();
level.addItem("Advance");
level.addItem("Medium");
level.addItem("Entry");
level.setBounds(130, 500, 200, 25);
JTextField planguage = new JTextField();
planguage.setBounds(130, 465, 200, 25);
booktype.addItemListener((ItemEvent e) -> {
if (e.getItem() == "Art Book") {
f1.remove(slanguage);
f1.remove(level);
f1.remove(l10);
f1.remove(planguage);
l8.setText("Age group");
f1.add(Agegroup);
f1.repaint();
} else if (e.getItem() == "Programing Book") {
l8.setText("language:");
f1.remove(Agegroup);
f1.remove(slanguage);
f1.add(l10);
f1.add(planguage);
f1.add(level);
f1.repaint();
} else {
f1.remove(Agegroup);
f1.remove(planguage);
f1.remove(level);
f1.remove(l10);
f1.add(slanguage);
l8.setText("language:");
f1.repaint();
}
});
f1.setVisible(true);
}
As in my last question, I declare that I'm very new to coding and Java is my only current language.
I'm building my first entirely independent project in eclipse, using a lot of Java imports.
It is a POS till system, it has 24 menu item buttons which are labelled by taking a string that holds null value to begin with.
I have an admin panel and from this panel I can update the title and price associated with the menu item button.
When I click "set" button relating to that particular menu item button, the string and double for item name and price update properly but title printed on JButton does not change.
What am I doing wrong/missing please?
Code follows:
public class Background implements ActionListener {
double menitprice1 = 0.0;
double menitprice2 = 0.0;
double menitprice3 = 0.0;
double menitprice4 = 0.0;
double menitprice5 = 0.0;
double menitprice6 = 0.0;
double menitprice7 = 0.0;
double menitprice8 = 0.0;
double menitprice9 = 0.0;
double menitprice10 = 0.0;
double menitprice11 = 0.0;
double menitprice12 = 0.0;
double menitprice13 = 0.0;
double menitprice14 = 0.0;
double menitprice15 = 0.0;
double menitprice16 = 0.0;
double menitprice17 = 0.0;
double menitprice18 = 0.0;
double menitprice19 = 0.0;
double menitprice20 = 0.0;
double menitprice21 = 0.0;
double menitprice22 = 0.0;
double menitprice23 = 0.0;
double menitprice24 = 0.0;
String menit1;
String menit2;
String menit3;
String menit4;
String menit5;
String menit6;
String menit7;
String menit8;
String menit9;
String menit10;
String menit11;
String menit12;
String menit13;
String menit14;
String menit15;
String menit16;
String menit17;
String menit18;
String menit19;
String menit20;
String menit21;
String menit22;
String menit23;
String menit24;
String adminPassword = "Password";
int splitParty;
JFrame mainFrame = new JFrame();
JFrame adminFrame = new JFrame();
JFrame payFrame = new JFrame();
JFrame splitFrame = new JFrame();
JFrame loginFrame = new JFrame();
Panel mainPanel = new Panel();
Panel adminPanel = new Panel();
Panel payPanel = new Panel();
Panel splitPanel = new Panel();
Panel loginPanel = new Panel();
JTextArea summaryList = new JTextArea();
JTextArea summaryListTotal = new JTextArea();
JTextArea dueAmount = new JTextArea();
JTextArea splitAmount = new JTextArea("How many people?");
JTextArea totalSplits = new JTextArea();
JTextArea splitInput = new JTextArea();
JTextArea logInPassword = new JTextArea();
JTextArea itemsList = new JTextArea("Menu item:");
JTextArea list1 = new JTextArea("Item 1");
JTextArea list2 = new JTextArea("Item 2");
JTextArea list3 = new JTextArea("Item 3");
JTextArea list4 = new JTextArea("Item 4");
JTextArea list5 = new JTextArea("Item 5");
JTextArea list6 = new JTextArea("Item 6");
JTextArea list7 = new JTextArea("Item 7");
JTextArea list8 = new JTextArea("Item 8");
JTextArea list9 = new JTextArea("Item 9");
JTextArea list10 = new JTextArea("Item 10");
JTextArea list11 = new JTextArea("Item 11");
JTextArea list12 = new JTextArea("Item 12");
JTextArea list13 = new JTextArea("Item 13");
JTextArea list14 = new JTextArea("Item 14");
JTextArea list15 = new JTextArea("Item 15");
JTextArea list16 = new JTextArea("Item 16");
JTextArea list17 = new JTextArea("Item 17");
JTextArea list18 = new JTextArea("Item 18");
JTextArea list19 = new JTextArea("Item 19");
JTextArea list20 = new JTextArea("Item 20");
JTextArea list21 = new JTextArea("Item 21");
JTextArea list22 = new JTextArea("Item 22");
JTextArea list23 = new JTextArea("Item 23");
JTextArea list24 = new JTextArea("Item 24");
JTextArea itemIs = new JTextArea("Item Name");
JTextArea price = new JTextArea("Price");
JTextArea setItem1 = new JTextArea(menit1);
JTextArea setItem2 = new JTextArea(menit2);
JTextArea setItem3 = new JTextArea(menit3);
JTextArea setItem4 = new JTextArea(menit4);
JTextArea setItem5 = new JTextArea(menit5);
JTextArea setItem6 = new JTextArea(menit6);
JTextArea setItem7 = new JTextArea(menit7);
JTextArea setItem8 = new JTextArea(menit8);
JTextArea setItem9 = new JTextArea(menit9);
JTextArea setItem10 = new JTextArea(menit10);
JTextArea setItem11 = new JTextArea(menit11);
JTextArea setItem12 = new JTextArea(menit12);
JTextArea setItem13 = new JTextArea(menit13);
JTextArea setItem14 = new JTextArea(menit14);
JTextArea setItem15 = new JTextArea(menit15);
JTextArea setItem16 = new JTextArea(menit16);
JTextArea setItem17 = new JTextArea(menit17);
JTextArea setItem18 = new JTextArea(menit18);
JTextArea setItem19 = new JTextArea(menit19);
JTextArea setItem20 = new JTextArea(menit20);
JTextArea setItem21 = new JTextArea(menit21);
JTextArea setItem22 = new JTextArea(menit22);
JTextArea setItem23 = new JTextArea(menit23);
JTextArea setItem24 = new JTextArea(menit24);
JTextArea setPrice1 = new JTextArea(String.valueOf(menitprice1));
JTextArea setPrice2 = new JTextArea(String.valueOf(menitprice2));
JTextArea setPrice3 = new JTextArea(String.valueOf(menitprice3));
JTextArea setPrice4 = new JTextArea(String.valueOf(menitprice4));
JTextArea setPrice5 = new JTextArea(String.valueOf(menitprice5));
JTextArea setPrice6 = new JTextArea(String.valueOf(menitprice6));
JTextArea setPrice7 = new JTextArea(String.valueOf(menitprice7));
JTextArea setPrice8 = new JTextArea(String.valueOf(menitprice8));
JTextArea setPrice9 = new JTextArea(String.valueOf(menitprice9));
JTextArea setPrice10 = new JTextArea(String.valueOf(menitprice10));
JTextArea setPrice11 = new JTextArea(String.valueOf(menitprice11));
JTextArea setPrice12 = new JTextArea(String.valueOf(menitprice12));
JTextArea setPrice13 = new JTextArea(String.valueOf(menitprice13));
JTextArea setPrice14 = new JTextArea(String.valueOf(menitprice14));
JTextArea setPrice15 = new JTextArea(String.valueOf(menitprice15));
JTextArea setPrice16 = new JTextArea(String.valueOf(menitprice16));
JTextArea setPrice17 = new JTextArea(String.valueOf(menitprice17));
JTextArea setPrice18 = new JTextArea(String.valueOf(menitprice18));
JTextArea setPrice19 = new JTextArea(String.valueOf(menitprice19));
JTextArea setPrice20 = new JTextArea(String.valueOf(menitprice20));
JTextArea setPrice21 = new JTextArea(String.valueOf(menitprice21));
JTextArea setPrice22 = new JTextArea(String.valueOf(menitprice22));
JTextArea setPrice23 = new JTextArea(String.valueOf(menitprice23));
JTextArea setPrice24 = new JTextArea(String.valueOf(menitprice24));
JButton menuItem1 = new JButton(menit1 + " " + menitprice1);
JButton menuItem2 = new JButton(menit2 + " " + menitprice2);
JButton menuItem3 = new JButton(menit3 + " " + menitprice3);
JButton menuItem4 = new JButton(menit4 + " " + menitprice4);
JButton menuItem5 = new JButton(menit5 + " " + menitprice5);
JButton menuItem6 = new JButton(menit6 + " " + menitprice6);
JButton menuItem7 = new JButton(menit7 + " " + menitprice7);
JButton menuItem8 = new JButton(menit8 + " " + menitprice8);
JButton menuItem9 = new JButton(menit9 + " " + menitprice9);
JButton menuItem10 = new JButton(menit10 + " " + menitprice10);
JButton menuItem11 = new JButton(menit11 + " " + menitprice11);
JButton menuItem12 = new JButton(menit12 + " " + menitprice12);
JButton menuItem13 = new JButton(menit13 + " " + menitprice13);
JButton menuItem14 = new JButton(menit14 + " " + menitprice14);
JButton menuItem15 = new JButton(menit15 + " " + menitprice15);
JButton menuItem16 = new JButton(menit16 + " " + menitprice16);
JButton menuItem17 = new JButton(menit17 + " " + menitprice17);
JButton menuItem18 = new JButton(menit18 + " " + menitprice18);
JButton menuItem19 = new JButton(menit19 + " " + menitprice19);
JButton menuItem20 = new JButton(menit20 + " " + menitprice20);
JButton menuItem21 = new JButton(menit21 + " " + menitprice21);
JButton menuItem22 = new JButton(menit22 + " " + menitprice22);
JButton menuItem23 = new JButton(menit23 + " " + menitprice23);
JButton menuItem24 = new JButton(menit24 + " " + menitprice24);
JButton adminButton = new JButton("Admin Control");
JButton payButton = new JButton("Payment");
JButton cashOpt = new JButton("Cash");
JButton cardOpt = new JButton("Card");
JButton splitOpt = new JButton("Split Payment");
JButton splitCancel = new JButton("Cancel");
JButton splitSubmit = new JButton("Split it");
JButton passwordSubmit = new JButton("Log in");
JButton menItSet1 = new JButton("set");
JButton menItSet2 = new JButton("set");
JButton menItSet3 = new JButton("set");
JButton menItSet4 = new JButton("set");
JButton menItSet5 = new JButton("set");
JButton menItSet6 = new JButton("set");
JButton menItSet7 = new JButton("set");
JButton menItSet8 = new JButton("set");
JButton menItSet9 = new JButton("set");
JButton menItSet10 = new JButton("set");
JButton menItSet11 = new JButton("set");
JButton menItSet12 = new JButton("set");
JButton menItSet13 = new JButton("set");
JButton menItSet14 = new JButton("set");
JButton menItSet15 = new JButton("set");
JButton menItSet16 = new JButton("set");
JButton menItSet17 = new JButton("set");
JButton menItSet18 = new JButton("set");
JButton menItSet19 = new JButton("set");
JButton menItSet20 = new JButton("set");
JButton menItSet21 = new JButton("set");
JButton menItSet22 = new JButton("set");
JButton menItSet23 = new JButton("set");
JButton menItSet24 = new JButton("set");
public Background() {
Border listBorder = BorderFactory.createLineBorder(Color.RED, 3);
Font listFont = new Font("ariel", Font.BOLD, 27);
// Admin Log in
loginFrame.setVisible(false);
loginFrame.setTitle("Indy POS: Admin log in");
loginFrame.setSize(400, 200);
loginFrame.setResizable(false);
loginFrame.add(loginPanel);
loginPanel.setBackground(Color.lightGray);
loginPanel.setLayout(null);
loginPanel.add(logInPassword);
logInPassword.setBounds(100, 50, 200, 50);
logInPassword.setBorder(listBorder);
logInPassword.setFont(listFont);
logInPassword.setBackground(Color.white);
logInPassword.setForeground(Color.lightGray);
loginPanel.add(passwordSubmit);
passwordSubmit.setBounds(125, 112, 150, 50);
passwordSubmit.addActionListener(this);
// Admin area
adminFrame.setVisible(false);
adminFrame.setTitle("Indy POS: Admin Control");
adminFrame.setSize(850, 750);
adminFrame.add(adminPanel);
adminPanel.setBackground(Color.lightGray);
adminPanel.setLayout(null);
adminPanel.add(list1);
list1.setEditable(false);
list1.setBackground(Color.lightGray);
list1.setBorder(listBorder);
list1.setBounds(50, 25, 50, 25);
adminPanel.add(list2);
list2.setEditable(false);
list2.setBackground(Color.lightGray);
list2.setBorder(listBorder);
list2.setBounds(50, 50, 50, 25);
adminPanel.add(list3);
list3.setEditable(false);
list3.setBackground(Color.lightGray);
list3.setBorder(listBorder);
list3.setBounds(50, 75, 50, 25);
adminPanel.add(list4);
list4.setEditable(false);
list4.setBackground(Color.lightGray);
list4.setBorder(listBorder);
list4.setBounds(50, 100, 50, 25);
adminPanel.add(list5);
list5.setEditable(false);
list5.setBackground(Color.lightGray);
list5.setBorder(listBorder);
list5.setBounds(50, 125, 50, 25);
adminPanel.add(list6);
list6.setEditable(false);
list6.setBackground(Color.lightGray);
list6.setBorder(listBorder);
list6.setBounds(50, 150, 50, 25);
adminPanel.add(list7);
list7.setEditable(false);
list7.setBackground(Color.lightGray);
list7.setBorder(listBorder);
list7.setBounds(50, 175, 50, 25);
adminPanel.add(list8);
list8.setEditable(false);
list8.setBackground(Color.lightGray);
list8.setBorder(listBorder);
list8.setBounds(50, 200, 50, 25);
adminPanel.add(list9);
list9.setEditable(false);
list9.setBackground(Color.lightGray);
list9.setBorder(listBorder);
list9.setBounds(50, 225, 50, 25);
adminPanel.add(list10);
list10.setEditable(false);
list10.setBackground(Color.lightGray);
list10.setBorder(listBorder);
list10.setBounds(50, 250, 50, 25);
adminPanel.add(list11);
list11.setEditable(false);
list11.setBackground(Color.lightGray);
list11.setBorder(listBorder);
list11.setBounds(50, 275, 50, 25);
adminPanel.add(list12);
list12.setEditable(false);
list12.setBackground(Color.lightGray);
list12.setBorder(listBorder);
list12.setBounds(50, 300, 50, 25);
adminPanel.add(list13);
list13.setEditable(false);
list13.setBackground(Color.lightGray);
list13.setBorder(listBorder);
list13.setBounds(50, 325, 50, 25);
adminPanel.add(list14);
list14.setEditable(false);
list14.setBackground(Color.lightGray);
list14.setBorder(listBorder);
list14.setBounds(50, 350, 50, 25);
adminPanel.add(list15);
list15.setEditable(false);
list15.setBackground(Color.lightGray);
list15.setBorder(listBorder);
list15.setBounds(50, 375, 50, 25);
adminPanel.add(list16);
list16.setEditable(false);
list16.setBackground(Color.lightGray);
list16.setBorder(listBorder);
list16.setBounds(50, 400, 50, 25);
adminPanel.add(list17);
list17.setEditable(false);
list17.setBackground(Color.lightGray);
list17.setBorder(listBorder);
list17.setBounds(50, 425, 50, 25);
adminPanel.add(list18);
list18.setEditable(false);
list18.setBackground(Color.lightGray);
list18.setBorder(listBorder);
list18.setBounds(50, 450, 50, 25);
adminPanel.add(list19);
list19.setEditable(false);
list19.setBackground(Color.lightGray);
list19.setBorder(listBorder);
list19.setBounds(50, 475, 50, 25);
adminPanel.add(list20);
list20.setEditable(false);
list20.setBackground(Color.lightGray);
list20.setBorder(listBorder);
list20.setBounds(50, 500, 50, 25);
adminPanel.add(list21);
list21.setEditable(false);
list21.setBackground(Color.lightGray);
list21.setBorder(listBorder);
list21.setBounds(50, 525, 50, 25);
adminPanel.add(list22);
list22.setEditable(false);
list22.setBackground(Color.lightGray);
list22.setBorder(listBorder);
list22.setBounds(50, 550, 50, 25);
adminPanel.add(list23);
list23.setEditable(false);
list23.setBackground(Color.lightGray);
list23.setBorder(listBorder);
list23.setBounds(50, 575, 50, 25);
adminPanel.add(list24);
list24.setEditable(false);
list24.setBackground(Color.lightGray);
list24.setBorder(listBorder);
list24.setBounds(50, 600, 50, 25);
adminPanel.add(itemIs);
itemIs.setEditable(false);
itemIs.setBackground(Color.lightGray);
itemIs.setBounds(150, 0, 150, 25);
adminPanel.add(price);
price.setEditable(false);
price.setBackground(Color.lightGray);
price.setBounds(350, 0, 150, 25);
adminPanel.add(setItem1);
setItem1.setBorder(listBorder);
setItem1.setBounds(150, 25, 150, 25);
adminPanel.add(setItem2);
setItem2.setBorder(listBorder);
setItem2.setBounds(150, 50, 150, 25);
adminPanel.add(setItem3);
setItem3.setBorder(listBorder);
setItem3.setBounds(150, 75, 150, 25);
adminPanel.add(setItem4);
setItem4.setBorder(listBorder);
setItem4.setBounds(150, 100, 150, 25);
adminPanel.add(setItem5);
setItem5.setBorder(listBorder);
setItem5.setBounds(150, 125, 150, 25);
adminPanel.add(setItem6);
setItem6.setBorder(listBorder);
setItem6.setBounds(150, 150, 150, 25);
adminPanel.add(setItem7);
setItem7.setBorder(listBorder);
setItem7.setBounds(150, 175, 150, 25);
adminPanel.add(setItem8);
setItem8.setBorder(listBorder);
setItem8.setBounds(150, 200, 150, 25);
adminPanel.add(setItem9);
setItem9.setBorder(listBorder);
setItem9.setBounds(150, 225, 150, 25);
adminPanel.add(setItem10);
setItem10.setBorder(listBorder);
setItem10.setBounds(150, 250, 150, 25);
adminPanel.add(setItem11);
setItem11.setBorder(listBorder);
setItem11.setBounds(150, 275, 150, 25);
adminPanel.add(setItem12);
setItem12.setBorder(listBorder);
setItem12.setBounds(150, 300, 150, 25);
adminPanel.add(setItem13);
setItem13.setBorder(listBorder);
setItem13.setBounds(150, 325, 150, 25);
adminPanel.add(setItem14);
setItem14.setBorder(listBorder);
setItem14.setBounds(150, 350, 150, 25);
adminPanel.add(setItem15);
setItem15.setBorder(listBorder);
setItem15.setBounds(150, 375, 150, 25);
adminPanel.add(setItem16);
setItem16.setBorder(listBorder);
setItem16.setBounds(150, 400, 150, 25);
adminPanel.add(setItem17);
setItem17.setBorder(listBorder);
setItem17.setBounds(150, 425, 150, 25);
adminPanel.add(setItem18);
setItem18.setBorder(listBorder);
setItem18.setBounds(150, 450, 150, 25);
adminPanel.add(setItem19);
setItem19.setBorder(listBorder);
setItem19.setBounds(150, 475, 150, 25);
adminPanel.add(setItem20);
setItem20.setBorder(listBorder);
setItem20.setBounds(150, 500, 150, 25);
adminPanel.add(setItem21);
setItem21.setBorder(listBorder);
setItem21.setBounds(150, 525, 150, 25);
adminPanel.add(setItem22);
setItem22.setBorder(listBorder);
setItem22.setBounds(150, 550, 150, 25);
adminPanel.add(setItem23);
setItem23.setBorder(listBorder);
setItem23.setBounds(150, 575, 150, 25);
adminPanel.add(setItem24);
setItem24.setBorder(listBorder);
setItem24.setBounds(150, 600, 150, 25);
adminPanel.add(setPrice1);
setPrice1.setBorder(listBorder);
setPrice1.setBounds(350, 25, 150, 25);
adminPanel.add(setPrice2);
setPrice2.setBorder(listBorder);
setPrice2.setBounds(350, 50, 150, 25);
adminPanel.add(setPrice3);
setPrice3.setBorder(listBorder);
setPrice3.setBounds(350, 75, 150, 25);
adminPanel.add(setPrice4);
setPrice4.setBorder(listBorder);
setPrice4.setBounds(350, 100, 150, 25);
adminPanel.add(setPrice5);
setPrice5.setBorder(listBorder);
setPrice5.setBounds(350, 125, 150, 25);
adminPanel.add(setPrice6);
setPrice6.setBorder(listBorder);
setPrice6.setBounds(350, 150, 150, 25);
adminPanel.add(setPrice7);
setPrice7.setBorder(listBorder);
setPrice7.setBounds(350, 175, 150, 25);
adminPanel.add(setPrice8);
setPrice8.setBorder(listBorder);
setPrice8.setBounds(350, 200, 150, 25);
adminPanel.add(setPrice9);
setPrice9.setBorder(listBorder);
setPrice9.setBounds(350, 225, 150, 25);
adminPanel.add(setPrice10);
setPrice10.setBorder(listBorder);
setPrice10.setBounds(350, 250, 150, 25);
adminPanel.add(setPrice11);
setPrice11.setBorder(listBorder);
setPrice11.setBounds(350, 275, 150, 25);
adminPanel.add(setPrice12);
setPrice12.setBorder(listBorder);
setPrice12.setBounds(350, 300, 150, 25);
adminPanel.add(setPrice13);
setPrice13.setBorder(listBorder);
setPrice13.setBounds(350, 325, 150, 25);
adminPanel.add(setPrice14);
setPrice14.setBorder(listBorder);
setPrice14.setBounds(350, 350, 150, 25);
adminPanel.add(setPrice15);
setPrice15.setBorder(listBorder);
setPrice15.setBounds(350, 375, 150, 25);
adminPanel.add(setPrice16);
setPrice16.setBorder(listBorder);
setPrice16.setBounds(350, 400, 150, 25);
adminPanel.add(setPrice17);
setPrice17.setBorder(listBorder);
setPrice17.setBounds(350, 425, 150, 25);
adminPanel.add(setPrice18);
setPrice18.setBorder(listBorder);
setPrice18.setBounds(350, 450, 150, 25);
adminPanel.add(setPrice19);
setPrice19.setBorder(listBorder);
setPrice19.setBounds(350, 475, 150, 25);
adminPanel.add(setPrice20);
setPrice20.setBorder(listBorder);
setPrice20.setBounds(350, 500, 150, 25);
adminPanel.add(setPrice21);
setPrice21.setBorder(listBorder);
setPrice21.setBounds(350, 525, 150, 25);
adminPanel.add(setPrice22);
setPrice22.setBorder(listBorder);
setPrice22.setBounds(350, 550, 150, 25);
adminPanel.add(setPrice23);
setPrice23.setBorder(listBorder);
setPrice23.setBounds(350, 575, 150, 25);
adminPanel.add(setPrice24);
setPrice24.setBorder(listBorder);
setPrice24.setBounds(350, 600, 150, 25);
adminPanel.add(menItSet1);
menItSet1.setBounds(550, 25, 100, 25);
adminPanel.add(menItSet2);
menItSet2.setBounds(550, 50, 100, 25);
adminPanel.add(menItSet3);
menItSet3.setBounds(550, 75, 100, 25);
adminPanel.add(menItSet4);
menItSet4.setBounds(550, 100, 100, 25);
adminPanel.add(menItSet5);
menItSet5.setBounds(550, 125, 100, 25);
adminPanel.add(menItSet6);
menItSet6.setBounds(550, 150, 100, 25);
adminPanel.add(menItSet7);
menItSet7.setBounds(550, 175, 100, 25);
adminPanel.add(menItSet8);
menItSet8.setBounds(550, 200, 100, 25);
adminPanel.add(menItSet9);
menItSet9.setBounds(550, 225, 100, 25);
adminPanel.add(menItSet10);
menItSet10.setBounds(550, 250, 100, 25);
adminPanel.add(menItSet11);
menItSet11.setBounds(550, 275, 100, 25);
adminPanel.add(menItSet12);
menItSet12.setBounds(550, 300, 100, 25);
adminPanel.add(menItSet13);
menItSet13.setBounds(550, 325, 100, 25);
adminPanel.add(menItSet14);
menItSet14.setBounds(550, 350, 100, 25);
adminPanel.add(menItSet15);
menItSet15.setBounds(550, 375, 100, 25);
adminPanel.add(menItSet16);
menItSet16.setBounds(550, 400, 100, 25);
adminPanel.add(menItSet17);
menItSet17.setBounds(550, 425, 100, 25);
adminPanel.add(menItSet18);
menItSet18.setBounds(550, 450, 100, 25);
adminPanel.add(menItSet19);
menItSet19.setBounds(550, 475, 100, 25);
adminPanel.add(menItSet20);
menItSet20.setBounds(550, 500, 100, 25);
adminPanel.add(menItSet21);
menItSet21.setBounds(550, 525, 100, 25);
adminPanel.add(menItSet22);
menItSet22.setBounds(550, 550, 100, 25);
adminPanel.add(menItSet23);
menItSet23.setBounds(550, 575, 100, 25);
adminPanel.add(menItSet24);
menItSet24.setBounds(550, 600, 100, 25);
menItSet1.addActionListener(this);
menItSet2.addActionListener(this);
menItSet3.addActionListener(this);
menItSet4.addActionListener(this);
menItSet5.addActionListener(this);
menItSet6.addActionListener(this);
menItSet7.addActionListener(this);
menItSet8.addActionListener(this);
menItSet9.addActionListener(this);
menItSet10.addActionListener(this);
menItSet11.addActionListener(this);
menItSet12.addActionListener(this);
menItSet13.addActionListener(this);
menItSet14.addActionListener(this);
menItSet15.addActionListener(this);
menItSet16.addActionListener(this);
menItSet17.addActionListener(this);
menItSet18.addActionListener(this);
menItSet19.addActionListener(this);
menItSet20.addActionListener(this);
menItSet21.addActionListener(this);
menItSet22.addActionListener(this);
menItSet23.addActionListener(this);
menItSet24.addActionListener(this);
//Payment area
payFrame.setVisible(false);
payFrame.setTitle("Indy POS: Payment");
payFrame.setSize(600, 300);
payFrame.setResizable(false);
payFrame.add(payPanel);
payPanel.setBackground(Color.lightGray);
payPanel.setLayout(null);
payPanel.add(dueAmount);
dueAmount.setBounds(100, 50, 250, 100);
dueAmount.setBorder(listBorder);
dueAmount.setFont(listFont);
dueAmount.setBackground(Color.BLACK);
dueAmount.setForeground(Color.WHITE);
dueAmount.setEditable(false);
payPanel.add(totalSplits);
totalSplits.setBounds(400, 50, 150, 100);
totalSplits.setBorder(listBorder);
totalSplits.setFont(listFont);
totalSplits.setBackground(Color.BLACK);
totalSplits.setForeground(Color.WHITE);
totalSplits.setEditable(false);
payPanel.add(cashOpt);
cashOpt.setBounds(100, 200, 100, 50);
payPanel.add(cardOpt);
cardOpt.setBounds(250, 200, 100, 50);
payPanel.add(splitOpt);
splitOpt.setBounds(400, 200, 150, 50);
cashOpt.addActionListener(this);
cardOpt.addActionListener(this);
splitOpt.addActionListener(this);
// Split area
splitFrame.setVisible(false);
splitFrame.setTitle("Indy POS: Split Order");
splitFrame.setSize(450, 200);
splitFrame.setResizable(false);
splitFrame.add(splitPanel);
splitPanel.setLayout(null);
splitPanel.setBackground(Color.lightGray);
splitPanel.add(splitAmount);
splitAmount.setBounds(20, 25, 260, 47);
splitAmount.setEditable(false);
splitAmount.setBackground(Color.lightGray);
splitAmount.setFont(listFont);
splitAmount.setForeground(Color.BLACK);
splitPanel.add(splitInput);
splitInput.setBorder(listBorder);
splitInput.setFont(listFont);
splitInput.setBackground(Color.BLACK);
splitInput.setForeground(Color.WHITE);
splitInput.setBounds(100, 75, 100, 50);
splitPanel.add(splitSubmit);
splitSubmit.setBounds(250, 75, 100, 50);
splitPanel.add(splitCancel);
splitCancel.setBounds(100, 175, 100, 50);
// Cash area
// Card area
// Main area
mainFrame.setVisible(true);
mainFrame.setTitle("Indy POS");
mainFrame.setSize(1000, 750);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(mainPanel);
mainPanel.setBackground(Color.lightGray);
mainPanel.setLayout(null);
mainPanel.add(summaryList);
summaryList.setBounds(100, 50, 300, 575);
summaryList.setBorder(listBorder);
summaryList.setBackground(Color.BLACK);
summaryList.setEditable(false);
summaryList.setFont(listFont);
summaryList.setForeground(Color.WHITE);
mainPanel.add(summaryListTotal);
summaryListTotal.setBounds(100, 650, 150, 50);
summaryListTotal.setBorder(listBorder);
summaryListTotal.setBackground(Color.BLACK);
summaryListTotal.setEditable(false);
summaryListTotal.setFont(listFont);
summaryListTotal.setForeground(Color.WHITE);
mainPanel.add(payButton);
payButton.setBounds(300, 650, 100, 50);
mainPanel.add(menuItem1);
menuItem1.setBounds(500, 50, 100, 50);
mainPanel.add(menuItem2);
menuItem2.setBounds(650, 50, 100, 50);
mainPanel.add(menuItem3);
menuItem3.setBounds(800, 50, 100, 50);
mainPanel.add(menuItem4);
menuItem4.setBounds(500, 125, 100, 50);
mainPanel.add(menuItem5);
menuItem5.setBounds(650, 125, 100, 50);
mainPanel.add(menuItem6);
menuItem6.setBounds(800, 125, 100, 50);
mainPanel.add(menuItem7);
menuItem7.setBounds(500, 200, 100, 50);
mainPanel.add(menuItem8);
menuItem8.setBounds(650, 200, 100, 50);
mainPanel.add(menuItem9);
menuItem9.setBounds(800, 200, 100, 50);
mainPanel.add(menuItem10);
menuItem10.setBounds(500, 275, 100, 50);
mainPanel.add(menuItem11);
menuItem11.setBounds(650, 275, 100, 50);
mainPanel.add(menuItem12);
menuItem12.setBounds(800, 275, 100, 50);
mainPanel.add(menuItem13);
menuItem13.setBounds(500, 350, 100, 50);
mainPanel.add(menuItem14);
menuItem14.setBounds(650, 350, 100, 50);
mainPanel.add(menuItem15);
menuItem15.setBounds(800, 350, 100, 50);
mainPanel.add(menuItem16);
menuItem16.setBounds(500, 425, 100, 50);
mainPanel.add(menuItem17);
menuItem17.setBounds(650, 425, 100, 50);
mainPanel.add(menuItem18);
menuItem18.setBounds(800, 425, 100, 50);
mainPanel.add(menuItem19);
menuItem19.setBounds(500, 500, 100, 50);
mainPanel.add(menuItem20);
menuItem20.setBounds(650, 500, 100, 50);
mainPanel.add(menuItem21);
menuItem21.setBounds(800, 500, 100, 50);
mainPanel.add(menuItem22);
menuItem22.setBounds(500, 575, 100, 50);
mainPanel.add(menuItem23);
menuItem23.setBounds(650, 575, 100, 50);
mainPanel.add(menuItem24);
menuItem24.setBounds(800, 575, 100, 50);
mainPanel.add(adminButton);
adminButton.setBounds(750, 650, 150, 50);
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
menuItem4.addActionListener(this);
menuItem5.addActionListener(this);
menuItem6.addActionListener(this);
menuItem7.addActionListener(this);
menuItem8.addActionListener(this);
menuItem9.addActionListener(this);
menuItem10.addActionListener(this);
menuItem11.addActionListener(this);
menuItem12.addActionListener(this);
menuItem13.addActionListener(this);
menuItem14.addActionListener(this);
menuItem15.addActionListener(this);
menuItem16.addActionListener(this);
menuItem17.addActionListener(this);
menuItem18.addActionListener(this);
menuItem19.addActionListener(this);
menuItem20.addActionListener(this);
menuItem21.addActionListener(this);
menuItem22.addActionListener(this);
menuItem23.addActionListener(this);
menuItem24.addActionListener(this);
adminButton.addActionListener(this);
payButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == payButton){
payFrame.setVisible(true);
}
if (source == splitOpt){
splitFrame.setVisible(true);
}
if (source == adminButton){
loginFrame.setVisible(true);
splitFrame.setVisible(false);
payFrame.setVisible(false);
}
if (source == passwordSubmit){
if (logInPassword.getText().equals(adminPassword)){
loginFrame.setVisible(false);
adminFrame.setVisible(true);
}
else logInPassword.setText(null);
logInPassword.append("Incorrect");
}
if (source == menItSet1){
menit1.equals(setItem1.getText());
menitprice1 = Double.parseDouble(setPrice1.getText());
}
}
}
You could just create a String and then pass the value from that String into your action listener. Please find underneath a local String (should be an Instance, but for testing I have made a local one). And then I have passed it to the button.
if (source == menItSet1){
String text = setItem1.getText();
menitprice1 = Double.parseDouble(setPrice1.getText());
menuItem1.setText(text + " " + menitprice1);
}
How to append a scroll pane for my window?
The program compiles properly, but the scroll pane for the window is not created. I really don't know why this is happening. I defined JScrollPane and even implemented it with scrollPane = new JScrollPane
Where is my mistake?
Below is my code:
import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
public class RegForm extends JFrame implements ItemListener{
JLabel l0,li,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13;
JButton b1,b2,b3,b4,b5;
JTextField t1,t2,t3,t4,t5,t6,t7,t8;
JTextArea a1,a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton( " Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox (" C ");
JCheckBox cb2 = new JCheckBox (" C++ ");
JCheckBox cb3 = new JCheckBox (" Java ");
JCheckBox cb4 = new JCheckBox (" Oracle ");
JCheckBox cb5 = new JCheckBox (" Android ");
JCheckBox cb6 = new JCheckBox (" iOS ");
JCheckBox cb7 = new JCheckBox (" Web Designing ");
JCheckBox cb8 = new JCheckBox (" .Net ");
JCheckBox cb9 = new JCheckBox (" Same as Contact Address ");
JScrollPane scrollPane = new JScrollPane();
RegForm()
{
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian",Font.ITALIC,20);
l0.setFont(f0);
l0.setBounds(600,10,250,50);
scrollPane.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting",Font.PLAIN,17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10,50,150,30);
scrollPane.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style",Font.PLAIN,12);
l1.setFont(f1);
l1.setBounds(10,70,100,50);
scrollPane.add(l1);
t1 = new JTextField(20);
t1.setBounds(165,85,140,20);
scrollPane.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10,100,150,50);
scrollPane.add(l2);
t2 = new JTextField(20);
t2.setBounds(165,115,140,20);
scrollPane.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15,130,120,50);
scrollPane.add(l3);
t3 = new JTextField(20);
t3.setBounds(165,145,140,20);
scrollPane.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15,160,150,50);
scrollPane.add(l4);
t4 = new JTextField(20);
t4.setBounds(165,175,140,20);
scrollPane.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10,190,100,50);
scrollPane.add(l5);
t5 = new JTextField(20);
t5.setBounds(165,205,140,20);
scrollPane.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10,220,150,50);
scrollPane.add(l6);
t6 = new JTextField(20);
t6.setBounds(165,235,140,20);
scrollPane.add(t6);
l7 = new JLabel(" * DD / MM / YYYY" );
Font f2 = new Font(" Comic Sans MS ",Font.ITALIC,12);
l7.setFont(f2);
l7.setBounds(10,260,150,50);
scrollPane.add(l7);
for(int j=1;j<=31;j++)
dd.addItem(new Integer(j));
dd.setBounds(165,275,47,20);
scrollPane.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212,275,90,20);
scrollPane.add(mm);
mm.addItemListener(this);
for(int i=1990;i<=2016;i++)
yyyy.addItem(new Integer(i));
yyyy.setBounds(302,275,70,20);
scrollPane.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15,290,50,50);
scrollPane.add(l8);
t8 = new JTextField(10);
t8.setBounds(165,305,50,20);
scrollPane.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15,320,120,50);
scrollPane.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165,335,100,20);
scrollPane.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15,360,80,50);
scrollPane.add(l10);
rb1.setBounds(165,365,80,39);
rb2.setBounds(250,365,80,39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
scrollPane.add(rb1);
scrollPane.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15,450,150,50);
scrollPane.add(l11);
cb1.setBounds(165,390,100,50);
scrollPane.add(cb1);
cb2.setBounds(285,390,100,50);
scrollPane.add(cb2);
cb3.setBounds(165,425,100,50);
scrollPane.add(cb3);
cb4.setBounds(285,425,100,50);
scrollPane.add(cb4);
cb5.setBounds(165,460,100,50);
scrollPane.add(cb5);
cb6.setBounds(285,460,100,50);
scrollPane.add(cb6);
cb7.setBounds(165,495,100,50);
scrollPane.add(cb7);
cb8.setBounds(285,495,100,50);
scrollPane.add(cb8);
cb9.setBounds(15,630,200,50);
scrollPane.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15,550,150,50);
scrollPane.add(l12);
a1 = new JTextArea (5,20);
a1.setBounds(165,545,250,80);
scrollPane.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15,675,150,50);
scrollPane.add(l13);
a2 = new JTextArea (5,20);
a2.setBounds(165,680,250,80);
scrollPane.add(a2);
cb9.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource() == yyyy){
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016-y));
t8.setEditable(false);
}
if(cb9.isSelected()){
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String[] args)
{
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(10,10,100,100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(scrollPane);
rf.setContentPane(contentPane);
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
scrollPane.add(l11);
Never add components directly to a scroll pane.
l1.setBounds(10,70,100,50);
Don't use setBounds(...). It is the job of the layout manager to set the size/location of the component.
The basic logic would be:
JPanel panel = new JPanel(); // set your layout manager for the panel.
panel.add( someComponent );
panel.add( anotherComponent );
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );
Read the Swing Tutorial for Swing basics. Every section in the tutorial has working examples. Maybe start with the section on How to Use Scroll Panes.
This works, but you'll need to layout the components better..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegForm extends JFrame implements ItemListener {
JLabel l0, li, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13;
JButton b1, b2, b3, b4, b5;
JTextField t1, t2, t3, t4, t5, t6, t7, t8;
JTextArea a1, a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton(" Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox(" C ");
JCheckBox cb2 = new JCheckBox(" C++ ");
JCheckBox cb3 = new JCheckBox(" Java ");
JCheckBox cb4 = new JCheckBox(" Oracle ");
JCheckBox cb5 = new JCheckBox(" Android ");
JCheckBox cb6 = new JCheckBox(" iOS ");
JCheckBox cb7 = new JCheckBox(" Web Designing ");
JCheckBox cb8 = new JCheckBox(" .Net ");
JCheckBox cb9 = new JCheckBox(" Same as Contact Address ");
JScrollPane scrollPane;
JPanel panel = new JPanel(new GridLayout(0,1));
RegForm() {
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian", Font.ITALIC, 20);
l0.setFont(f0);
l0.setBounds(600, 10, 250, 50);
panel.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting", Font.PLAIN, 17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10, 50, 150, 30);
panel.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style", Font.PLAIN, 12);
l1.setFont(f1);
l1.setBounds(10, 70, 100, 50);
panel.add(l1);
t1 = new JTextField(20);
t1.setBounds(165, 85, 140, 20);
panel.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10, 100, 150, 50);
panel.add(l2);
t2 = new JTextField(20);
t2.setBounds(165, 115, 140, 20);
panel.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15, 130, 120, 50);
panel.add(l3);
t3 = new JTextField(20);
t3.setBounds(165, 145, 140, 20);
panel.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15, 160, 150, 50);
panel.add(l4);
t4 = new JTextField(20);
t4.setBounds(165, 175, 140, 20);
panel.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10, 190, 100, 50);
panel.add(l5);
t5 = new JTextField(20);
t5.setBounds(165, 205, 140, 20);
panel.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10, 220, 150, 50);
panel.add(l6);
t6 = new JTextField(20);
t6.setBounds(165, 235, 140, 20);
panel.add(t6);
l7 = new JLabel(" * DD / MM / YYYY");
Font f2 = new Font(" Comic Sans MS ", Font.ITALIC, 12);
l7.setFont(f2);
l7.setBounds(10, 260, 150, 50);
panel.add(l7);
for (int j = 1; j <= 31; j++) {
dd.addItem(new Integer(j));
}
dd.setBounds(165, 275, 47, 20);
panel.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212, 275, 90, 20);
panel.add(mm);
mm.addItemListener(this);
for (int i = 1990; i <= 2016; i++) {
yyyy.addItem(new Integer(i));
}
yyyy.setBounds(302, 275, 70, 20);
panel.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15, 290, 50, 50);
panel.add(l8);
t8 = new JTextField(10);
t8.setBounds(165, 305, 50, 20);
panel.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15, 320, 120, 50);
panel.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165, 335, 100, 20);
panel.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15, 360, 80, 50);
panel.add(l10);
rb1.setBounds(165, 365, 80, 39);
rb2.setBounds(250, 365, 80, 39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
panel.add(rb1);
panel.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15, 450, 150, 50);
panel.add(l11);
cb1.setBounds(165, 390, 100, 50);
panel.add(cb1);
cb2.setBounds(285, 390, 100, 50);
panel.add(cb2);
cb3.setBounds(165, 425, 100, 50);
panel.add(cb3);
cb4.setBounds(285, 425, 100, 50);
panel.add(cb4);
cb5.setBounds(165, 460, 100, 50);
panel.add(cb5);
cb6.setBounds(285, 460, 100, 50);
panel.add(cb6);
cb7.setBounds(165, 495, 100, 50);
panel.add(cb7);
cb8.setBounds(285, 495, 100, 50);
panel.add(cb8);
cb9.setBounds(15, 630, 200, 50);
panel.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15, 550, 150, 50);
panel.add(l12);
a1 = new JTextArea(5, 20);
a1.setBounds(165, 545, 250, 80);
panel.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15, 675, 150, 50);
panel.add(l13);
a2 = new JTextArea(5, 20);
a2.setBounds(165, 680, 250, 80);
panel.add(a2);
cb9.addItemListener(this);
scrollPane = new JScrollPane(panel);
add(scrollPane);
//add(panel);
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == yyyy) {
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016 - y));
t8.setEditable(false);
}
if (cb9.isSelected()) {
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String[] args) {
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
/*JScrollPane panel = new JScrollPane();
panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setBounds(10, 10, 100, 100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(panel);
rf.setContentPane(contentPane);*/
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
Notes:
As mentioned by #camickr, setting the bounds of the components won't work reliably. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Please try to solve programming errors before you've got to dozens of components! For simple testing of layouts with many components, just add dummy components in a loop. To put that another way: For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
Safer to use logical font names that specific one, e.g. Algerian will only work on systems with that font installed!
I'm having a hard time removing data from my JTable. I've tried JTable .repaint(), setRowCount(0) , also model.removeRow(). I created print statements to see the row count when using setRowCount and the rows do become 0 however my table does not update. This is the last part of this project I need to implement. I'm not sure why my table isn't updating because I am altering the model with removeRow as well as setRowCount. If you guys could guide me in the right direction I would appreciate it.
public class TemplateGui extends JFrame {
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField;
private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
private static String finalFlags="",finalHS="",columnToConvert="Result";
private Vector<String> SF,HS,column;
private JTable hotelSecurityTable,securityFlagsTable;
private DefaultTableModel hsTableModel,sfTableModel;
public TemplateGui(){
super("Galaxy Template Generator V1.0");
//column name
column = new Vector(Arrays.asList(columnToConvert));
getContentPane().setForeground(new Color(0, 0, 0));
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(51, 51, 51));
//radio buttons
JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnNewRadioButton.setBackground(Color.WHITE);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
getContentPane().add(rdbtnNewRadioButton);
final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPropertyUser.setBackground(Color.WHITE);
buttonGroup.add(rdbtnPropertyUser);
rdbtnPropertyUser.setBounds(222, 38, 127, 34);
getContentPane().add(rdbtnPropertyUser);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 35, 53, 34);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
lblHotelSecurity.setBounds(10, 144, 189, 23);
lblHotelSecurity.setBackground(new Color(204, 204, 204));
lblHotelSecurity.setOpaque(true);
getContentPane().add(lblHotelSecurity);
JLabel label = new JLabel("Security Flags (S F)");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 13));
label.setBounds(222, 144, 372, 23);
label.setBackground(new Color(204, 204, 204));
label.setOpaque(true);
getContentPane().add(label);
JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEnterTemplateCode.setBounds(10, 9, 175, 23);
getContentPane().add(lblEnterTemplateCode);
JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
lblSelectUserRole.setForeground(new Color(255, 255, 255));
lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectUserRole.setBounds(222, 13, 195, 14);
getContentPane().add(lblSelectUserRole);
//Submit button action
Button button = new Button("Generate Template");
button.setFont(new Font("Dialog", Font.BOLD, 12));
button.setBackground(new Color(102, 255, 102));
button.setForeground(Color.BLACK);
button.setBounds(467, 83, 127, 41);
getContentPane().add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Query excell = new Query();
//get template text
String template = textField.getText().toUpperCase();
System.out.println(template);
if(rdbtnPropertyUser.isSelected()){
try {
//property user was selected
excell.runProcess(1);
System.out.println("you selected Property user");
} catch (IOException e) {
e.printStackTrace();
}
}
else{
try {
//Central User was selected
excell.runProcess(2);
System.out.println("you selected central user");
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("NOW WERE HERE");
//get static variables from Excel Query
for(int i = 0; i< Query.sortedGF.length; i++)
{
if(Query.sortedGF[i].contains(template)){
sortedRoles_Flags =Query.sortedGF[i].split(" ");
System.out.println("THIS RAN"+" :"+i);
break;
}
}
//add data to table
int j=1;
int sizeOfFlags = Query.securityFlags.length;
//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length+1-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+"+";
System.out.println("Hotel security:"+" "+finalHS);
}
}
//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags+1);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+"\t\t: "+ sortedRoles_Flags[i]+" + ";
j++;
}
//Leave open just incase they would prefer a text file for template in which case we just write it
System.out.println(finalFlags);
System.out.println(finalHS);
//Convert to String Arrays in order to add to our JTable
finalFlagsArr= finalFlags.split("\\+");
finalHSArr = finalHS.split("\\+");
for(int i=0; i <finalHSArr.length;i++)
finalHSArr[i].replaceAll(" ","");
hsTableModel.setRowCount(0);
for (String row : finalHSArr) {
hsTableModel.addRow(new Object[]{row});
}
sfTableModel.setRowCount(0);
for (String row : finalFlagsArr) {
sfTableModel.addRow(new Object[]{row});
}
}
});
//scroll panes for flags
JScrollPane scrollPaneHS = new JScrollPane();
scrollPaneHS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneHS.setBounds(10, 170, 189, 185);
getContentPane().add(scrollPaneHS);
JScrollPane scrollPaneSF = new JScrollPane();
scrollPaneSF.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneSF.setBounds(222, 170, 372, 187);
getContentPane().add(scrollPaneSF);
//tables for updates
hsTableModel = new DefaultTableModel(HS,column);
hotelSecurityTable = new JTable(hsTableModel);
hotelSecurityTable.setBackground(new Color(255, 255, 255));
scrollPaneHS.setViewportView(hotelSecurityTable);
sfTableModel = new DefaultTableModel(SF,column);
securityFlagsTable = new JTable(sfTableModel);
scrollPaneSF.setViewportView(securityFlagsTable);
}
}
Update
my latest attempt. It seemed to work but not the second time around.
if(hsTableModel.getRowCount() !=0 && sfTableModel.getRowCount() !=0){
for(int i=0; i<hsTableModel.getRowCount();i++)
hsTableModel.removeRow(i);
for(int i=0; i<sfTableModel.getRowCount();i++)
sfTableModel.removeRow(i);
sfTableModel.fireTableRowsDeleted(0, sfTableModel.getRowCount()-1);
hsTableModel.fireTableRowsDeleted(0, hsTableModel.getRowCount()-1);
}
when using setRowCount and the rows do become 0 however my table does not update.
Since you are using a DefaultTableModel, the setRowCount(0) is the proper method to use to clear the model of all the entries. The should automatically repaint itself.
Create a simple SSCCE that demonstrates your problem. The SSCCE should consist of a JTable with data, and then a JButton to invoke the setRowCount() method.
Prove to yourself that the basic concept works and then determine why you real code doesn't. If the table does not repaint then it usually means you are invoking the setRowCount() method on a TableModel that has NOT be added to the JTable that is visible on the GUI.
Also, don't:
use Button. You should be using a JButton
use a null layout. Swing was designed to be used with Layout Managers.
setRowCount(0) works.
I was forgetting to reset the string Values of
finalHS =""; finalFlags=""; Therefore the table was printing out the values stored in the variable.
I had the variables obtain their data through += Method as seen below.
Next time I will SSCCE before posting.
for(int i=0;i< sortedRoles_Flags.length+1-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+"+";
System.out.println("Hotel security:"+" "+finalHS);
}
}
//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags+1);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+"\t\t: "+ sortedRoles_Flags[i]+" + ";
j++;
}
I have this GUI:
I would like after I enter a number in the Length of hot tub text box for that number to be automatically entered into the Width of hot tub text box, but only if the Round Tub radio button is selected.
public void createHotTubs()
{
hotTubs = new JPanel();
hotTubs.setLayout(null);
labelTubStatus = new JTextArea(6, 30);
hotTubs.add(labelTubStatus);
JLabel lengthLabel = new JLabel(
"Length of hot tub(ft):");
lengthLabel.setBounds(10, 15, 260, 20);
hotTubs.add(lengthLabel);
hotTubLengthText = new JTextField();
hotTubLengthText.setBounds(180, 15, 150, 20);
hotTubs.add(hotTubLengthText);
JLabel widthLabel = new JLabel(
"Width of hot tub(ft):");
widthLabel.setBounds(10, 40, 260, 20);
hotTubs.add(widthLabel);
hotTubWidthText = new JTextField();
hotTubWidthText.setBounds(180, 40, 150, 20);
hotTubs.add(hotTubWidthText);
JLabel depthLabel = new JLabel(
"Average depth the hot tub(ft):");
depthLabel.setBounds(10, 65, 260, 20);
hotTubs.add(depthLabel);
hotTubDepthText = new JTextField();
hotTubDepthText.setBounds(180, 65, 150, 20);
hotTubs.add(hotTubDepthText);
JLabel volumeLabel = new JLabel("The hot tub volume is:(ft ^3");
volumeLabel.setBounds(10, 110, 260, 20);
hotTubs.add(volumeLabel);
hotTubVolumeText = new JTextField();
hotTubVolumeText.setBounds(180, 110, 150, 20);
hotTubVolumeText.setEditable(false);
hotTubs.add(hotTubVolumeText);
final JRadioButton rdbtnRoundTub = new JRadioButton("Round Tub");
rdbtnRoundTub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
hotTubWidthText.setEditable(false);
}
});
rdbtnRoundTub.setSelected(true);
rdbtnRoundTub.setBounds(79, 150, 109, 23);
hotTubs.add(rdbtnRoundTub);
JRadioButton rdbtnOvalTub = new JRadioButton("Oval Tub");
rdbtnOvalTub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
hotTubWidthText.setEditable(true);
}
});
rdbtnOvalTub.setBounds(201, 150, 109, 23);
hotTubs.add(rdbtnOvalTub);
ButtonGroup radioBtnGroup = new ButtonGroup();
radioBtnGroup.add(rdbtnRoundTub);
radioBtnGroup.add(rdbtnOvalTub);
JButton btnCalculateVlmn = new JButton("Calculate Volume");
btnCalculateVlmn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
double width = 0, length = 0, depth = 0, volume = 0;
String lengthString, widthString, depthString;
lengthString = hotTubLengthText.getText();
widthString = hotTubWidthText.getText();
depthString = hotTubDepthText.getText();
depth = Double.valueOf(depthString);
length = Double.valueOf(lengthString);
width = Double.valueOf(widthString);
try
{
if (rdbtnRoundTub.isSelected())
{
volume = length * width * depth;
}
else
{
volume = Math.PI * length * width / 4 * depth;
}
DecimalFormat formatter = new DecimalFormat("#,###,###.###");
hotTubVolumeText.setText("" + formatter.format(volume));
}
catch (NumberFormatException e)
{
labelTubStatus
.setText("Enter all three numbers!!");
}
}
});
Add a focus listener to your length text field when it loses focus and the round tub is selected, compute and set the the width.