can not refer a value to a radiobutton - java

i write a convert temperature code using radiobutton, but when i use actionListener to print the value to a label, it show "Cannot refer to the non-final local variable valueto defined in an enclosing scope"
can some one show me the problem and how to fix it? (also sorry for my English)
the Radiobutton
JRadioButton Cbutton = new JRadioButton("Celsius");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
contentPane.add(Cbutton);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
i use this to convert
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
Edit : i declare valueto at the bottom of the class
double value = 0;
double valueto = 0;
double F = 0, C = 0, K = 0;
it ask me to add final to "double valueto", but when i add it the valueto in "if" become error
The Whole code
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class CtoKtoF extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CtoKtoF frame = new CtoKtoF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #return
*/
public CtoKtoF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 303);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
double value = 0 ;
double valueto = 0;
double F = 0, C = 0, K = 0;
Border border = LineBorder.createGrayLineBorder();//make border for Jlabel
final JLabel label = new JLabel("New label");
label.setFont(new Font("Tahoma", Font.PLAIN, 12));
label.setBounds(10, 228, 220, 22);
label.setBorder(border);
contentPane.add(label);
JLabel lblNewLabel = new JLabel("Convert from");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel.setBounds(10, 11, 92, 22);
contentPane.add(lblNewLabel);
JRadioButton Fbutton = new JRadioButton("Fahrenheit");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
//Fbutton.setActionCommand("F");
contentPane.add(Fbutton);
JRadioButton Cbutton = new JRadioButton("Celcius");
Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton.setBounds(121, 40, 109, 23);
//Cbutton.setActionCommand("C");
contentPane.add(Cbutton);
JRadioButton Kbutton = new JRadioButton("Kelvin");
Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton.setBounds(232, 40, 109, 23);
//Kbutton.setActionCommand("K");
contentPane.add(Kbutton);
JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature");
lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEnterNumericTemperature.setBounds(10, 70, 238, 22);
contentPane.add(lblEnterNumericTemperature);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
textField.setBounds(10, 103, 220, 22);
textField.setText(String.valueOf(value));
contentPane.add(textField);
textField.setColumns(10);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton2.setBounds(10, 165, 109, 23);
//Fbutton2.setActionCommand("F2");
contentPane.add(Fbutton2);
JRadioButton Cbutton2 = new JRadioButton("Celcius");
Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton2.setBounds(121, 165, 109, 23);
//Cbutton2.setActionCommand("C2");
contentPane.add(Cbutton2);
JRadioButton Kbutton2 = new JRadioButton("Kelvin");
Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton2.setBounds(232, 165, 109, 23);
//Kbutton2.setActionCommand("K2");
contentPane.add(Kbutton2);
ButtonGroup group1 = new ButtonGroup();
group1.add(Fbutton);
group1.add(Kbutton);
group1.add(Cbutton);
ButtonGroup group2 = new ButtonGroup();
group2.add(Fbutton2);
group2.add(Cbutton2);
group2.add(Kbutton2);
/*Fbutton.addActionListener(this);
Cbutton.addActionListener(this);
Kbutton.addActionListener(this);
Fbutton2.addActionListener(this);
Cbutton2.addActionListener(this);
Kbutton2.addActionListener(this);*/
//boolean checked = Fbutton.getState();
JLabel lblConvertTo = new JLabel("Convert to");
lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConvertTo.setBounds(10, 136, 92, 22);
contentPane.add(lblConvertTo);
JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is");
lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblComparableTemperatureIs.setBounds(10, 195, 238, 22);
contentPane.add(lblComparableTemperatureIs);
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
}
}

Related

popup JTable when key pressed to Search

