I have a program to calculate the amount of paint needed to cover certain area. I have a button that that I have coded which is supposed to clear the text field but it is not working. I have used the exact same format in other programs and it has worked before but for some reason it is not working in this particular program. The error says cannot find symbol on the clearjbuttonactionperformed. Please help.
/*
FileName: PaintCalculator
Name: Bhattarai
Date: October.2014
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class PaintCalculator extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
DecimalFormat currency;
JTextField currencyJTextField;
JLabel roomWidthJLabel;
JTextField roomWidthJTextField;
JLabel roomLengthJLabel;
JTextField roomLengthJTextField;
JLabel numberofGallonsJLabel;
JTextField numberofGallonsJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalAmountJLabel;
JTextField totalAmountJTextField;
JButton enterJButton;
JButton clearJButton;
double roomWidth;
double roomLength;
double numberofGallons;
double subTotal;
double salesTax;
double totalAmount;
public PaintCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
// initialize components
// input
roomWidthJLabel = new JLabel ();
roomWidthJLabel.setBounds (50, 20, 150, 20);
roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJLabel.setText ("Enter Width of Room (Ft):");
roomWidthJLabel.setForeground(black);
roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomWidthJLabel);
roomWidthJTextField = new JTextField();
roomWidthJTextField.setBounds(230, 20, 100, 20);
roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomWidthJTextField.setForeground(black);
roomWidthJTextField.setBackground(white);
roomWidthJTextField.setEditable(true);
contentPane.add(roomWidthJTextField);
roomLengthJLabel = new JLabel();
roomLengthJLabel.setBounds(50, 70, 150, 20);
roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJLabel.setText("Enter Length of Room (Ft):");
roomLengthJLabel.setForeground(black);
roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(roomLengthJLabel);
roomLengthJTextField = new JTextField();
roomLengthJTextField.setBounds(230, 70, 100, 20);
roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomLengthJTextField.setForeground(black);
roomLengthJTextField.setBackground(white);
roomLengthJTextField.setEditable(true);
contentPane.add(roomLengthJTextField);
// outputs
numberofGallonsJLabel = new JLabel();
numberofGallonsJLabel.setBounds(50, 120, 150, 20);
numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJLabel.setText("Number of Gallons:");
numberofGallonsJLabel.setForeground(black);
numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(numberofGallonsJLabel);
numberofGallonsJTextField = new JTextField();
numberofGallonsJTextField.setBounds(230, 120, 100, 20);
numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberofGallonsJTextField.setForeground(black);
numberofGallonsJTextField.setBackground(white);
numberofGallonsJTextField.setEditable(false);
contentPane.add(numberofGallonsJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 170, 150, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(230, 170, 100, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 220, 150, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax (6%):");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(230, 220, 100, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalAmountJLabel = new JLabel();
totalAmountJLabel.setBounds(50, 270, 150, 20);
totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJLabel.setText("Total Sales:");
totalAmountJLabel.setForeground(black);
totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(totalAmountJLabel);
totalAmountJTextField = new JTextField();
totalAmountJTextField.setBounds(230, 270, 100, 20);
totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
totalAmountJTextField.setForeground(black);
totalAmountJTextField.setBackground(white);
totalAmountJTextField.setEditable(false);
contentPane.add(totalAmountJTextField);
// control
enterJButton = new JButton();
enterJButton.setBounds(50, 320, 100, 30);
enterJButton.setFont(new Font("Default", Font.BOLD, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(230, 320, 100, 30);
clearJButton.setFont(new Font("Default", Font.BOLD, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
// set properties of application’s window
setTitle("Paint Calculator"); // set title
setSize( 400, 400 ); // set window size
setVisible(true); // display window
}
// main method
public static void main(String[] args)
{
PaintCalculator application = new PaintCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getRoomWidthInput();
}
/* Try-Catch Method */
public void getRoomWidthInput()
{
try
{
roomWidth = Double.parseDouble(roomWidthJTextField.getText());
getRoomLengthInput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Width!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
}
}
public void getRoomLengthInput()
{
try
{
roomLength = Double.parseDouble(roomLengthJTextField.getText());
calculateNumberofGallons();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Length!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomLengthJTextField.setText("");
roomLengthJTextField.requestFocusInWindow();
}
}
public void calculateNumberofGallons()
{ //process data
final double Tax_Rate = .07;
final double priceperGallon = 24.95;
numberofGallons = (roomWidth * roomLength)/ 400;
subTotal = numberofGallons * priceperGallon;
salesTax = subTotal * Tax_Rate;
totalAmount = subTotal + salesTax;
//display results
currency = new DecimalFormat("$0.00");
numberofGallonsJTextField.setText("" + numberofGallons);
subTotalJTextField.setText("" + currency.format(subTotal));
salesTaxJTextField.setText("" + currency.format(salesTax));
totalAmountJTextField.setText("" + currency.format(totalAmount));
}
}
Related
I am creating a program where I can add and delete items. When added, I want the TOTAL panel to get values from itemprice_textfield when the button ADD is clicked.
private JFrame frame;
private JTextField itemname_textfield;
private JTextField itemprice_textfield;
private JButton add_button;
private JComboBox comboBox;
private JButton del_button;
private JLabel itemprice_label;
private JLabel itemname_label;
private JLabel lblNewLabel_3;
private JLabel totalamount_label;
private JTextField textField_2;
private double amount;
private JLabel bigger_itemname_label;
private JLabel bigger_itemprice_label;
private ArrayList <String> itemnames = new ArrayList();
private ArrayList <Double> itemprices = new ArrayList();
private JTable table;
//DefaultTableModel dtm;
//String header [] = new String [] {itemname_textfield, itemprice_textfield};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EntapaCCE103LabExam1_Frame1 window = new EntapaCCE103LabExam1_Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class lists{ // user defined method lists class
public String itemname;
public double price;
public lists(String itemname, double price)
{
this.itemname = itemname;
this.price = price;
}
public lists(JTextField textField) {
// TODO Auto-generated constructor stub
}
public lists(JTextField textField, JTextField textField_1) {
// TODO Auto-generated constructor stub
}
} // end
public EntapaCCE103LabExam1_Frame1() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
JLabel big_title = new JLabel("MY ORDERS");
big_title.setFont(new Font("Times New Roman", Font.PLAIN, 18));
big_title.setBounds(245, 11, 116, 52);
frame.getContentPane().add(big_title);
itemname_textfield = new JTextField(10); // name text
itemname_textfield.setBounds(412, 268, 109, 20);
frame.getContentPane().add(itemname_textfield);
itemname_textfield.setColumns(10);
itemprice_textfield = new JTextField(); // price text
itemprice_textfield.setBounds(412, 330, 109, 20);
frame.getContentPane().add(itemprice_textfield);
itemprice_textfield.setColumns(10);
add_button = new JButton("ADD ITEM");
add_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String getname = itemname_textfield.getText(); // used to get the input string from user to transfer to the combo box
comboBox.addItem(getname); // used to display the received string inputs
}
});
add_button.setFont(new Font("Serif", Font.PLAIN, 13));
add_button.setBounds(412, 361, 109, 23);
frame.getContentPane().add(add_button);
comboBox = new JComboBox();
comboBox.setBounds(412, 479, 109, 22);
frame.getContentPane().add(comboBox);
del_button = new JButton("DELETE ITEM");
del_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.removeItem(comboBox.getSelectedItem()); // used to delete the selected index in combo box.
}
});
del_button.setFont(new Font("Serif", Font.PLAIN, 11));
del_button.setBounds(412, 512, 109, 23);
frame.getContentPane().add(del_button);
itemprice_label = new JLabel("Enter Item Price:"); // label for item price
itemprice_label.setBounds(412, 311, 109, 14);
frame.getContentPane().add(itemprice_label); // used to display the text from label
itemname_label = new JLabel("Enter Item Name:"); // label for item name
itemname_label.setBounds(412, 249, 102, 14);
frame.getContentPane().add(itemname_label); // used to display the text from label
lblNewLabel_3 = new JLabel("OPERATION");
lblNewLabel_3.setFont(new Font("Sitka Subheading", Font.PLAIN, 17));
lblNewLabel_3.setBounds(419, 185, 102, 32);
frame.getContentPane().add(lblNewLabel_3);
String getname = itemname_textfield.getText();
totalamount_label = new JLabel("TOTAL: " + amount); // amount here
totalamount_label.setFont(new Font("Serif", Font.PLAIN, 13));
totalamount_label.setBounds(412, 130, 136, 20);
frame.getContentPane().add(totalamount_label);
bigger_itemname_label = new JLabel("ITEM NAME");
bigger_itemname_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemname_label.setBounds(84, 190, 96, 14);
frame.getContentPane().add(bigger_itemname_label);
bigger_itemprice_label = new JLabel("PRICE");
bigger_itemprice_label.setFont(new Font("Serif", Font.PLAIN, 14));
bigger_itemprice_label.setBounds(245, 192, 46, 14);
frame.getContentPane().add(bigger_itemprice_label);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(39, 225, 322, 285);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
}
}
But I don't know how or what to do since I am new to Java GUI.
Also, please tell me if there's something weird in my code so that I can make improvement.
So I am creating a program that converts binary, hexadecimal, and decimal values to each other. So a mass converter for the three data types. I have completed all the algorithms for the conversion however, in my GUI I have encountered a problem. The problem is that my buttons do not respond to the being clicked and therefore does not launch the code within the action listener.
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public menu() {
GUI();
}
public static void main(String args[]) {
new menu();
}
private void GUI() {
JFrame main = new JFrame("Converter App");
main.setSize(1000, 1000);
main.setVisible(true);
main.setLayout(null);
JPanel main_Panel = new JPanel();
main_Panel.setBounds(5,5, 1000, 275);
main_Panel.setBackground(Color.gray);
main_Panel.setVisible(true);
main_Panel.setLayout(null);
JRadioButton binary = new JRadioButton("Binary");
JRadioButton hexadecimal = new JRadioButton("Hexadecimal");
JRadioButton decimal = new JRadioButton("Decimal");
JRadioButton binary_Fraction = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed = new JRadioButton ("Signed Decimal");
ButtonGroup input = new ButtonGroup();
input.add(binary); input.add(decimal); input.add(decimal_Fraction); input.add(decimal_Signed);
input.add(hexadecimal); input.add(binary_Fraction); input.add(binary_Signed);
JLabel input_Ins = new JLabel ("Please choose your input: ");
JLabel output_Ins = new JLabel ("Please choose your output: ");
binary.setBounds(15, 50, 150, 30);
binary.setBackground(Color.white);
binary_Fraction.setBounds(15, 100, 150, 30);
binary_Fraction.setBackground(Color.white);
binary_Signed.setBounds(15, 150, 150, 30);
binary_Signed.setBackground(Color.white);
hexadecimal.setBounds(90, 200, 150, 30);
hexadecimal.setBackground(Color.white);
decimal.setBounds(180, 50, 150, 30);
decimal.setBackground(Color.white);
decimal_Fraction.setBounds(180, 100, 150, 30);
decimal_Fraction.setBackground(Color.white);
decimal_Signed.setBounds(180, 150, 150, 30);
decimal_Signed.setBackground(Color.white);
input_Ins.setBounds( 70, 10, 200, 20);
input_Ins.setBackground(Color.white);
input_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
input_Ins.setOpaque(true);
output_Ins.setBounds( 575, 10, 200, 20);
output_Ins.setBackground(Color.white);
output_Ins.setFont(new Font ("Comic Sans", Font.PLAIN, 15));
output_Ins.setOpaque(true);
JRadioButton binary_Out = new JRadioButton("Binary");
JRadioButton hexadecimal_Out = new JRadioButton("Hexadecimal");
JRadioButton decimal_Out = new JRadioButton("Decimal");
JRadioButton binary_Fraction_Out = new JRadioButton ("Binary Fraction");
JRadioButton binary_Signed_Out = new JRadioButton ("Signed Binary");
JRadioButton decimal_Fraction_Out = new JRadioButton ("Decimal Fraction");
JRadioButton decimal_Signed_Out = new JRadioButton ("Signed Decimal");
ButtonGroup output = new ButtonGroup();
output.add(binary_Out); output.add(binary_Fraction_Out); output.add(binary_Signed_Out); output.add(hexadecimal_Out);
output.add(decimal_Out); output.add(decimal_Fraction_Out); output.add(decimal_Signed_Out);
binary_Out.setBounds(500, 50, 150, 30);
binary_Out.setBackground(Color.white);
binary_Fraction_Out.setBounds(500, 100, 150, 30);
binary_Fraction_Out.setBackground(Color.white);
binary_Signed_Out.setBounds(500, 150, 150, 30);
binary_Signed_Out.setBackground(Color.white);
hexadecimal_Out.setBounds(600, 200, 150, 30);
hexadecimal_Out.setBackground(Color.white);
decimal_Out.setBounds(700, 50, 150, 30);
decimal_Out.setBackground(Color.white);
decimal_Fraction_Out.setBounds(700, 100, 150, 30);
decimal_Fraction_Out.setBackground(Color.white);
decimal_Signed_Out.setBounds(700, 150, 150, 30);
decimal_Signed_Out.setBackground(Color.white);
main.add(main_Panel);
main_Panel.revalidate();
main_Panel.repaint();
main_Panel.add(binary); main_Panel.add(hexadecimal); main_Panel.add(decimal); main_Panel.add(binary_Fraction); main_Panel.add(input_Ins); main_Panel.add(output_Ins);
main_Panel.add(decimal_Signed); main_Panel.add(decimal_Fraction); main_Panel.add(binary_Signed);
main_Panel.add(binary_Out); main_Panel.add(hexadecimal_Out); main_Panel.add(decimal_Out); main_Panel.add(binary_Fraction_Out);
main_Panel.add(decimal_Signed_Out); main_Panel.add(decimal_Fraction_Out); main_Panel.add(binary_Signed_Out);
JPanel work = new JPanel();
work.setBounds(0, 300, 600, 400);
work.setBackground(Color.gray);
JButton instructions = new JButton("Instructions");
JLabel user_Input = new JLabel("Please insert an appropriate input: ");
JLabel user_Output = new JLabel("Your converted output is: ");
JLabel instructions_For_Instructions = new JLabel("Please click on the instructions button first.");
JTextField user_in = new JTextField();
JLabel user_out = new JLabel();
JButton source = new JButton("Sources");
JButton convert = new JButton("Convert");
instructions.setBounds(400, 5, 150, 50);
instructions.setBackground( Color.white);
user_Input.setBounds(150, 125, 300, 50);
user_Input.setFont(new Font("Serif", Font.PLAIN, 20));
user_Input.setBackground(Color.white);
user_Input.setOpaque(true);
user_Output.setBounds( 150, 250, 300, 50 );
user_Output.setFont(new Font("Serif", Font.PLAIN, 20));
user_Output.setBackground(Color.white);
user_Output.setOpaque(true);
user_in.setBounds(500, 125, 200, 50);
user_in.setBackground( Color.white);
user_in.setFont(new Font("Serif", Font.PLAIN, 20));
user_in.setDocument(new JTextFieldLimit(10));
user_out.setBounds(500, 250, 200, 50);
user_out.setBackground( Color.white);
user_out.setOpaque(true);
source.setBounds(880, 5, 100, 50);
source.setBackground(Color.white);
instructions_For_Instructions.setBounds(100, 10, 270, 25);
instructions_For_Instructions.setBackground(Color.WHITE);
instructions_For_Instructions.setOpaque(true);
convert.setBounds(750, 200, 100, 50);
convert.setBackground(Color.white);
System.out.println("Yes");
convert.addActionListener(this);
binary.addActionListener(this);
source.addActionListener(this);
main.add(work);
work.add(instructions); work.add(user_Input); work.add(user_Output); work.add(user_in);
work.add(user_out); work.add(source); work.add(instructions_For_Instructions); work.add(convert);
work.setVisible(true);
work.setSize(1000, 400);
work.setLayout(null);
main.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == source) {
JOptionPane.showMessageDialog(null, "YES");
}
if (e.getSource() == convert) {
user_out.setText("Yes");
//binary_To_Dec_Converter();
}
}
public void binary_To_Dec_Converter() {
if (binary.isSelected() ) {
String input_From_User = user_in.getText();
String converted = Integer.toString(Binary_Converter.Binary_To_Dec(input_From_User));
System.out.println(converted);
user_out.setText("YES");
}
}
}
I am currently doing a diploma course in Java and for our project we have to create a Restaurant Calculator(See screenshot), I have the majority of the GUI done but I am having great difficulty with the SQL. I have never used SQL before so I'm not 100% certain what to do. I understand how it works but I can't even run the SQL file that the college supplied us in MySQL. Any help with this would be greatly appreciated as I am really struggling with this. I have had this GUI done the past 2 weeks and have been searching online for help but cannot get my head around it
Code is below...
import java.awt.Container;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RestaurantCalculator {
private static JTextField TotalText;
private static JTextField TaxText;
private static JTextField Table;
private static JTextField Sub;
private static JTextField WName;
private static JComboBox BevMenu;
private static JComboBox AppMenu;
private static JComboBox MainMenu;
private static JComboBox DesMenu;
public static void testFun(String myName){
TotalText.setText(myName);
}
public static void addComponentsToPane(Container pane){
pane.setLayout(null);
Border border = BorderFactory.createLoweredBevelBorder();
Border button = BorderFactory.createRaisedBevelBorder();
JLabel Welcome = new JLabel("Restaurant");
Welcome.setBounds(250, 5, 150, 100);
Welcome.setFont(new Font("Arial Black", Font.BOLD, 20));
JLabel Waiter = new JLabel("Waiter Information");
Waiter.setBounds(100, 100, 200, 100);
Waiter.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Tableno = new JLabel("Table number:");
Tableno.setBounds(120, 130, 200, 100);
Tableno.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel WaiterName = new JLabel("Waiter Name:");
WaiterName.setBounds(120, 160, 200, 100);
WaiterName.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MenuItems = new JLabel("Menu Items");
MenuItems.setBounds(100, 220, 150, 100);
MenuItems.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Beverage = new JLabel("Beverage:");
Beverage.setBounds(120, 250, 200, 100);
Beverage.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Appetizer = new JLabel("Appetizer:");
Appetizer.setBounds(120, 280, 200, 100);
Appetizer.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MainCourse = new JLabel("Main Course:");
MainCourse.setBounds(120, 310, 200, 100);
MainCourse.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Dessert = new JLabel("Dessert:");
Dessert.setBounds(120, 340, 200, 100);
Dessert.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel SubTotal = new JLabel("Sub Total:");
SubTotal.setBounds(120, 440, 200, 100);
SubTotal.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Tax = new JLabel("Tax:");
Tax.setBounds(120, 470, 200, 100);
Tax.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Total = new JLabel("Total:");
Total.setBounds(120, 500, 200, 100);
Total.setFont(new Font("Arial Black", Font.BOLD, 12));
Table = new JTextField(10);
Table.setBounds(290, 171, 150, 20);
Table.setBorder(border);
WName = new JTextField(11);
WName.setBounds(290, 201, 150, 20);
WName.setBorder(border);
Sub = new JTextField(10);
Sub.setBounds(260,482,150, 20);
Sub.setBorder(border);
Sub.setEditable(false);
TotalText = new JTextField(10);
TotalText.setBounds(260,545,150, 20);
TotalText.setBorder(border);
TotalText.setEditable(false);
TaxText = new JTextField(10);
TaxText.setBounds(260,515,150, 20);
TaxText.setBorder(border);
TaxText.setEditable(false);
BevMenu = new JComboBox();
BevMenu.setBounds(290, 295, 180, 20);
AppMenu = new JComboBox();
AppMenu.setBounds(290, 325, 180, 20);
MainMenu = new JComboBox();
MainMenu.setBounds(290, 355, 180, 20);
DesMenu = new JComboBox();
DesMenu.setBounds(290, 385, 180, 20);
JButton calculateJButton;
calculateJButton = new JButton( "Calculate Bill" );
calculateJButton.setBounds(250,425, 170, 30);
calculateJButton.setBorder(button);
pane.add(Welcome);
pane.add(Waiter);
pane.add(Tableno);
pane.add(WaiterName);
pane.add(MenuItems);
pane.add(Beverage);
pane.add(Appetizer);
pane.add(MainCourse);
pane.add(Dessert);
pane.add(SubTotal);
pane.add(Tax);
pane.add(Total);
pane.add(Table);
pane.add(WName);
pane.add(Sub);
pane.add(TaxText);
pane.add(TotalText);
pane.add(calculateJButton);
pane.add(BevMenu);
pane.add(AppMenu);
pane.add(MainMenu);
pane.add(DesMenu);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Restaurant Bill Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(600 + insets.left + insets.right,
700 + insets.top + insets.bottom);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
String theBest = "test";
testFun(theBest);
}
});
}
I just started learning about Java programming and have been trying to code a program that calculates the amount of pain that is needed for a certain area of space. The program runs but the clear button is not functioning. I have checked everything and tried everything but still can't find the problem.
Please help!
// declarations
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
DecimalFormat currency;
JTextField currencyJTextField;
JLabel roomWidthJLabel;
JTextField roomWidthJTextField;
JLabel roomLengthJLabel;
JTextField roomLengthJTextField;
JLabel numberofGallonsJLabel;
JTextField numberofGallonsJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel totalAmountJLabel;
JTextField totalAmountJTextField;
JButton enterJButton;
JButton clearJButton;
double roomWidth;
double roomLength;
double numberofGallons;
double subTotal;
double salesTax;
double totalAmount;
public PaintCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
// initialize components
// input
roomWidthJLabel = new JLabel ();
roomWidthJLabel.setBounds (50, 20, 150, 20);
roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJLabel.setText ("Enter Width of Room (Ft):");
roomWidthJLabel.setForeground(black);
roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(roomWidthJLabel);
roomWidthJTextField = new JTextField();
roomWidthJTextField.setBounds(230, 20, 100, 20);
roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomWidthJTextField.setForeground(black);
roomWidthJTextField.setBackground(white);
roomWidthJTextField.setEditable(true);
contentPane.add(roomWidthJTextField);
roomLengthJLabel = new JLabel();
roomLengthJLabel.setBounds(50, 70, 150, 20);
roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJLabel.setText("Enter Length of Room (Ft):");
roomLengthJLabel.setForeground(black);
roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(roomLengthJLabel);
roomLengthJTextField = new JTextField();
roomLengthJTextField.setBounds(230, 70, 100, 20);
roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
roomLengthJTextField.setForeground(black);
roomLengthJTextField.setBackground(white);
roomLengthJTextField.setEditable(true);
contentPane.add(roomLengthJTextField);
// outputs
numberofGallonsJLabel = new JLabel();
numberofGallonsJLabel.setBounds(50, 120, 150, 20);
numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJLabel.setText("Number of Gallons:");
numberofGallonsJLabel.setForeground(black);
numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(numberofGallonsJLabel);
numberofGallonsJTextField = new JTextField();
numberofGallonsJTextField.setBounds(230, 120, 100, 20);
numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberofGallonsJTextField.setForeground(black);
numberofGallonsJTextField.setBackground(white);
numberofGallonsJTextField.setEditable(false);
contentPane.add(numberofGallonsJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 170, 150, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total:");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setBounds(230, 170, 100, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(false);
contentPane.add(subTotalJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 220, 150, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax (6%):");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
salesTaxJTextField.setBounds(230, 220, 100, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(false);
contentPane.add(salesTaxJTextField);
totalAmountJLabel = new JLabel();
totalAmountJLabel.setBounds(50, 270, 150, 20);
totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJLabel.setText("Total Sales:");
totalAmountJLabel.setForeground(black);
totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
contentPane.add(totalAmountJLabel);
totalAmountJTextField = new JTextField();
totalAmountJTextField.setBounds(230, 270, 100, 20);
totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
totalAmountJTextField.setForeground(black);
totalAmountJTextField.setBackground(white);
totalAmountJTextField.setEditable(false);
contentPane.add(totalAmountJTextField);
// control
enterJButton = new JButton();
enterJButton.setBounds(50, 320, 100, 30);
enterJButton.setFont(new Font("Default", Font.BOLD, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(230, 320, 100, 30);
clearJButton.setFont(new Font("Default", Font.BOLD, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
// set properties of application’s window
setTitle("Paint Calculator"); // set title
setSize( 400, 400 ); // set window size
setVisible(true); // display window
}
// main method
public static void main(String[] args)
{
PaintCalculator application = new PaintCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
getRoomWidthInput();
}
/* Try-Catch Method */
public void getRoomWidthInput()
{
try
{
roomWidth = Double.parseDouble(roomWidthJTextField.getText());
getRoomLengthInput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Width!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomWidthJTextField.setText("");
roomWidthJTextField.requestFocusInWindow();
}
}
public void getRoomLengthInput()
{
try
{
roomLength = Double.parseDouble(roomLengthJTextField.getText());
calculateNumberofGallons();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Room Length!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
roomLengthJTextField.setText("");
roomLengthJTextField.requestFocusInWindow();
}
}
public void calculateNumberofGallons()
{ //process data
final double Tax_Rate = .07;
final double priceperGallon = 24.95;
numberofGallons = (roomWidth * roomLength)/ 400;
subTotal = numberofGallons * priceperGallon;
salesTax = subTotal * Tax_Rate;
totalAmount = subTotal + salesTax;
//display results
currency = new DecimalFormat("$0.00");
numberofGallonsJTextField.setText("" + numberofGallons);
subTotalJTextField.setText("" + currency.format(subTotal));
salesTaxJTextField.setText("" + currency.format(salesTax));
totalAmountJTextField.setText("" + currency.format(totalAmount));
}
}
I'm trying to refresh my table but it won't refresh. I tried using the fireTableDataChanged() as well as creating a new model but my Table will not budge. I can get the initial values in but I can't figure out how to refresh it.
HS = new Vector(Arrays.asList(finalHSArr));
SF = new Vector(Arrays.asList(finalFlagsArr));
I have two separate JTables that I am trying to refresh and these are the new values above. I've been trying for 6 hours now.
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;
}
}
System.out.println("NOW WERE HERE 103 " +Query.securityFlags.length);
//add data to table
int j=0;
int sizeOfFlags = Query.securityFlags.length;
//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length-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);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+": "+ 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(" ");
//convert to vectors
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
System.out.print(HS);
hsTableModel.fireTableDataChanged();
sfTableModel.fireTableDataChanged();
}
});
//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);
scrollPaneHS.setViewportView(hotelSecurityTable);
sfTableModel = new DefaultTableModel(SF,column);
securityFlagsTable = new JTable(sfTableModel);
scrollPaneSF.setViewportView(securityFlagsTable);
}
}
When you do
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
You change what HS and SF where point at, however, it does not change what, internally, the TableModel is pointing at.
You could use DefaultTableModel#setDataVector, but i might simpler to simply create new TableModels and set them to the JTables instead.
hsTableModel = new DefaultTableModel(HS, column);
hotelSecurityTable.setModel(hsTableModel);
sfTableModel = new DefaultTableModel(HS, column);
securityFlagsTable.setModel(sfTableModel);
As it's pretty much the same thing...
Updated
DefaultTableModel is expecting a Vector of Vectors, but you're supplying a Vector of Strings.
Try something like...
hsTableModel.setRowCount(0);
for (String row : HS) {
hsTableModel.addRow(new Object[]{row});
}
Instead...