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);
}
});
}
Related
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;
}
}
}
I have made a simple digital business card in Java swing using Eclipse, I am brand new to Java and programming in general so please bear with me. Basically what I want to do is add a button that says "Our Work" or "Portfolio" for example, which when clicked will open a new window where I can then add pictures or links or false reviews etc.
I know this is probably quite simple, but I struggle to understand a lot of tutorials when it is based on other peoples code.
package dbuscard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JSeparator;
public class card {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
card window = new card();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public card() {
initialize();
}
/**
* Initialise the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(153, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblLogo = new JLabel("");
lblLogo.setBounds(127, 11, 219, 104);
frame.getContentPane().add(lblLogo);
lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png"));
JLabel lblNewLabel = new JLabel("t: 01254 777494");
lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel.setBounds(20, 187, 126, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblEApexuxdesigngmailcom = new JLabel("e: apexuxdesign#gmail.com");
lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14);
frame.getContentPane().add(lblEApexuxdesigngmailcom);
JLabel lblVisitWebsite = new JLabel("visit website");
lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11));
lblVisitWebsite.setBounds(10, 237, 117, 14);
frame.getContentPane().add(lblVisitWebsite);
JLabel facebook = new JLabel("");
facebook.setBounds(282, 204, 64, 47);
frame.getContentPane().add(facebook);
facebook.setIcon(new ImageIcon("Images\\facebook.png"));
JLabel twitter = new JLabel("");
twitter.setBounds(320, 204, 72, 47);
frame.getContentPane().add(twitter);
twitter.setIcon(new ImageIcon("Images\\twitter.png"));
JLabel youtube = new JLabel("");
youtube.setBounds(356, 204, 68, 47);
frame.getContentPane().add(youtube);
youtube.setIcon(new ImageIcon("Images\\youtube.png"));
JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson");
lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblSeanHutchinson.setBounds(20, 128, 126, 14);
frame.getContentPane().add(lblSeanHutchinson);
JLabel lblUxDesigner = new JLabel("UX Designer");
lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblUxDesigner.setBounds(20, 145, 107, 14);
frame.getContentPane().add(lblUxDesigner);
JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design");
lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel_1.setBounds(20, 162, 158, 14);
frame.getContentPane().add(lblNewLabel_1);
}
}
Simple. Add an action listener which uses the method setVisible(true); on a seperate JFrame. Example code:
package com.nonsense;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class del {
private JFrame frame, frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
del window = new del();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public del() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame2 = new JFrame();
frame2.setBounds(100, 100, 450, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.getContentPane().setLayout(null);
JButton btnOpenWindow = new JButton("Open Window");
btnOpenWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame2.setVisible(true);
}
});
btnOpenWindow.setBounds(167, 118, 120, 23);
frame.getContentPane().add(btnOpenWindow);
}
}
I use "DISPOSE_ON_CLOSE" so that the second window does not terminate the program when the X button is pressed.
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));
}
}
I'm trying to make my JPanel scrollable but it's not working. I made a JPanel, added components to it, then added my JPanel to a JScrollPane. This is what you're supposed to do, right? What am I doing wrong?
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;
public class RegisterPane extends JPanel {
private JTextField txtJohnDoe;
private JTextField txtExampledomaincom;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
/**
* Create the panel.
*/
public RegisterPane() {
setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(6, 36, 300, 450);
panel.setLayout(null);
JLabel lblFirstName = new JLabel("First Name");
lblFirstName.setBounds(28, 6, 92, 16);
panel.add(lblFirstName);
txtJohnDoe = new JTextField();
txtJohnDoe.setText("John Doe");
txtJohnDoe.setBounds(124, 0, 251, 28);
panel.add(txtJohnDoe);
txtJohnDoe.setColumns(10);
txtExampledomaincom = new JTextField();
txtExampledomaincom.setText("Example#domain.com");
txtExampledomaincom.setColumns(10);
txtExampledomaincom.setBounds(124, 40, 251, 28);
panel.add(txtExampledomaincom);
JLabel lblEmail = new JLabel("Email");
lblEmail.setBounds(28, 46, 92, 16);
panel.add(lblEmail);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(28, 89, 55, 16);
panel.add(lblGender);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBounds(124, 85, 92, 23);
panel.add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBounds(283, 85, 92, 23);
panel.add(rdbtnFemale);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(28, 157, 55, 16);
panel.add(lblPassword);
JLabel passDirections = new JLabel();
passDirections.setHorizontalAlignment(SwingConstants.CENTER);
passDirections.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
passDirections.setBackground(SystemColor.window);
passDirections.setText("Password's Must be at Least 6 characters long and contain 1 non letter character");
passDirections.setBounds(29, 117, 346, 28);
panel.add(passDirections);
passwordField = new JPasswordField();
passwordField.setBounds(124, 151, 251, 28);
panel.add(passwordField);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(124, 195, 251, 28);
panel.add(passwordField_1);
JLabel lblRetypePassword = new JLabel("Password Again");
lblRetypePassword.setBounds(28, 201, 92, 16);
panel.add(lblRetypePassword);
JCheckBox chckbxSubscribeToNews = new JCheckBox("Subscribe to News Letter");
chckbxSubscribeToNews.setSelected(true);
chckbxSubscribeToNews.setHorizontalAlignment(SwingConstants.CENTER);
chckbxSubscribeToNews.setBounds(29, 244, 346, 23);
panel.add(chckbxSubscribeToNews);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(28, 280, 55, 16);
panel.add(lblNewLabel);
JScrollPane scroll = new JScrollPane(panel);
scroll.setSize(new Dimension(450,300));
panel.setAutoscrolls(true);
add(scroll);
}
}
Here's my JFrame class
import java.awt.BorderLayout;
public class MainFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setResizable(false);
setContentPane(new RegisterPane());
//RegisterPane isn't scrolling ^
}
}
Thanks in Advance for your help!
I'm seeing setLayout(null) alot in your code.
The JViewport uses the component's/view's preferred size as a bases for determining if the view expands beyond the visible bounds of the JScrollPane, because you've seen fit to ignore this feature, the components have begun to break down.
Swing is designed to work layout managers and it makes it much easier to develop complex user interfaces that can work across a multitude of platforms and environments
Following is my Java Swing applet code in which I have first field as employee code and text box for entering employee code, I want to retrieve data of employee from MySql database when I will enter employee code in text box after hitting Enter key on keyboard and set retrieve data in respective text boxes and combo boxes. and same to embed it in JSP as applet.
Please help.
Here is my code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
public class App extends JApplet{
public void start()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");
String data []={"A","AB","B","B +","A +","O +","O -"};
JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
//pin.setBounds(10, 110, 100, 25);
//pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
//button2.setBounds(10, 70, 75, 25);
c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);
c.add(but_per);
//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}
public void stop(){}
}
In your ActionListener for that button retrieve the text of the textfield using textfield.getText() and then use this value to query database. I'm assuming that you know well how to query database.
Wait! you haven't implemented ActionListener!
Do the following:
but_per.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
/// write your code here!
String a= field.getText();
System.out.print(a);
///or DB code
}
});
Here is a LINK if you aren't familiar with Action Listener
UPDATE:
If you want all processing after a key press, you have to implement keybord events,
you can find a tutorial link here
This source is the closest workable version of your code I could produce, but it still has many serious problems.
Using the start() method incorrectly. The method will be called each time the browser is restored from minimized.
Use of a null layout and setBounds().
A free floating, non modal GUI element will cause focus problems..
Having said that, type a number in the employee code field and hit enter to see something like (screenshot shortened vertically)..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* <applet code=App width=400 height=600></applet> */
public class App extends JApplet {
public void start() {
final JFrame frame = new JFrame("Form");
ActionListener doDB = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "Query the DB off the EDT!");
}
};
// This cannot be done in a sand-boxed applet
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// hides the bottom field..
//frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
Container c;
c = frame.getContentPane();
c.setLayout(null);
JLabel name = new JLabel("Name :");
JLabel compcode = new JLabel("Company Code :");
JLabel cardno = new JLabel("Card Number: ");
JLabel cardtype = new JLabel("Card Type :");
JLabel pin = new JLabel("Pin :");
JLabel bldgrp = new JLabel("Blood Group :");
JLabel empcode = new JLabel("Employee Code :");
JLabel dob = new JLabel("DOB :");
JLabel valupto = new JLabel("Valid Upto :");
JLabel jdate = new JLabel("Joining Date :");
JLabel dept = new JLabel("Department :");
JLabel uid = new JLabel("UID :");
String data[] = {"A", "AB", "B", "B +", "A +", "O +", "O -"};
JTextField nametxt = new JTextField(10);
JComboBox compcodetxt = new JComboBox();
JTextField cardnumtxt = new JTextField(10);
JTextField cardtypetxt = new JTextField(10);
JTextField pintxt = new JTextField(10);
JComboBox bldgrptxt = new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt = new JTextField(10);
empcodetxt.addActionListener(doDB);
JTextField dobtxt = new JTextField(10);
JTextField valuptotxt = new JTextField(10);
JTextField jdatetxt = new JTextField(10);
JTextField depttxt = new JTextField(10);
JTextField uidtxt = new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
c.add(name);
c.add(nametxt);
c.add(compcode);
c.add(compcodetxt);
c.add(cardno);
c.add(cardnumtxt);
c.add(pin);
c.add(pintxt);
c.add(bldgrp);
c.add(bldgrptxt);
c.add(empcode);
c.add(empcodetxt);
c.add(dob);
c.add(dobtxt);
c.add(valupto);
c.add(valuptotxt);
c.add(jdate);
c.add(jdatetxt);
c.add(dept);
c.add(depttxt);
c.add(uid);
c.add(uidtxt);
c.add(but_per);
frame.setSize(350, 400);
frame.setVisible(true);
}
public void stop() {
}
}