I made an application that has some TextFields. When I click to search an item it popups JTable that contains a list of items. But the problem is how we can do it. I have tried many times but I failed please help in my project.
this is my code that i have tried
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;
import javax.swing.JFrame;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import com.alee.laf.WebLookAndFeel;
import com.toedter.calendar.JDateChooser;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
public class DailySales3 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField ItemID;
private JTextField ItemName;
private JTextField Stock;
private JTextField Qnty;
private JTextField UnitPrice;
private JTextField Tax;
private JTextField Amount;
private JTextField Discount;
private Connection con;
private String query,auto;
private PreparedStatement PStat;
private ResultSet res;
private DefaultTableModel model,model1;
private JTable table_2;
private JScrollPane scrollPane;
private double AmountGet=0.0,AmountCal=0.0,AmountResult=0.0;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
DailySales3 window = new DailySales3();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public DailySales3() {
initialize();
con=Database.Database();
}
public void AmountCalculate()
{
AmountGet=Double.parseDouble(UnitPrice.getText());
AmountCal=Double.parseDouble(Qnty.getText());
AmountResult=AmountCal*AmountGet;
Amount.setText(Double.toString(AmountResult));
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 1229, 658);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 1213, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
);
panel.setLayout(null);
JLabel label = new JLabel("Bill No");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Georgia", Font.BOLD, 12));
label.setBounds(10, 11, 73, 35);
panel.add(label);
textField = new JTextField();
textField.setFont(new Font("Georgia", Font.BOLD, 15));
textField.setEditable(false);
textField.setColumns(10);
textField.setBounds(82, 11, 132, 35);
panel.add(textField);
JLabel label_1 = new JLabel("Bill Date");
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setFont(new Font("Georgia", Font.BOLD, 12));
label_1.setBounds(224, 11, 73, 35);
panel.add(label_1);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setFont(new Font("Georgia", Font.BOLD, 15));
dateChooser.setBounds(296, 11, 132, 35);
panel.add(dateChooser);
JLabel label_2 = new JLabel("Sales Type");
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setFont(new Font("Georgia", Font.BOLD, 12));
label_2.setBounds(438, 11, 73, 35);
panel.add(label_2);
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("Georgia", Font.BOLD, 15));
comboBox.setBounds(524, 10, 190, 35);
panel.add(comboBox);
JLabel label_3 = new JLabel("Customers");
label_3.setHorizontalAlignment(SwingConstants.CENTER);
label_3.setFont(new Font("Georgia", Font.BOLD, 12));
label_3.setBounds(724, 11, 73, 35);
panel.add(label_3);
textField_1 = new JTextField();
textField_1.setFont(new Font("Georgia", Font.BOLD, 15));
textField_1.setColumns(10);
textField_1.setBounds(797, 12, 210, 35);
panel.add(textField_1);
JButton button = new JButton("");
button.setBounds(1016, 12, 38, 35);
panel.add(button);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 68, 997, 43);
panel.add(panel_1);
JLabel lblItemId = new JLabel("Item ID");
lblItemId.setHorizontalAlignment(SwingConstants.CENTER);
lblItemId.setPreferredSize(new Dimension(91, 35));
lblItemId.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblItemId);
JLabel lblItemName = new JLabel("Item Name");
lblItemName.setPreferredSize(new Dimension(273, 35));
lblItemName.setHorizontalAlignment(SwingConstants.CENTER);
lblItemName.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblItemName);
JLabel lblStock = new JLabel("Stock");
lblStock.setPreferredSize(new Dimension(86, 35));
lblStock.setHorizontalAlignment(SwingConstants.CENTER);
lblStock.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblStock);
JLabel lblQnty = new JLabel("Qnty");
lblQnty.setPreferredSize(new Dimension(86, 35));
lblQnty.setHorizontalAlignment(SwingConstants.CENTER);
lblQnty.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblQnty);
JLabel lblUnitPrice = new JLabel("Unit Price");
lblUnitPrice.setPreferredSize(new Dimension(123, 35));
lblUnitPrice.setHorizontalAlignment(SwingConstants.CENTER);
lblUnitPrice.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblUnitPrice);
JLabel lblTax = new JLabel("Tax (%)");
lblTax.setPreferredSize(new Dimension(86, 35));
lblTax.setHorizontalAlignment(SwingConstants.CENTER);
lblTax.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblTax);
JLabel lblDiscount = new JLabel("Discount (%)");
lblDiscount.setPreferredSize(new Dimension(86, 35));
lblDiscount.setHorizontalAlignment(SwingConstants.CENTER);
lblDiscount.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblDiscount);
JLabel lblAmount = new JLabel("Amount");
lblAmount.setPreferredSize(new Dimension(123, 35));
lblAmount.setHorizontalAlignment(SwingConstants.CENTER);
lblAmount.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblAmount);
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 112, 997, 43);
panel.add(panel_2);
panel_2.setLayout(null);
ItemID = new JTextField();
ItemID.setBounds(5, 5, 89, 34);
ItemID.setFont(new Font("Georgia", Font.BOLD, 15));
ItemID.setColumns(10);
panel_2.add(ItemID);
ItemName = new JTextField();
ItemName.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(ItemName.getText().equals(""))
{
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
}
else if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
table_2.requestFocus();
table_2.changeSelection(0, 0, false, false);
}
else
{
try {
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
query="Select Item_No,Item_Name,Current_Stock,Unit_Price,Tax,Discount from StockEntry where Item_Name like '"+ItemName.getText().trim()+"%'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
while(res.next())
{
String ItemNo=res.getString("Item_No");
String ItemName=res.getString("Item_Name");
double CStock=res.getDouble("Current_Stock");
double PRate=res.getDouble("Unit_Price");
double tax=res.getDouble("Tax");
double discount=res.getDouble("Discount");
Object row[]= {ItemNo,ItemName,CStock,PRate,tax,discount};
model.addRow(row);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
ItemName.setBounds(98, 5, 276, 34);
ItemName.setFont(new Font("Georgia", Font.BOLD, 15));
ItemName.setColumns(10);
panel_2.add(ItemName);
Stock = new JTextField();
Stock.setBounds(376, 5, 89, 34);
Stock.setFont(new Font("Georgia", Font.BOLD, 15));
Stock.setColumns(10);
panel_2.add(Stock);
Qnty = new JTextField();
Qnty.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
model1=(DefaultTableModel)table.getModel();
String itemno=ItemID.getText();
String itemname=ItemName.getText();
String qnty=Qnty.getText();
String unitprice=UnitPrice.getText();
String tax=Tax.getText();
String discount=Discount.getText();
String amount=Amount.getText();
Object rows[]= {itemno,itemname,qnty,unitprice,tax,discount,amount};
model1.addRow(rows);
}
}
});
Qnty.setBounds(469, 5, 89, 34);
Qnty.setFont(new Font("Georgia", Font.BOLD, 15));
Qnty.getDocument().addDocumentListener(new DocumentListener()
{
#Override
public void changedUpdate(DocumentEvent arg0) {
AmountCalculate();
}
#Override
public void insertUpdate(DocumentEvent arg0) {
AmountCalculate();
}
#Override
public void removeUpdate(DocumentEvent arg0) {
}
});
Qnty.setColumns(10);
panel_2.add(Qnty);
UnitPrice = new JTextField();
UnitPrice.setBounds(561, 5, 122, 34);
UnitPrice.setFont(new Font("Georgia", Font.BOLD, 15));
UnitPrice.setColumns(10);
panel_2.add(UnitPrice);
Tax = new JTextField();
Tax.setBounds(687, 5, 89, 34);
Tax.setFont(new Font("Georgia", Font.BOLD, 15));
Tax.setColumns(10);
panel_2.add(Tax);
Discount = new JTextField();
Discount.setBounds(778, 5, 89, 34);
Discount.setFont(new Font("Georgia", Font.BOLD, 15));
Discount.setColumns(10);
panel_2.add(Discount);
Amount = new JTextField();
Amount.setBounds(869, 5, 122, 34);
Amount.setFont(new Font("Georgia", Font.BOLD, 15));
Amount.setColumns(10);
panel_2.add(Amount);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 156, 997, 412);
panel.add(scrollPane);
table_2 = new JTable();
table_2.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
try
{
int row=table_2.getSelectedRow();
String TableClicked=(table_2.getModel().getValueAt(row, 0).toString());
query="Select * from StockEntry where Item_No='"+TableClicked+"'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
if(res.next())
{
String Itemno=res.getString("Item_No");
ItemID.setText(Itemno);
String itemName=res.getString("Item_Name");
ItemName.setText(itemName);
String PurchaseRate=Double.toString(res.getDouble("Unit_Price"));
UnitPrice.setText(PurchaseRate);
String stock=Double.toString(res.getDouble("Current_Stock"));
Stock.setText(stock);
String tax=Double.toString(res.getDouble("Tax"));
Tax.setText(tax);
String disc=Double.toString(res.getDouble("Discount"));
Discount.setText(disc);
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
finally
{
try
{
PStat.close();
res.close();
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
Qnty.requestFocus();
}
}
});
model=(DefaultTableModel)table_2.getModel();
model.addColumn("Item ID");
model.addColumn("Item Name");
model.addColumn("Qnty");
model.addColumn("Unit Price");
model.addColumn("Tax (%)");
model.addColumn("Discount (%)");
model.addColumn("Amount");
table_2.setRowHeight(30);
table_2.getTableHeader().setFont(new Font("Georgia", Font.BOLD, 14));
table_2.setFont(new Font("Georgia", Font.BOLD, 17));
scrollPane.setViewportView(table_2);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 156, 997, 412);
panel.add(scrollPane_1);
table = new JTable();
model1=(DefaultTableModel)table.getModel();
model1.addColumn("Item ID");
model1.addColumn("Item Name");
model1.addColumn("Qnty");
model1.addColumn("Unit Price");
model1.addColumn("Tax (%)");
model1.addColumn("Discount (%)");
model1.addColumn("Amount");
table.setRowHeight(30);
table.setFont(new Font("Georgia", Font.BOLD, 17));
scrollPane_1.setViewportView(table);
frame.getContentPane().setLayout(groupLayout);
}
}

Populate a JComboBox using SQL

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);
}
});
}

WindowBuilder Source Code Layout and Database Implementation

Hi I am a newbie java programmer, I am currently creating a java gui application using WindowBuilder within eclipse.
I have the layout that I like however, the source code is all in one class, and I can't seem to navigate through the panels correctly.
A link to the image for how the layout of panels is in WindowBuilder - http://postimg.org/image/xmnm9ilm1/
I was wondering how can I best layout the code, and how best to navigate through the program.
Bear in mind I don't know many techical terms.
Also what is the best way to implement a microsoft access backend database that can be editied within the program for login usernames, passwords and adding and removing items? I have a seperate database class at the moment.
Thank You
Source code of program is shown below
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.Box;
import java.awt.Panel;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class UnivEPOS {
private JFrame frmUniveposV;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnivEPOS window = new UnivEPOS();
window.frmUniveposV.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnivEPOS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmUniveposV = new JFrame();
frmUniveposV.setTitle("UnivEPOS v1.0");
frmUniveposV.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frmUniveposV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmUniveposV.getContentPane().setLayout(new CardLayout(0, 0));
JPanel Login = new JPanel();
Login.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Login, "name_244778779455709");
Login.setLayout(null);
JLabel lblLogin = new JLabel("LOGIN");
lblLogin.setForeground(new Color(255, 255, 255));
lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
lblLogin.setBounds(10, 0, 60, 50);
Login.add(lblLogin);
textField = new JTextField();
textField.setBounds(518, 282, 262, 20);
Login.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(518, 319, 262, 20);
Login.add(textField_1);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(416, 283, 92, 14);
Login.add(lblNewLabel);
JLabel lblPassword = new JLabel("Password");
lblPassword.setForeground(new Color(255, 255, 255));
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
lblPassword.setBounds(416, 320, 92, 14);
Login.add(lblPassword);
JButton btnNewButton = new JButton("ENTER");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(691, 396, 89, 23);
Login.add(btnNewButton);
JButton btnNewButton_1 = new JButton("EXIT");
btnNewButton_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_1.setBounds(518, 396, 89, 23);
Login.add(btnNewButton_1);
JPanel Home = new JPanel();
Home.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Home, "name_244789265918213");
Home.setLayout(null);
JLabel lblCurrentOrders = new JLabel("CURRENT ORDERS");
lblCurrentOrders.setForeground(new Color(255, 255, 255));
lblCurrentOrders.setBounds(535, 0, 346, 31);
lblCurrentOrders.setFont(new Font("Tahoma", Font.BOLD, 35));
Home.add(lblCurrentOrders);
JButton btnNewButton_2 = new JButton("Previous");
btnNewButton_2.setBounds(614, 598, 95, 31);
Home.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Next");
btnNewButton_3.setBounds(731, 598, 95, 31);
Home.add(btnNewButton_3);
JButton btnNewButton_6 = new JButton("NEW ORDER");
btnNewButton_6.setBounds(10, 598, 244, 112);
btnNewButton_6.setFont(new Font("Tahoma", Font.BOLD, 18));
Home.add(btnNewButton_6);
JButton btnNewButton_4 = new JButton("Show Paid");
btnNewButton_4.setBounds(614, 632, 95, 31);
Home.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("Show Unpaid");
btnNewButton_5.setBounds(731, 632, 95, 31);
Home.add(btnNewButton_5);
JButton btnShowAll = new JButton("SHOW ALL");
btnShowAll.setBounds(658, 663, 131, 31);
Home.add(btnShowAll);
JButton btnAdmin = new JButton("ADMIN");
btnAdmin.setBounds(1245, 682, 95, 37);
Home.add(btnAdmin);
JLabel lblCurrentUser = new JLabel("Current User:");
lblCurrentUser.setForeground(new Color(255, 255, 255));
lblCurrentUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblCurrentUser.setBounds(1081, 12, 83, 14);
Home.add(lblCurrentUser);
JLabel lblNewLabel_1 = new JLabel("Date & Time:");
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_1.setBounds(10, 14, 80, 14);
Home.add(lblNewLabel_1);
JPanel Order = new JPanel();
Order.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Order, "name_244796279334447");
Order.setLayout(null);
JLabel lblOrder = new JLabel("ORDER:");
lblOrder.setForeground(new Color(255, 255, 255));
lblOrder.setFont(new Font("Tahoma", Font.BOLD, 40));
lblOrder.setBounds(10, 11, 166, 43);
Order.add(lblOrder);
JLabel lblDateTime = new JLabel("DATE & TIME:");
lblDateTime.setForeground(new Color(255, 255, 255));
lblDateTime.setFont(new Font("Tahoma", Font.BOLD, 40));
lblDateTime.setBounds(771, 11, 282, 43);
Order.add(lblDateTime);
JLabel lblTable = new JLabel("TABLE:");
lblTable.setForeground(new Color(255, 255, 255));
lblTable.setFont(new Font("Tahoma", Font.BOLD, 40));
lblTable.setBounds(391, 11, 146, 43);
Order.add(lblTable);
Panel panel = new Panel();
panel.setBounds(10, 75, 650, 489);
Order.add(panel);
panel.setLayout(null);
JLabel lblNewLabel_2 = new JLabel("TOTAL:");
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 40));
lblNewLabel_2.setBounds(10, 421, 157, 57);
panel.add(lblNewLabel_2);
JLabel lblCurrentOrder = new JLabel("CURRENT ORDER");
lblCurrentOrder.setForeground(new Color(255, 255, 255));
lblCurrentOrder.setBounds(251, 5, 198, 31);
lblCurrentOrder.setFont(new Font("Tahoma", Font.PLAIN, 25));
panel.add(lblCurrentOrder);
Panel panel_1 = new Panel();
panel_1.setBounds(690, 75, 650, 489);
Order.add(panel_1);
panel_1.setLayout(null);
JLabel lblCategories = new JLabel("CATEGORIES");
lblCategories.setForeground(new Color(255, 255, 255));
lblCategories.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblCategories.setBounds(254, 11, 153, 31);
panel_1.add(lblCategories);
JButton btnPrevious = new JButton("PREVIOUS");
btnPrevious.setBounds(462, 455, 89, 23);
panel_1.add(btnPrevious);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(551, 455, 89, 23);
panel_1.add(btnNext);
JButton btnNewButton_7 = new JButton("SAVE");
btnNewButton_7.setFont(new Font("Tahoma", Font.BOLD, 30));
btnNewButton_7.setBounds(10, 582, 254, 137);
Order.add(btnNewButton_7);
JButton btnSavePay = new JButton("SAVE & PAY");
btnSavePay.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSavePay.setBounds(274, 582, 274, 137);
Order.add(btnSavePay);
JButton btnFinish = new JButton("FINISH");
btnFinish.setFont(new Font("Tahoma", Font.BOLD, 30));
btnFinish.setBounds(558, 582, 253, 137);
Order.add(btnFinish);
JButton button = new JButton("ADMIN");
button.setFont(new Font("Tahoma", Font.BOLD, 11));
button.setBounds(1245, 682, 95, 37);
Order.add(button);
JButton btnExit = new JButton("EXIT");
btnExit.setFont(new Font("Tahoma", Font.BOLD, 11));
btnExit.setBounds(1114, 682, 95, 37);
Order.add(btnExit);
JPanel Reports = new JPanel();
Reports.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Reports, "name_244798869260892");
Reports.setLayout(null);
Panel panel_2 = new Panel();
panel_2.setBounds(0, 180, 1350, 459);
Reports.add(panel_2);
panel_2.setLayout(null);
JLabel lblSales = new JLabel("SALES");
lblSales.setForeground(new Color(255, 255, 255));
lblSales.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblSales.setBounds(600, 0, 78, 52);
panel_2.add(lblSales);
JLabel lblReports = new JLabel("REPORTS");
lblReports.setForeground(new Color(255, 255, 255));
lblReports.setFont(new Font("Tahoma", Font.BOLD, 35));
lblReports.setBounds(556, 0, 174, 52);
Reports.add(lblReports);
JButton btnNewButton_8 = new JButton("SALES");
btnNewButton_8.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnNewButton_8.setBounds(10, 97, 174, 77);
Reports.add(btnNewButton_8);
JButton btnCancellations = new JButton("CANCELLATIONS");
btnCancellations.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnCancellations.setBounds(194, 97, 221, 77);
Reports.add(btnCancellations);
JButton btnItemsSold = new JButton("ITEMS SOLD");
btnItemsSold.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnItemsSold.setBounds(425, 97, 221, 77);
Reports.add(btnItemsSold);
JLabel lblNewLabel_3 = new JLabel("PAGE");
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
lblNewLabel_3.setBounds(610, 645, 106, 74);
Reports.add(lblNewLabel_3);
JButton btnNewButton_9 = new JButton("PREVIOUS");
btnNewButton_9.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_9.setBounds(860, 645, 163, 74);
Reports.add(btnNewButton_9);
JButton btnNext_1 = new JButton("NEXT");
btnNext_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNext_1.setBounds(1025, 645, 164, 74);
Reports.add(btnNext_1);
JButton btnNewButton_10 = new JButton("EXIT");
btnNewButton_10.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_10.setBounds(1199, 645, 141, 74);
Reports.add(btnNewButton_10);
JPanel Settings = new JPanel();
Settings.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Settings, "name_244813941995636");
Settings.setLayout(null);
JLabel lblNewLabel_4 = new JLabel("SYSTEM SETTINGS");
lblNewLabel_4.setForeground(new Color(255, 255, 255));
lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_4.setBounds(500, 0, 339, 54);
Settings.add(lblNewLabel_4);
JList list = new JList();
list.setForeground(new Color(255, 255, 255));
list.setBackground(new Color(128, 0, 0));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFont(new Font("Tahoma", Font.PLAIN, 40));
list.setModel(new AbstractListModel() {
String[] values = new String[] {"USER 1", "USER 2", "USER 3", "USER 4", "USER 5"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.setBounds(10, 82, 634, 481);
Settings.add(list);
JButton btnNewButton_11 = new JButton("ADD USER");
btnNewButton_11.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_11.setBounds(10, 617, 196, 102);
Settings.add(btnNewButton_11);
JButton btnChangeSuperuserPassword = new JButton("CHANGE SUPERUSER PASSWORD");
btnChangeSuperuserPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
btnChangeSuperuserPassword.setBounds(433, 617, 211, 102);
Settings.add(btnChangeSuperuserPassword);
JButton button_1 = new JButton("EXIT");
button_1.setFont(new Font("Tahoma", Font.BOLD, 11));
button_1.setBounds(1245, 682, 95, 37);
Settings.add(button_1);
Panel panel_3 = new Panel();
panel_3.setBounds(650, 84, 690, 479);
Settings.add(panel_3);
panel_3.setLayout(null);
JLabel lblNewLabel_5 = new JLabel("RECEIPT OF SALE");
lblNewLabel_5.setForeground(new Color(255, 255, 255));
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel_5.setBounds(260, 11, 178, 44);
panel_3.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("COMPANY");
lblNewLabel_6.setForeground(new Color(255, 255, 255));
lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblNewLabel_6.setBounds(24, 136, 125, 44);
panel_3.add(lblNewLabel_6);
JLabel lblSalesRepresentative = new JLabel("SALES REP");
lblSalesRepresentative.setForeground(new Color(255, 255, 255));
lblSalesRepresentative.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblSalesRepresentative.setBounds(24, 213, 125, 44);
panel_3.add(lblSalesRepresentative);
JLabel lblAddress = new JLabel("ADDRESS");
lblAddress.setForeground(new Color(255, 255, 255));
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblAddress.setBounds(24, 171, 125, 48);
panel_3.add(lblAddress);
JLabel lblMessage = new JLabel("MESSAGE");
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblMessage.setBounds(24, 256, 125, 38);
panel_3.add(lblMessage);
textField_2 = new JTextField();
textField_2.setBounds(154, 151, 404, 26);
panel_3.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(154, 188, 404, 26);
panel_3.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(154, 228, 404, 26);
panel_3.add(textField_4);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(154, 268, 404, 26);
panel_3.add(textField_5);
JPanel Administration = new JPanel();
Administration.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Administration, "name_244816080502214");
Administration.setLayout(null);
JLabel lblNewLabel_7 = new JLabel("ADMINISTRATION");
lblNewLabel_7.setForeground(new Color(255, 255, 255));
lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_7.setBounds(506, 0, 335, 43);
Administration.add(lblNewLabel_7);
JLabel lblCategories_1 = new JLabel("CATEGORIES");
lblCategories_1.setForeground(new Color(255, 255, 255));
lblCategories_1.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblCategories_1.setBounds(90, 88, 214, 43);
Administration.add(lblCategories_1);
JLabel lblSubcategories = new JLabel("SUB-CATEGORIES");
lblSubcategories.setForeground(new Color(255, 255, 255));
lblSubcategories.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblSubcategories.setBounds(528, 88, 292, 43);
Administration.add(lblSubcategories);
JLabel label = new JLabel("SUB-CATEGORIES");
label.setForeground(new Color(255, 255, 255));
label.setFont(new Font("Tahoma", Font.PLAIN, 35));
label.setBounds(1008, 88, 292, 43);
Administration.add(label);
JList list_1 = new JList();
list_1.setBounds(55, 142, 292, 404);
Administration.add(list_1);
JList list_2 = new JList();
list_2.setBounds(528, 142, 292, 404);
Administration.add(list_2);
JList list_3 = new JList();
list_3.setBounds(1008, 142, 292, 404);
Administration.add(list_3);
JButton btnAdd = new JButton("ADD");
btnAdd.setBounds(55, 557, 89, 23);
Administration.add(btnAdd);
JButton button_2 = new JButton("ADD");
button_2.setBounds(528, 557, 89, 23);
Administration.add(button_2);
JButton button_3 = new JButton("ADD");
button_3.setBounds(1008, 557, 89, 23);
Administration.add(button_3);
JButton btnRemove = new JButton("REMOVE");
btnRemove.setBounds(258, 557, 89, 23);
Administration.add(btnRemove);
JButton button_4 = new JButton("REMOVE");
button_4.setBounds(731, 557, 89, 23);
Administration.add(button_4);
JButton button_5 = new JButton("REMOVE");
button_5.setBounds(1211, 557, 89, 23);
Administration.add(button_5);
JButton btnReports = new JButton("REPORTS");
btnReports.setFont(new Font("Tahoma", Font.BOLD, 30));
btnReports.setBounds(10, 638, 180, 81);
Administration.add(btnReports);
JButton btnSettings = new JButton("SETTINGS");
btnSettings.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSettings.setBounds(200, 638, 185, 81);
Administration.add(btnSettings);
JButton button_6 = new JButton("EXIT");
button_6.setFont(new Font("Tahoma", Font.BOLD, 11));
button_6.setBounds(1245, 682, 95, 37);
Administration.add(button_6);
}
}
Source code of database class is shown below
import java.awt.event.*;
import java.sql.*;
public class database {
Connection con;
Statement st;
ResultSet rs;
public database()
{
connect();
frame();
}
public void connect()
{
try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch(Exception ex){
}
}
public void frame()
{
new UnivEPOS();
}
}

JApplet issue stating class not found and class not found exception

I am having an issue that is giving me an error message and a blank applet is appearing. The error message says load:class Myapp.class not found and java.lang.ClassNotFoundException and then a list of lines saying # and then some other jargon.
If I run try to run the program as a Jcreator applet like I expected I get the prior stated error and if I run it in an application it just gives me the main method error.
Here is my program:
package MyPack;
import javax.swing.*;
import java.awt.event.*;
public class Myapp extends JApplet implements ItemListener{
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
JComboBox comboBox;
JComboBox comboBox_1;
JCheckBox chckbxPizza;
JCheckBox chckbxBurger;
JCheckBox chckbxSalad;
JLabel label_3;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
JTextArea txtrCommentsForSpecial;
JTextArea txtrCommentsForSpecial_2;
JTextArea txtrCommentsForSpecial_1;
JInternalFrame internalFrame;
public Myapp() {
}
public void init() {
setSize(700,640);
// setVisible(true);
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Create two buttons and a label.
// setLayout(null);
String a[] = { "Chiken Tikka", "Fajita", "Vegetable"};
comboBox = new JComboBox(a);
comboBox.setBounds(115, 103, 100, 20);
comboBox.addItemListener(this);
getContentPane().add(comboBox);
String b[] = { "small", "medium","large","extra large"};
comboBox_1 = new JComboBox(b);
// comboBox_1.addItemListener(this);
// comboBox_1.setEnabled(false);
getContentPane().add(comboBox_1);
comboBox_1.setBounds(290, 103, 140, 20);
// setSize(300, 300);
getContentPane().setLayout(null);
JLabel lblSelectMenu = new JLabel("Select Menu");
lblSelectMenu.setForeground(new Color(72, 61, 139));
lblSelectMenu.setFont(new Font("Segoe Script", Font.BOLD, 16));
lblSelectMenu.setBounds(58, 10, 107, 34);
getContentPane().add(lblSelectMenu);
chckbxPizza = new JCheckBox("Pizza");
chckbxPizza.setBounds(37, 64, 97, 23);
getContentPane().add(chckbxPizza);
JLabel lblFlavour = new JLabel("flavour");
lblFlavour.setBounds(60, 103, 45, 20);
getContentPane().add(lblFlavour);
JLabel lblSize = new JLabel("size");
lblSize.setBounds(253, 103, 45, 20);
getContentPane().add(lblSize);
JLabel lblQty = new JLabel("Qty");
lblQty.setBounds(451, 103, 35, 20);
getContentPane().add(lblQty);
textField = new JTextField();
textField.setBounds(485, 103, 86, 20);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblComments = new JLabel("comments");
lblComments.setBounds(139, 154, 76, 20);
getContentPane().add(lblComments);
txtrCommentsForSpecial = new JTextArea();
txtrCommentsForSpecial.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial.setText("comments for special features");
txtrCommentsForSpecial.setBounds(210, 154, 268, 61);
getContentPane().add(txtrCommentsForSpecial);
chckbxBurger = new JCheckBox("Burger");
chckbxBurger.setBounds(37, 236, 97, 23);
getContentPane().add(chckbxBurger);
JLabel label = new JLabel("flavour");
label.setBounds(60, 286, 45, 20);
getContentPane().add(label);
String c[] = { "Fish", "Vegetable", "Chicken"};
JComboBox comboBox_2 = new JComboBox(c);
comboBox_2.setBounds(115, 286, 100, 20);
getContentPane().add(comboBox_2);
JLabel label_1 = new JLabel("size");
label_1.setBounds(253, 286, 45, 20);
getContentPane().add(label_1);
JComboBox comboBox_3 = new JComboBox(b);
comboBox_3.setBounds(290, 286, 110, 20);
getContentPane().add(comboBox_3);
JLabel label_2 = new JLabel("Qty");
label_2.setBounds(451, 286, 35, 20);
getContentPane().add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(485, 286, 86, 20);
getContentPane().add(textField_1);
textField_1.setColumns(10);
label_3 = new JLabel("comments");
label_3.setBounds(139, 340, 76, 20);
getContentPane().add(label_3);
txtrCommentsForSpecial_1 = new JTextArea();
txtrCommentsForSpecial_1.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_1.setText("comments for special features");
txtrCommentsForSpecial_1.setBounds(210, 340, 268, 61);
getContentPane().add(txtrCommentsForSpecial_1);
chckbxSalad = new JCheckBox("Soft Drink");
chckbxSalad.setBounds(37, 424, 97, 23);
getContentPane().add(chckbxSalad);
JLabel label_4 = new JLabel("flavour");
label_4.setBounds(60, 475, 45, 20);
getContentPane().add(label_4);
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
JComboBox comboBox_4 = new JComboBox(f);
comboBox_4.setBounds(115, 475, 86, 20);
getContentPane().add(comboBox_4);
JLabel label_5 = new JLabel("size");
label_5.setBounds(253, 475, 45, 20);
getContentPane().add(label_5);
JComboBox comboBox_5 = new JComboBox(b);
comboBox_5.setBounds(290, 475, 110, 20);
getContentPane().add(comboBox_5);
JLabel label_6 = new JLabel("Qty");
label_6.setBounds(451, 475, 35, 20);
getContentPane().add(label_6);
textField_2 = new JTextField();
textField_2.setBounds(485, 475, 86, 20);
getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel label_7 = new JLabel("comments");
label_7.setBounds(139, 546, 76, 20);
getContentPane().add(label_7);
txtrCommentsForSpecial_2 = new JTextArea();
txtrCommentsForSpecial_2.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_2.setText("comments for special features");
txtrCommentsForSpecial_2.setBounds(210, 546, 268, 61);
getContentPane().add(txtrCommentsForSpecial_2);
JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame = new JInternalFrame("Order Details");
internalFrame.setBounds(100, 80, 450, 300);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
internalFrame.getContentPane().setLayout(null);
internalFrame.setVisible(true);
JTextArea textArea = new JTextArea();
textArea.setBounds(67, 52, 270, 149);
internalFrame.getContentPane().add(textArea);
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame.setVisible(false);
}
});
btnOk.setBounds(164, 212, 89, 23);
internalFrame.getContentPane().add(btnOk);
JLabel lblOrderYouPlaced = new JLabel("Order You Placed");
lblOrderYouPlaced.setBounds(67, 26, 98, 14);
internalFrame.getContentPane().add(lblOrderYouPlaced);
getContentPane().add(internalFrame);
}
});
btnNewButton.setBounds(162, 616, 89, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
chckbxPizza.setText("");
chckbxBurger.setText("");
chckbxSalad.setText("");
txtrCommentsForSpecial.setText("");
txtrCommentsForSpecial_2.setText("");
txtrCommentsForSpecial_1.setText("");
}
});
btnNewButton_1.setBounds(279, 616, 89, 23);
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Cancel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_2.setBounds(389, 616, 89, 23);
getContentPane().add(btnNewButton_2);
}
public void itemStateChanged(ItemEvent e) {
String b[] = { "Chikken Tikka", "Fajita" };
String c[] = { "Fish", "Vegetable", "Chicken"};
String d[] = { "Russian Salad", "Fruit Salad", "Beans Salad"};
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
if (e.getSource() == comboBox) {
if (comboBox.getSelectedItem().equals("Select")) {
comboBox_1.setEnabled(false);
} else if (comboBox.getSelectedItem().equals("Pizza")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < b.length; i++) {
comboBox_1.addItem(b[i]);
}
} else if (comboBox.getSelectedItem().equals("Burger")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < c.length; i++) {
comboBox_1.removeItem(c[i]);
comboBox_1.addItem(c[i]);
}
} else if (comboBox.getSelectedItem().equals("Salad")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < d.length; i++) {
comboBox_1.addItem(d[i]);
}
}else if (comboBox.getSelectedItem().equals("Soft Drink")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < f.length; i++) {
comboBox_1.addItem(f[i]);
}
}
}
}
}
Write the itemListener with a captial letter like this
public class Myapp extends JApplet implements ItemListener{
and import all this packages:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

JDOM write content to an existing file without deleting

I created a database in xml and I want to create a register form in Jframe to add object to the db. So I build up the code, but every time I click the button Registra, it overwrites me that file. How can I keep the file and simply add elements without deleting the others? Here's the code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.text.Format;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Registrati extends JFrame {
private JPanel contentPane;
private static JTextField textField;
private static JTextField textField_1;
private static JTextField textField_2;
private static JTextField textField_3;
private static JTextField textField_4;
private static JTextField textField_5;
private static JTextField textField_6;
private static JPasswordField passwordField;
private static JTextField textField_7;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Registrati frame = new Registrati();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Registrati() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 463, 460);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblInserisciITuoi = new JLabel("Inserisci i tuoi dati per registrarti");
lblInserisciITuoi.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblInserisciITuoi.setBounds(156, 23, 215, 14);
contentPane.add(lblInserisciITuoi);
JLabel lblNome = new JLabel("Nome");
lblNome.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNome.setBounds(38, 58, 46, 14);
contentPane.add(lblNome);
textField = new JTextField();
textField.setBounds(127, 56, 229, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblCognome = new JLabel("Cognome");
lblCognome.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblCognome.setBounds(38, 89, 64, 14);
contentPane.add(lblCognome);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(127, 87, 229, 20);
contentPane.add(textField_1);
JLabel lblDataDiNascita = new JLabel("Data di Nascita");
lblDataDiNascita.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblDataDiNascita.setBounds(38, 120, 98, 14);
contentPane.add(lblDataDiNascita);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(127, 118, 229, 20);
contentPane.add(textField_2);
JLabel lblAltezza = new JLabel("Altezza");
lblAltezza.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblAltezza.setBounds(38, 171, 64, 14);
contentPane.add(lblAltezza);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(127, 169, 229, 20);
contentPane.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(127, 200, 229, 20);
contentPane.add(textField_4);
JLabel lblPeso = new JLabel("Peso");
lblPeso.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPeso.setBounds(38, 203, 46, 14);
contentPane.add(lblPeso);
JLabel lblSesso = new JLabel("Sesso");
lblSesso.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblSesso.setBounds(38, 233, 46, 14);
contentPane.add(lblSesso);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(127, 231, 229, 20);
contentPane.add(textField_5);
JLabel lblEmail = new JLabel("email");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEmail.setBounds(38, 283, 46, 14);
contentPane.add(lblEmail);
textField_6 = new JTextField();
textField_6.setColumns(10);
textField_6.setBounds(127, 281, 229, 20);
contentPane.add(textField_6);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPassword.setBounds(38, 343, 64, 14);
contentPane.add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(127, 341, 229, 20);
contentPane.add(passwordField);
textField_7 = new JTextField();
textField_7.setColumns(10);
textField_7.setBounds(127, 312, 229, 20);
contentPane.add(textField_7);
JLabel lblUsername = new JLabel("Username");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUsername.setBounds(38, 314, 64, 14);
contentPane.add(lblUsername);
JButton btnRegistrami = new JButton("Registrami");
btnRegistrami.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Atleta atleta= new Atleta();
try {
Writer(atleta);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnRegistrami.setBounds(186, 387, 110, 23);
contentPane.add(btnRegistrami);
}
public static void Writer(Atleta atleta) throws JDOMException, IOException {
Document document = null;
Element root = null;
File xmlFile = new File("db.xml");
if(xmlFile.exists()) {
// try to load document from xml file if it exist
// create a file input stream
FileInputStream fis = new FileInputStream(xmlFile);
// create a sax builder to parse the document
SAXBuilder sb = new SAXBuilder();
// parse the xml content provided by the file input stream and create a Document object
document = sb.build(fis);
// get the root element of the document
root = document.getRootElement();
fis.close();
} else {
// if it does not exist create a new document and new root
document = new Document();
root = new Element("Ercole");
}
String nome = textField.getText();
String cognome = textField_1.getText();
#SuppressWarnings("deprecation")
String password = passwordField.getText();
String username = textField_7.getText();
String email = textField_6.getText();
String nascita = textField_2.getText();
String peso = textField_4.getText();
String sesso = textField_5.getText();
String altezza = textField_3.getText();
Element child = new Element("Atleta");
child.addContent(new Element("Password").setText(password));
child.addContent(new Element("Username").setText(username));
child.addContent(new Element("email").setText(email));
child.addContent(new Element("Nome").setText(nome));
child.addContent(new Element("Cognome").setText(cognome));
child.addContent(new Element("ID").setText("da modificare!"));
child.addContent(new Element("Nascita").setText(nascita));
child.addContent(new Element("Peso").setText(peso));
child.addContent(new Element("Sesso").setText(sesso));
child.addContent(new Element("Altezza").setText(altezza));
root.addContent(child);
document.setContent(root);
try {
FileWriter writer = new FileWriter("c:\\db.xml");
XMLOutputter outputter = new XMLOutputter();
//outputter.setFormat(Format.getPrettyFormat());
outputter.output(document, writer);
outputter.output(document, System.out);
writer.close(); // close writer
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think the file you are reading is not the file you are writing
You read "db.xml"
You write "c:\\db.xml"
The first one probably never exists, so it has no content to append your new registration to, so you just end up writing your new registration to the c:\db.xml file

Categories

Resources