Show variable on JLabel - java

Just and average calculator. Well, you can interact with the program putting
grades and selecting the mean. First, I couldn't make it work taking up the
mean, so I change it. Then, it gave an error every time I tried to select a
method, so nope. Then, the method wouldn't work correctly. So I finally got it
to work, except for one thing... it can't show me average.
private JTextField textField_2;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JLabel mostrar;
private double combo, nota1, nota2, nota3, nota4, sem1, sem2, fin, aux1, aux2;
private JRadioButton DosSemestres, CuatroParciales, TresParciales, DosParciales;
private JLabel SelecMetod, LBLMax , LBLMin, lblNota1, lblNota2, lblNota3, lblNota4, lblSem1, lblsem2, lblfinal;
private JButton NotaBoton, Calcular, Elegir;
private JLabel AreaSem1, AreaSem2, AreaPromedioFin;
private JLabel Clasi;
public Panel() {
setLayout(null);
setSize(850, 556);
//metodo de creacion de año electivo
SelecMetod = new JLabel("Seleccione el metodo electivo: ");
DosSemestres = new JRadioButton("1. Dos semestres de dos parciales cada uno");
CuatroParciales = new JRadioButton("2. Cuatro parciales repartidos en un año electivo");
TresParciales = new JRadioButton("3. Tres parciales repartidos en un año electivo");
DosParciales = new JRadioButton("4. Dos parciales repartidos en un año electivo");
ButtonGroup group = new ButtonGroup();
Dimension size1 = DosSemestres.getPreferredSize();
Dimension size2 = CuatroParciales.getPreferredSize();
Dimension size3 = TresParciales.getPreferredSize();
Dimension size4 = DosParciales.getPreferredSize();
SelecMetod.setBounds(20, 5, size1.width, 20);
SelecMetod.setForeground(Color.BLUE);
DosSemestres.setBounds(20, 25, size1.width, 20);
CuatroParciales.setBounds(20, 55, size2.width, 20);
TresParciales.setBounds(20, 85, size3.width, 20);
DosParciales.setBounds(20, 115, size4.width, 20);
group.add(DosSemestres);
group.add(CuatroParciales);
group.add(TresParciales);
group.add(DosParciales);
add(SelecMetod);
add(DosSemestres);
add(CuatroParciales);
add(TresParciales);
add(DosParciales);
DosSemestres.addActionListener(this);
CuatroParciales.addActionListener(this);
TresParciales.addActionListener(this);
DosParciales.addActionListener(this);
//Notas minimas y maximas
LBLMax = new JLabel("Ingrese la nota minima de excelencia academia: ");
LBLMax.setForeground(Color.GREEN);
LBLMin = new JLabel("Ingrese la nota minima para aprobar: ");
LBLMin.setForeground(Color.GREEN);
Dimension Not1 = LBLMax.getPreferredSize();
Dimension Not2 = LBLMin.getPreferredSize();
LBLMax.setBounds(440, 25, Not1.width, 20);
LBLMin.setBounds(440, 90 , Not2.width, 20);
add(LBLMax);
add(LBLMin);
textField = new JTextField("");
textField.setBackground(Color.BLUE);
textField.setForeground(Color.WHITE);
textField_2 = new JTextField("");
textField_2.setBackground(Color.BLUE);
textField_2.setForeground(Color.WHITE);
textField.setBounds(440, 55 , 60, 20);
textField_2.setBounds(440, 120 , 60, 20);
add(textField);
add(textField_2);
mostrar = new JLabel("La nota media es:" );
mostrar.setBounds(440, 140, 180, 30);
add(mostrar);
Elegir = new JButton("Aceptar");
Elegir.setBounds(440, 170, 120, 30);
add(Elegir);
Elegir.addActionListener(this);
//Notas reales
lblNota1 = new JLabel("Ingrese la primera nota:");
lblNota2 = new JLabel("Ingrese la segunda nota:");
lblNota3 = new JLabel("Ingrese la tercera nota:");
lblNota4 = new JLabel("Ingrese la cuarta nota:");
Dimension nota1 = lblNota1.getPreferredSize();
Dimension nota2 = lblNota2.getPreferredSize();
Dimension nota3 = lblNota4.getPreferredSize();
Dimension nota4 = lblNota4.getPreferredSize();
lblNota1.setBounds(30, 220, nota1.width, nota1.height);
lblNota2.setBounds(30, 250, nota2.width, nota2.height);
lblNota3.setBounds(30, 280, 280, nota3.height);
lblNota4.setBounds(30, 310, nota4.width, nota4.height);
textField_1 = new JTextField();
textField_1.setBackground(Color.LIGHT_GRAY);
textField_1.setForeground(Color.BLACK);
textField_1.setBounds(250, 220, 40, 20);
add(textField_1);
textField_3 = new JTextField();
textField_3.setBackground(Color.LIGHT_GRAY);
textField_3.setForeground(Color.BLACK);
textField_3.setBounds(250, 250, 40, 20);
add(textField_3);
textField_4 = new JTextField();
textField_4.setBackground(Color.LIGHT_GRAY);
textField_4.setForeground(Color.BLACK);
textField_4.setBounds(250, 280, 40, 20);
add(textField_4);
textField_4.setVisible(false);
textField_5 = new JTextField();
textField_5.setBackground(Color.LIGHT_GRAY);
textField_5.setForeground(Color.BLACK);
textField_5.setBounds(250, 310, 40, 20);
add(textField_5);
textField_5.setVisible(false);
add(lblNota1);
add(lblNota2);
add(lblNota3);
add(lblNota4);
lblNota3.setVisible(false);
lblNota4.setVisible(false);
//Boton que calculara
Calcular = new JButton("Calcular");
Calcular.setBounds(130, 350 , 160, 40);
add(Calcular);
Calcular.addActionListener(this);
//Promedios
lblSem1 = new JLabel("El promedio del primer semestre es: ");
lblsem2 = new JLabel("El promedio del segundo semestre es: ");
lblSem1.setBounds(440, 220, 240, 40);
add(lblSem1);
lblSem1.setVisible(false);
lblsem2.setBounds(440, 260, 250, 40);
add(lblsem2);
lblsem2.setVisible(false);
AreaSem1 = new JLabel();
AreaSem2 = new JLabel();
AreaSem1.setBounds(520, 220, 80, 50);
AreaSem1.setVisible(false);
add(AreaSem1);
AreaSem2.setBounds(520, 260, 80, 50);
AreaSem2.setVisible(false);
add(AreaSem2);
lblfinal = new JLabel("El promedio final es: ");
lblfinal.setBounds(440, 300, 240, 40);
add(lblfinal);
AreaPromedioFin = new JLabel();
AreaPromedioFin.setBounds(520, 300, 90, 40);
add(AreaPromedioFin);
Clasi = new JLabel();
Clasi.setBackground(Color.BLUE);
Clasi.setBounds(520, 350, 250, 50);
AreaPromedioFin.setBounds(520, 360, 180, 40);
add(Clasi);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Elegir){
double min = (Double.parseDouble(textField.getText()));
double aprobado = (Double.parseDouble(textField_2.getText()));
double R = ((min + aprobado)/2);
mostrar.setText("La nota media es: "+String.valueOf(R));}
if(e.getSource() == DosSemestres){
lblNota3.setVisible(true);
lblNota4.setVisible(true);
textField_5.setVisible(true);
textField_4.setVisible(true);
lblSem1.setVisible(true);
lblsem2.setVisible(true);
AreaSem1.setVisible(true);
AreaSem2.setVisible(true);
double nota1 = (Double.parseDouble(textField_1.getText()));
double nota2 = (Double.parseDouble(textField_3.getText()));
double sem1 = (nota1+nota2)/2;
double nota3 = (Double.parseDouble(textField_4.getText()));
double nota4 = (Double.parseDouble(textField_5.getText()));
double sem2 = (nota3+nota4)/2;
double fin = (nota1+nota2+nota3+nota4)/4;
if(e.getSource() == Calcular){
AreaSem1.setText(String.valueOf(sem1));
AreaSem2.setText(String.valueOf(sem2));
AreaPromedioFin.setText(String.valueOf(fin));
}
else if(e.getSource() == CuatroParciales){
lblNota3.setVisible(true);
lblNota4.setVisible(true);
textField_5.setVisible(true);
textField_4.setVisible(true);
lblSem1.setVisible(false);
lblsem2.setVisible(false);
AreaSem1.setVisible(false);
AreaSem2.setVisible(false);
double nota1_1 = (Double.parseDouble(textField_1.getText()));
double nota2_1 = (Double.parseDouble(textField_3.getText()));
double nota3_1 = (Double.parseDouble(textField_4.getText()));
double nota4_1 = (Double.parseDouble(textField_5.getText()));
double fin_1 = (nota1+nota2+nota3+nota4)/4;
if(e.getSource() == Calcular){
AreaPromedioFin.setText(String.valueOf(fin));}
}
else if(e.getSource() == TresParciales){
lblNota3.setVisible(true);
lblNota4.setVisible(false);
textField_5.setVisible(false);
textField_4.setVisible(true);
lblSem1.setVisible(false);
lblsem2.setVisible(false);
AreaSem1.setVisible(false);
AreaSem2.setVisible(false);
double nota1_2 = (Double.parseDouble(textField_1.getText()));
double nota2_2 = (Double.parseDouble(textField_3.getText()));
double nota3_2 = (Double.parseDouble(textField_4.getText()));
double fin_2 = (nota1+nota2+nota3)/3;
if(e.getSource() == Calcular){
AreaPromedioFin.setText(String.valueOf(fin));}
}
else if(e.getSource() == DosParciales){
lblNota3.setVisible(false);
lblNota4.setVisible(false);
textField_5.setVisible(false);
textField_4.setVisible(false);
lblSem1.setVisible(false);
lblsem2.setVisible(false);
AreaSem1.setVisible(false);
AreaSem2.setVisible(false);
double nota1_3 = (Double.parseDouble(textField_1.getText()));
double nota2_3 = (Double.parseDouble(textField_3.getText()));
double fin_3 = (nota1+nota2)/2;
if(e.getSource() == Calcular){
AreaPromedioFin.setText(String.valueOf(fin));}
}
}
}
}

double nota1 = (Double.parseDouble(textField_1.getText())); is causing a potential java.lang.NumberFormatException if the field is empty. You should be using textField_1.getText().isEmpty() to check if the field has any text at all. You should also be trapping the NumberFormatException in case the user inputs an invalid value.
A JFormattedTextField or JSpinner will help reduce some of theses issues. Have a look at How to Use Formatted Text Fields and How to Use Spinners for more details.
When the user selects 1. Dos semestres de dos parciales cada uno, you show new fields, but you try and process those fields before the user has entered anything into them which is further causing NumberFormatException
The following...
if (e.getSource() == DosSemestres) {
//...
if (e.getSource() == Calcular) {
Will never work. The event can only have a single source, so it will either be DosSemestres OR Calcular, but never both. This seems to be a significant source of your problem, along with the previous comments.
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
You might also like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others

Related

Trying to Update Values of JThermometer and JSliders Dynamically in a for loop

I am new in java and trying to create a dashboard where I have N number of sensors and now i wants to show values of Humidity and temperature of each sensor in a separate panels . So firstly i have created N number of panels containing a JSlider and JThermometer for Humidity and temperature each. Now in a loop it reads the lines of outputs from arduino and i am trying to provide that values to that thermometers and sliders .
But i am not able to differentiate that output for which panel it is goin to set . even when i tried to set values to them it is not working at all.
Kindly give any idea how can i implement.
Thank you for help.
Java version - 18.0.1.1
IDE - Apache netbeans 14
static JTextField txtTemp, txtMois;
static JLabel lblTemp, lblMois;
static JSlider slider1, slider2;
static JThermometer th1, th2 ;
static JPanel JP1, JP ;
public void interfaces(){
///Here number of panels is 2
if(count1 > 0){
for(int i = 0 ; i<=count1 ; i++){
JP1 = myPanel(i,0,0);
add(JP1);
}
}
setVisible(true);
setTitle("Temperature and Humidity");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(1200,800);
getContentPane().setBackground(Color.lightGray);
setMinimumSize(new Dimension(350,450));
setLayout(null);
SerialPort[] AvailablePorts = SerialPort.getCommPorts();
SerialPort currPort = AvailablePorts[0];
for (SerialPort S : AvailablePorts) {
System.out.println("\n " + S.toString());
currPort = S;
}
int BaudRate = 9600;
int DataBits = 8;
int StopBits = SerialPort.ONE_STOP_BIT;
int Parity = SerialPort.NO_PARITY;
System.out.println("\n " + currPort.getSystemPortName());
currPort.setComPortParameters(BaudRate,
DataBits,
StopBits,
Parity);
currPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING,
1000,
0);
currPort.openPort();
if (currPort.isOpen())
{
System.out.println("is Open ");
} else {
System.out.println(" Port not open ");
}
try {
while (true) {
try{input = new BufferedReader(new InputStreamReader(currPort.getInputStream()));
String Line1 = input.readLine();
int count = 0;
String[] Data = Line1.split(" ");
for(int i = 0; i < Data.length; i++){
count +=1;
String[] Final_data = Data[i].split(",");
double temp, mois;
try{ temp = Double.parseDouble(Final_data[0]);
mois = Double.parseDouble(Final_data[1]);
System.out.println("Temparature : " + temp);
System.out.println("Humidity : " + mois);
th1.setValue(temp);
th2.setValue(mois);
slider1.setValue((int) temp);
slider2.setValue((int) mois);
txtTemp.setText(String.valueOf(temp));
txtMois.setText(String.valueOf(mois));
}
catch(NumberFormatException e){
System.out.println(e.toString());
}
}
}
catch(SerialPortTimeoutException ex){
System.out.println(ex);
}
}
} catch (Exception e) {
e.printStackTrace();
}
currPort.closePort();
}
//MyPannel funaction
public static JPanel myPanel(int count, double temp, double mois){
JP = new JPanel();
int width = (count-1)*420;
test = new JTextField(String.valueOf(count));
test.setBounds(0, 0, 200, 20);
JP.add(test);
th1 = new JThermometer();
th1.setBounds(10, 50, 150,300);
th1.setValue(0);
th1.setBackground(new Color(0,0,0,0));
th1.setForeground(Color.white);
String color = th1.getBackground().toString();
slider1 = new JSlider(JSlider.VERTICAL,0,100,0);
slider1.setBounds(110, 60, 50, 230);
slider1.setMinorTickSpacing(2);
slider1.setMajorTickSpacing(10);
slider1.setPaintTicks(true);
slider1.setPaintLabels(true);
slider1.setForeground(Color.white);
slider1.setBackground(new Color(0,0,0,0));
lblTemp = new JLabel("TEMPERATURE",SwingConstants.CENTER);
lblTemp.setBounds(15,30,140,30);
lblTemp.setForeground(Color.WHITE);
lblTemp.setFont(new Font("Arial", Font.BOLD, 20));
lblTemp.setBackground(Color.getColor(color));
txtTemp = new JTextField();
txtTemp.setFont(new Font("Arial", Font.BOLD, 16));
txtTemp.setBounds(115,300,50,30);
th2 = new JThermometer();
th2.setBounds(210, 50, 150, 300);
th2.setValue(0);
th2.setBackground(new Color(0,0,0,0));
th2.setForeground(Color.white);
slider2 = new JSlider(JSlider.VERTICAL,0,100,0);
slider2.setBounds(310, 60, 50, 230);
slider2.setMinorTickSpacing(2);
slider2.setMajorTickSpacing(10);
slider2.setPaintTicks(true);
slider2.setPaintLabels(true);
slider2.setBackground(new Color(0,0,0,0));
slider2.setForeground(Color.white);
lblMois = new JLabel("HUMIDITY",SwingConstants.CENTER);
lblMois.setBounds(215,30,140,30);
lblMois.setFont(new Font("Arial", Font.BOLD, 20));
lblMois.setForeground(Color.WHITE);
txtMois = new JTextField();
txtMois.setFont(new Font("Arial", Font.BOLD, 16));
txtMois.setBounds(315,300,50,30);
JP.add(lblTemp);
JP.add(txtTemp);
JP.add(lblMois);
JP.add(txtMois);
JP.add(th1,BorderLayout.CENTER);
JP.add(slider1);
JP.add(slider2);
JP.add(th2,BorderLayout.CENTER);
JP.setBounds(width, 20, 400, 400);
JP.setVisible(true);
JP.setLayout(new java.awt.BorderLayout());
JP.validate();
//JP.add(txt);
return JP;
}
//and arduino output will be like this..
29.5,56.9 25.5,40.3
29.5,54,8 25.5,40.0
so on...

JButton update label

As in my last question, I declare that I'm very new to coding and Java is my only current language.
I'm building my first entirely independent project in eclipse, using a lot of Java imports.
It is a POS till system, it has 24 menu item buttons which are labelled by taking a string that holds null value to begin with.
I have an admin panel and from this panel I can update the title and price associated with the menu item button.
When I click "set" button relating to that particular menu item button, the string and double for item name and price update properly but title printed on JButton does not change.
What am I doing wrong/missing please?
Code follows:
public class Background implements ActionListener {
double menitprice1 = 0.0;
double menitprice2 = 0.0;
double menitprice3 = 0.0;
double menitprice4 = 0.0;
double menitprice5 = 0.0;
double menitprice6 = 0.0;
double menitprice7 = 0.0;
double menitprice8 = 0.0;
double menitprice9 = 0.0;
double menitprice10 = 0.0;
double menitprice11 = 0.0;
double menitprice12 = 0.0;
double menitprice13 = 0.0;
double menitprice14 = 0.0;
double menitprice15 = 0.0;
double menitprice16 = 0.0;
double menitprice17 = 0.0;
double menitprice18 = 0.0;
double menitprice19 = 0.0;
double menitprice20 = 0.0;
double menitprice21 = 0.0;
double menitprice22 = 0.0;
double menitprice23 = 0.0;
double menitprice24 = 0.0;
String menit1;
String menit2;
String menit3;
String menit4;
String menit5;
String menit6;
String menit7;
String menit8;
String menit9;
String menit10;
String menit11;
String menit12;
String menit13;
String menit14;
String menit15;
String menit16;
String menit17;
String menit18;
String menit19;
String menit20;
String menit21;
String menit22;
String menit23;
String menit24;
String adminPassword = "Password";
int splitParty;
JFrame mainFrame = new JFrame();
JFrame adminFrame = new JFrame();
JFrame payFrame = new JFrame();
JFrame splitFrame = new JFrame();
JFrame loginFrame = new JFrame();
Panel mainPanel = new Panel();
Panel adminPanel = new Panel();
Panel payPanel = new Panel();
Panel splitPanel = new Panel();
Panel loginPanel = new Panel();
JTextArea summaryList = new JTextArea();
JTextArea summaryListTotal = new JTextArea();
JTextArea dueAmount = new JTextArea();
JTextArea splitAmount = new JTextArea("How many people?");
JTextArea totalSplits = new JTextArea();
JTextArea splitInput = new JTextArea();
JTextArea logInPassword = new JTextArea();
JTextArea itemsList = new JTextArea("Menu item:");
JTextArea list1 = new JTextArea("Item 1");
JTextArea list2 = new JTextArea("Item 2");
JTextArea list3 = new JTextArea("Item 3");
JTextArea list4 = new JTextArea("Item 4");
JTextArea list5 = new JTextArea("Item 5");
JTextArea list6 = new JTextArea("Item 6");
JTextArea list7 = new JTextArea("Item 7");
JTextArea list8 = new JTextArea("Item 8");
JTextArea list9 = new JTextArea("Item 9");
JTextArea list10 = new JTextArea("Item 10");
JTextArea list11 = new JTextArea("Item 11");
JTextArea list12 = new JTextArea("Item 12");
JTextArea list13 = new JTextArea("Item 13");
JTextArea list14 = new JTextArea("Item 14");
JTextArea list15 = new JTextArea("Item 15");
JTextArea list16 = new JTextArea("Item 16");
JTextArea list17 = new JTextArea("Item 17");
JTextArea list18 = new JTextArea("Item 18");
JTextArea list19 = new JTextArea("Item 19");
JTextArea list20 = new JTextArea("Item 20");
JTextArea list21 = new JTextArea("Item 21");
JTextArea list22 = new JTextArea("Item 22");
JTextArea list23 = new JTextArea("Item 23");
JTextArea list24 = new JTextArea("Item 24");
JTextArea itemIs = new JTextArea("Item Name");
JTextArea price = new JTextArea("Price");
JTextArea setItem1 = new JTextArea(menit1);
JTextArea setItem2 = new JTextArea(menit2);
JTextArea setItem3 = new JTextArea(menit3);
JTextArea setItem4 = new JTextArea(menit4);
JTextArea setItem5 = new JTextArea(menit5);
JTextArea setItem6 = new JTextArea(menit6);
JTextArea setItem7 = new JTextArea(menit7);
JTextArea setItem8 = new JTextArea(menit8);
JTextArea setItem9 = new JTextArea(menit9);
JTextArea setItem10 = new JTextArea(menit10);
JTextArea setItem11 = new JTextArea(menit11);
JTextArea setItem12 = new JTextArea(menit12);
JTextArea setItem13 = new JTextArea(menit13);
JTextArea setItem14 = new JTextArea(menit14);
JTextArea setItem15 = new JTextArea(menit15);
JTextArea setItem16 = new JTextArea(menit16);
JTextArea setItem17 = new JTextArea(menit17);
JTextArea setItem18 = new JTextArea(menit18);
JTextArea setItem19 = new JTextArea(menit19);
JTextArea setItem20 = new JTextArea(menit20);
JTextArea setItem21 = new JTextArea(menit21);
JTextArea setItem22 = new JTextArea(menit22);
JTextArea setItem23 = new JTextArea(menit23);
JTextArea setItem24 = new JTextArea(menit24);
JTextArea setPrice1 = new JTextArea(String.valueOf(menitprice1));
JTextArea setPrice2 = new JTextArea(String.valueOf(menitprice2));
JTextArea setPrice3 = new JTextArea(String.valueOf(menitprice3));
JTextArea setPrice4 = new JTextArea(String.valueOf(menitprice4));
JTextArea setPrice5 = new JTextArea(String.valueOf(menitprice5));
JTextArea setPrice6 = new JTextArea(String.valueOf(menitprice6));
JTextArea setPrice7 = new JTextArea(String.valueOf(menitprice7));
JTextArea setPrice8 = new JTextArea(String.valueOf(menitprice8));
JTextArea setPrice9 = new JTextArea(String.valueOf(menitprice9));
JTextArea setPrice10 = new JTextArea(String.valueOf(menitprice10));
JTextArea setPrice11 = new JTextArea(String.valueOf(menitprice11));
JTextArea setPrice12 = new JTextArea(String.valueOf(menitprice12));
JTextArea setPrice13 = new JTextArea(String.valueOf(menitprice13));
JTextArea setPrice14 = new JTextArea(String.valueOf(menitprice14));
JTextArea setPrice15 = new JTextArea(String.valueOf(menitprice15));
JTextArea setPrice16 = new JTextArea(String.valueOf(menitprice16));
JTextArea setPrice17 = new JTextArea(String.valueOf(menitprice17));
JTextArea setPrice18 = new JTextArea(String.valueOf(menitprice18));
JTextArea setPrice19 = new JTextArea(String.valueOf(menitprice19));
JTextArea setPrice20 = new JTextArea(String.valueOf(menitprice20));
JTextArea setPrice21 = new JTextArea(String.valueOf(menitprice21));
JTextArea setPrice22 = new JTextArea(String.valueOf(menitprice22));
JTextArea setPrice23 = new JTextArea(String.valueOf(menitprice23));
JTextArea setPrice24 = new JTextArea(String.valueOf(menitprice24));
JButton menuItem1 = new JButton(menit1 + " " + menitprice1);
JButton menuItem2 = new JButton(menit2 + " " + menitprice2);
JButton menuItem3 = new JButton(menit3 + " " + menitprice3);
JButton menuItem4 = new JButton(menit4 + " " + menitprice4);
JButton menuItem5 = new JButton(menit5 + " " + menitprice5);
JButton menuItem6 = new JButton(menit6 + " " + menitprice6);
JButton menuItem7 = new JButton(menit7 + " " + menitprice7);
JButton menuItem8 = new JButton(menit8 + " " + menitprice8);
JButton menuItem9 = new JButton(menit9 + " " + menitprice9);
JButton menuItem10 = new JButton(menit10 + " " + menitprice10);
JButton menuItem11 = new JButton(menit11 + " " + menitprice11);
JButton menuItem12 = new JButton(menit12 + " " + menitprice12);
JButton menuItem13 = new JButton(menit13 + " " + menitprice13);
JButton menuItem14 = new JButton(menit14 + " " + menitprice14);
JButton menuItem15 = new JButton(menit15 + " " + menitprice15);
JButton menuItem16 = new JButton(menit16 + " " + menitprice16);
JButton menuItem17 = new JButton(menit17 + " " + menitprice17);
JButton menuItem18 = new JButton(menit18 + " " + menitprice18);
JButton menuItem19 = new JButton(menit19 + " " + menitprice19);
JButton menuItem20 = new JButton(menit20 + " " + menitprice20);
JButton menuItem21 = new JButton(menit21 + " " + menitprice21);
JButton menuItem22 = new JButton(menit22 + " " + menitprice22);
JButton menuItem23 = new JButton(menit23 + " " + menitprice23);
JButton menuItem24 = new JButton(menit24 + " " + menitprice24);
JButton adminButton = new JButton("Admin Control");
JButton payButton = new JButton("Payment");
JButton cashOpt = new JButton("Cash");
JButton cardOpt = new JButton("Card");
JButton splitOpt = new JButton("Split Payment");
JButton splitCancel = new JButton("Cancel");
JButton splitSubmit = new JButton("Split it");
JButton passwordSubmit = new JButton("Log in");
JButton menItSet1 = new JButton("set");
JButton menItSet2 = new JButton("set");
JButton menItSet3 = new JButton("set");
JButton menItSet4 = new JButton("set");
JButton menItSet5 = new JButton("set");
JButton menItSet6 = new JButton("set");
JButton menItSet7 = new JButton("set");
JButton menItSet8 = new JButton("set");
JButton menItSet9 = new JButton("set");
JButton menItSet10 = new JButton("set");
JButton menItSet11 = new JButton("set");
JButton menItSet12 = new JButton("set");
JButton menItSet13 = new JButton("set");
JButton menItSet14 = new JButton("set");
JButton menItSet15 = new JButton("set");
JButton menItSet16 = new JButton("set");
JButton menItSet17 = new JButton("set");
JButton menItSet18 = new JButton("set");
JButton menItSet19 = new JButton("set");
JButton menItSet20 = new JButton("set");
JButton menItSet21 = new JButton("set");
JButton menItSet22 = new JButton("set");
JButton menItSet23 = new JButton("set");
JButton menItSet24 = new JButton("set");
public Background() {
Border listBorder = BorderFactory.createLineBorder(Color.RED, 3);
Font listFont = new Font("ariel", Font.BOLD, 27);
// Admin Log in
loginFrame.setVisible(false);
loginFrame.setTitle("Indy POS: Admin log in");
loginFrame.setSize(400, 200);
loginFrame.setResizable(false);
loginFrame.add(loginPanel);
loginPanel.setBackground(Color.lightGray);
loginPanel.setLayout(null);
loginPanel.add(logInPassword);
logInPassword.setBounds(100, 50, 200, 50);
logInPassword.setBorder(listBorder);
logInPassword.setFont(listFont);
logInPassword.setBackground(Color.white);
logInPassword.setForeground(Color.lightGray);
loginPanel.add(passwordSubmit);
passwordSubmit.setBounds(125, 112, 150, 50);
passwordSubmit.addActionListener(this);
// Admin area
adminFrame.setVisible(false);
adminFrame.setTitle("Indy POS: Admin Control");
adminFrame.setSize(850, 750);
adminFrame.add(adminPanel);
adminPanel.setBackground(Color.lightGray);
adminPanel.setLayout(null);
adminPanel.add(list1);
list1.setEditable(false);
list1.setBackground(Color.lightGray);
list1.setBorder(listBorder);
list1.setBounds(50, 25, 50, 25);
adminPanel.add(list2);
list2.setEditable(false);
list2.setBackground(Color.lightGray);
list2.setBorder(listBorder);
list2.setBounds(50, 50, 50, 25);
adminPanel.add(list3);
list3.setEditable(false);
list3.setBackground(Color.lightGray);
list3.setBorder(listBorder);
list3.setBounds(50, 75, 50, 25);
adminPanel.add(list4);
list4.setEditable(false);
list4.setBackground(Color.lightGray);
list4.setBorder(listBorder);
list4.setBounds(50, 100, 50, 25);
adminPanel.add(list5);
list5.setEditable(false);
list5.setBackground(Color.lightGray);
list5.setBorder(listBorder);
list5.setBounds(50, 125, 50, 25);
adminPanel.add(list6);
list6.setEditable(false);
list6.setBackground(Color.lightGray);
list6.setBorder(listBorder);
list6.setBounds(50, 150, 50, 25);
adminPanel.add(list7);
list7.setEditable(false);
list7.setBackground(Color.lightGray);
list7.setBorder(listBorder);
list7.setBounds(50, 175, 50, 25);
adminPanel.add(list8);
list8.setEditable(false);
list8.setBackground(Color.lightGray);
list8.setBorder(listBorder);
list8.setBounds(50, 200, 50, 25);
adminPanel.add(list9);
list9.setEditable(false);
list9.setBackground(Color.lightGray);
list9.setBorder(listBorder);
list9.setBounds(50, 225, 50, 25);
adminPanel.add(list10);
list10.setEditable(false);
list10.setBackground(Color.lightGray);
list10.setBorder(listBorder);
list10.setBounds(50, 250, 50, 25);
adminPanel.add(list11);
list11.setEditable(false);
list11.setBackground(Color.lightGray);
list11.setBorder(listBorder);
list11.setBounds(50, 275, 50, 25);
adminPanel.add(list12);
list12.setEditable(false);
list12.setBackground(Color.lightGray);
list12.setBorder(listBorder);
list12.setBounds(50, 300, 50, 25);
adminPanel.add(list13);
list13.setEditable(false);
list13.setBackground(Color.lightGray);
list13.setBorder(listBorder);
list13.setBounds(50, 325, 50, 25);
adminPanel.add(list14);
list14.setEditable(false);
list14.setBackground(Color.lightGray);
list14.setBorder(listBorder);
list14.setBounds(50, 350, 50, 25);
adminPanel.add(list15);
list15.setEditable(false);
list15.setBackground(Color.lightGray);
list15.setBorder(listBorder);
list15.setBounds(50, 375, 50, 25);
adminPanel.add(list16);
list16.setEditable(false);
list16.setBackground(Color.lightGray);
list16.setBorder(listBorder);
list16.setBounds(50, 400, 50, 25);
adminPanel.add(list17);
list17.setEditable(false);
list17.setBackground(Color.lightGray);
list17.setBorder(listBorder);
list17.setBounds(50, 425, 50, 25);
adminPanel.add(list18);
list18.setEditable(false);
list18.setBackground(Color.lightGray);
list18.setBorder(listBorder);
list18.setBounds(50, 450, 50, 25);
adminPanel.add(list19);
list19.setEditable(false);
list19.setBackground(Color.lightGray);
list19.setBorder(listBorder);
list19.setBounds(50, 475, 50, 25);
adminPanel.add(list20);
list20.setEditable(false);
list20.setBackground(Color.lightGray);
list20.setBorder(listBorder);
list20.setBounds(50, 500, 50, 25);
adminPanel.add(list21);
list21.setEditable(false);
list21.setBackground(Color.lightGray);
list21.setBorder(listBorder);
list21.setBounds(50, 525, 50, 25);
adminPanel.add(list22);
list22.setEditable(false);
list22.setBackground(Color.lightGray);
list22.setBorder(listBorder);
list22.setBounds(50, 550, 50, 25);
adminPanel.add(list23);
list23.setEditable(false);
list23.setBackground(Color.lightGray);
list23.setBorder(listBorder);
list23.setBounds(50, 575, 50, 25);
adminPanel.add(list24);
list24.setEditable(false);
list24.setBackground(Color.lightGray);
list24.setBorder(listBorder);
list24.setBounds(50, 600, 50, 25);
adminPanel.add(itemIs);
itemIs.setEditable(false);
itemIs.setBackground(Color.lightGray);
itemIs.setBounds(150, 0, 150, 25);
adminPanel.add(price);
price.setEditable(false);
price.setBackground(Color.lightGray);
price.setBounds(350, 0, 150, 25);
adminPanel.add(setItem1);
setItem1.setBorder(listBorder);
setItem1.setBounds(150, 25, 150, 25);
adminPanel.add(setItem2);
setItem2.setBorder(listBorder);
setItem2.setBounds(150, 50, 150, 25);
adminPanel.add(setItem3);
setItem3.setBorder(listBorder);
setItem3.setBounds(150, 75, 150, 25);
adminPanel.add(setItem4);
setItem4.setBorder(listBorder);
setItem4.setBounds(150, 100, 150, 25);
adminPanel.add(setItem5);
setItem5.setBorder(listBorder);
setItem5.setBounds(150, 125, 150, 25);
adminPanel.add(setItem6);
setItem6.setBorder(listBorder);
setItem6.setBounds(150, 150, 150, 25);
adminPanel.add(setItem7);
setItem7.setBorder(listBorder);
setItem7.setBounds(150, 175, 150, 25);
adminPanel.add(setItem8);
setItem8.setBorder(listBorder);
setItem8.setBounds(150, 200, 150, 25);
adminPanel.add(setItem9);
setItem9.setBorder(listBorder);
setItem9.setBounds(150, 225, 150, 25);
adminPanel.add(setItem10);
setItem10.setBorder(listBorder);
setItem10.setBounds(150, 250, 150, 25);
adminPanel.add(setItem11);
setItem11.setBorder(listBorder);
setItem11.setBounds(150, 275, 150, 25);
adminPanel.add(setItem12);
setItem12.setBorder(listBorder);
setItem12.setBounds(150, 300, 150, 25);
adminPanel.add(setItem13);
setItem13.setBorder(listBorder);
setItem13.setBounds(150, 325, 150, 25);
adminPanel.add(setItem14);
setItem14.setBorder(listBorder);
setItem14.setBounds(150, 350, 150, 25);
adminPanel.add(setItem15);
setItem15.setBorder(listBorder);
setItem15.setBounds(150, 375, 150, 25);
adminPanel.add(setItem16);
setItem16.setBorder(listBorder);
setItem16.setBounds(150, 400, 150, 25);
adminPanel.add(setItem17);
setItem17.setBorder(listBorder);
setItem17.setBounds(150, 425, 150, 25);
adminPanel.add(setItem18);
setItem18.setBorder(listBorder);
setItem18.setBounds(150, 450, 150, 25);
adminPanel.add(setItem19);
setItem19.setBorder(listBorder);
setItem19.setBounds(150, 475, 150, 25);
adminPanel.add(setItem20);
setItem20.setBorder(listBorder);
setItem20.setBounds(150, 500, 150, 25);
adminPanel.add(setItem21);
setItem21.setBorder(listBorder);
setItem21.setBounds(150, 525, 150, 25);
adminPanel.add(setItem22);
setItem22.setBorder(listBorder);
setItem22.setBounds(150, 550, 150, 25);
adminPanel.add(setItem23);
setItem23.setBorder(listBorder);
setItem23.setBounds(150, 575, 150, 25);
adminPanel.add(setItem24);
setItem24.setBorder(listBorder);
setItem24.setBounds(150, 600, 150, 25);
adminPanel.add(setPrice1);
setPrice1.setBorder(listBorder);
setPrice1.setBounds(350, 25, 150, 25);
adminPanel.add(setPrice2);
setPrice2.setBorder(listBorder);
setPrice2.setBounds(350, 50, 150, 25);
adminPanel.add(setPrice3);
setPrice3.setBorder(listBorder);
setPrice3.setBounds(350, 75, 150, 25);
adminPanel.add(setPrice4);
setPrice4.setBorder(listBorder);
setPrice4.setBounds(350, 100, 150, 25);
adminPanel.add(setPrice5);
setPrice5.setBorder(listBorder);
setPrice5.setBounds(350, 125, 150, 25);
adminPanel.add(setPrice6);
setPrice6.setBorder(listBorder);
setPrice6.setBounds(350, 150, 150, 25);
adminPanel.add(setPrice7);
setPrice7.setBorder(listBorder);
setPrice7.setBounds(350, 175, 150, 25);
adminPanel.add(setPrice8);
setPrice8.setBorder(listBorder);
setPrice8.setBounds(350, 200, 150, 25);
adminPanel.add(setPrice9);
setPrice9.setBorder(listBorder);
setPrice9.setBounds(350, 225, 150, 25);
adminPanel.add(setPrice10);
setPrice10.setBorder(listBorder);
setPrice10.setBounds(350, 250, 150, 25);
adminPanel.add(setPrice11);
setPrice11.setBorder(listBorder);
setPrice11.setBounds(350, 275, 150, 25);
adminPanel.add(setPrice12);
setPrice12.setBorder(listBorder);
setPrice12.setBounds(350, 300, 150, 25);
adminPanel.add(setPrice13);
setPrice13.setBorder(listBorder);
setPrice13.setBounds(350, 325, 150, 25);
adminPanel.add(setPrice14);
setPrice14.setBorder(listBorder);
setPrice14.setBounds(350, 350, 150, 25);
adminPanel.add(setPrice15);
setPrice15.setBorder(listBorder);
setPrice15.setBounds(350, 375, 150, 25);
adminPanel.add(setPrice16);
setPrice16.setBorder(listBorder);
setPrice16.setBounds(350, 400, 150, 25);
adminPanel.add(setPrice17);
setPrice17.setBorder(listBorder);
setPrice17.setBounds(350, 425, 150, 25);
adminPanel.add(setPrice18);
setPrice18.setBorder(listBorder);
setPrice18.setBounds(350, 450, 150, 25);
adminPanel.add(setPrice19);
setPrice19.setBorder(listBorder);
setPrice19.setBounds(350, 475, 150, 25);
adminPanel.add(setPrice20);
setPrice20.setBorder(listBorder);
setPrice20.setBounds(350, 500, 150, 25);
adminPanel.add(setPrice21);
setPrice21.setBorder(listBorder);
setPrice21.setBounds(350, 525, 150, 25);
adminPanel.add(setPrice22);
setPrice22.setBorder(listBorder);
setPrice22.setBounds(350, 550, 150, 25);
adminPanel.add(setPrice23);
setPrice23.setBorder(listBorder);
setPrice23.setBounds(350, 575, 150, 25);
adminPanel.add(setPrice24);
setPrice24.setBorder(listBorder);
setPrice24.setBounds(350, 600, 150, 25);
adminPanel.add(menItSet1);
menItSet1.setBounds(550, 25, 100, 25);
adminPanel.add(menItSet2);
menItSet2.setBounds(550, 50, 100, 25);
adminPanel.add(menItSet3);
menItSet3.setBounds(550, 75, 100, 25);
adminPanel.add(menItSet4);
menItSet4.setBounds(550, 100, 100, 25);
adminPanel.add(menItSet5);
menItSet5.setBounds(550, 125, 100, 25);
adminPanel.add(menItSet6);
menItSet6.setBounds(550, 150, 100, 25);
adminPanel.add(menItSet7);
menItSet7.setBounds(550, 175, 100, 25);
adminPanel.add(menItSet8);
menItSet8.setBounds(550, 200, 100, 25);
adminPanel.add(menItSet9);
menItSet9.setBounds(550, 225, 100, 25);
adminPanel.add(menItSet10);
menItSet10.setBounds(550, 250, 100, 25);
adminPanel.add(menItSet11);
menItSet11.setBounds(550, 275, 100, 25);
adminPanel.add(menItSet12);
menItSet12.setBounds(550, 300, 100, 25);
adminPanel.add(menItSet13);
menItSet13.setBounds(550, 325, 100, 25);
adminPanel.add(menItSet14);
menItSet14.setBounds(550, 350, 100, 25);
adminPanel.add(menItSet15);
menItSet15.setBounds(550, 375, 100, 25);
adminPanel.add(menItSet16);
menItSet16.setBounds(550, 400, 100, 25);
adminPanel.add(menItSet17);
menItSet17.setBounds(550, 425, 100, 25);
adminPanel.add(menItSet18);
menItSet18.setBounds(550, 450, 100, 25);
adminPanel.add(menItSet19);
menItSet19.setBounds(550, 475, 100, 25);
adminPanel.add(menItSet20);
menItSet20.setBounds(550, 500, 100, 25);
adminPanel.add(menItSet21);
menItSet21.setBounds(550, 525, 100, 25);
adminPanel.add(menItSet22);
menItSet22.setBounds(550, 550, 100, 25);
adminPanel.add(menItSet23);
menItSet23.setBounds(550, 575, 100, 25);
adminPanel.add(menItSet24);
menItSet24.setBounds(550, 600, 100, 25);
menItSet1.addActionListener(this);
menItSet2.addActionListener(this);
menItSet3.addActionListener(this);
menItSet4.addActionListener(this);
menItSet5.addActionListener(this);
menItSet6.addActionListener(this);
menItSet7.addActionListener(this);
menItSet8.addActionListener(this);
menItSet9.addActionListener(this);
menItSet10.addActionListener(this);
menItSet11.addActionListener(this);
menItSet12.addActionListener(this);
menItSet13.addActionListener(this);
menItSet14.addActionListener(this);
menItSet15.addActionListener(this);
menItSet16.addActionListener(this);
menItSet17.addActionListener(this);
menItSet18.addActionListener(this);
menItSet19.addActionListener(this);
menItSet20.addActionListener(this);
menItSet21.addActionListener(this);
menItSet22.addActionListener(this);
menItSet23.addActionListener(this);
menItSet24.addActionListener(this);
//Payment area
payFrame.setVisible(false);
payFrame.setTitle("Indy POS: Payment");
payFrame.setSize(600, 300);
payFrame.setResizable(false);
payFrame.add(payPanel);
payPanel.setBackground(Color.lightGray);
payPanel.setLayout(null);
payPanel.add(dueAmount);
dueAmount.setBounds(100, 50, 250, 100);
dueAmount.setBorder(listBorder);
dueAmount.setFont(listFont);
dueAmount.setBackground(Color.BLACK);
dueAmount.setForeground(Color.WHITE);
dueAmount.setEditable(false);
payPanel.add(totalSplits);
totalSplits.setBounds(400, 50, 150, 100);
totalSplits.setBorder(listBorder);
totalSplits.setFont(listFont);
totalSplits.setBackground(Color.BLACK);
totalSplits.setForeground(Color.WHITE);
totalSplits.setEditable(false);
payPanel.add(cashOpt);
cashOpt.setBounds(100, 200, 100, 50);
payPanel.add(cardOpt);
cardOpt.setBounds(250, 200, 100, 50);
payPanel.add(splitOpt);
splitOpt.setBounds(400, 200, 150, 50);
cashOpt.addActionListener(this);
cardOpt.addActionListener(this);
splitOpt.addActionListener(this);
// Split area
splitFrame.setVisible(false);
splitFrame.setTitle("Indy POS: Split Order");
splitFrame.setSize(450, 200);
splitFrame.setResizable(false);
splitFrame.add(splitPanel);
splitPanel.setLayout(null);
splitPanel.setBackground(Color.lightGray);
splitPanel.add(splitAmount);
splitAmount.setBounds(20, 25, 260, 47);
splitAmount.setEditable(false);
splitAmount.setBackground(Color.lightGray);
splitAmount.setFont(listFont);
splitAmount.setForeground(Color.BLACK);
splitPanel.add(splitInput);
splitInput.setBorder(listBorder);
splitInput.setFont(listFont);
splitInput.setBackground(Color.BLACK);
splitInput.setForeground(Color.WHITE);
splitInput.setBounds(100, 75, 100, 50);
splitPanel.add(splitSubmit);
splitSubmit.setBounds(250, 75, 100, 50);
splitPanel.add(splitCancel);
splitCancel.setBounds(100, 175, 100, 50);
// Cash area
// Card area
// Main area
mainFrame.setVisible(true);
mainFrame.setTitle("Indy POS");
mainFrame.setSize(1000, 750);
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(mainPanel);
mainPanel.setBackground(Color.lightGray);
mainPanel.setLayout(null);
mainPanel.add(summaryList);
summaryList.setBounds(100, 50, 300, 575);
summaryList.setBorder(listBorder);
summaryList.setBackground(Color.BLACK);
summaryList.setEditable(false);
summaryList.setFont(listFont);
summaryList.setForeground(Color.WHITE);
mainPanel.add(summaryListTotal);
summaryListTotal.setBounds(100, 650, 150, 50);
summaryListTotal.setBorder(listBorder);
summaryListTotal.setBackground(Color.BLACK);
summaryListTotal.setEditable(false);
summaryListTotal.setFont(listFont);
summaryListTotal.setForeground(Color.WHITE);
mainPanel.add(payButton);
payButton.setBounds(300, 650, 100, 50);
mainPanel.add(menuItem1);
menuItem1.setBounds(500, 50, 100, 50);
mainPanel.add(menuItem2);
menuItem2.setBounds(650, 50, 100, 50);
mainPanel.add(menuItem3);
menuItem3.setBounds(800, 50, 100, 50);
mainPanel.add(menuItem4);
menuItem4.setBounds(500, 125, 100, 50);
mainPanel.add(menuItem5);
menuItem5.setBounds(650, 125, 100, 50);
mainPanel.add(menuItem6);
menuItem6.setBounds(800, 125, 100, 50);
mainPanel.add(menuItem7);
menuItem7.setBounds(500, 200, 100, 50);
mainPanel.add(menuItem8);
menuItem8.setBounds(650, 200, 100, 50);
mainPanel.add(menuItem9);
menuItem9.setBounds(800, 200, 100, 50);
mainPanel.add(menuItem10);
menuItem10.setBounds(500, 275, 100, 50);
mainPanel.add(menuItem11);
menuItem11.setBounds(650, 275, 100, 50);
mainPanel.add(menuItem12);
menuItem12.setBounds(800, 275, 100, 50);
mainPanel.add(menuItem13);
menuItem13.setBounds(500, 350, 100, 50);
mainPanel.add(menuItem14);
menuItem14.setBounds(650, 350, 100, 50);
mainPanel.add(menuItem15);
menuItem15.setBounds(800, 350, 100, 50);
mainPanel.add(menuItem16);
menuItem16.setBounds(500, 425, 100, 50);
mainPanel.add(menuItem17);
menuItem17.setBounds(650, 425, 100, 50);
mainPanel.add(menuItem18);
menuItem18.setBounds(800, 425, 100, 50);
mainPanel.add(menuItem19);
menuItem19.setBounds(500, 500, 100, 50);
mainPanel.add(menuItem20);
menuItem20.setBounds(650, 500, 100, 50);
mainPanel.add(menuItem21);
menuItem21.setBounds(800, 500, 100, 50);
mainPanel.add(menuItem22);
menuItem22.setBounds(500, 575, 100, 50);
mainPanel.add(menuItem23);
menuItem23.setBounds(650, 575, 100, 50);
mainPanel.add(menuItem24);
menuItem24.setBounds(800, 575, 100, 50);
mainPanel.add(adminButton);
adminButton.setBounds(750, 650, 150, 50);
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
menuItem4.addActionListener(this);
menuItem5.addActionListener(this);
menuItem6.addActionListener(this);
menuItem7.addActionListener(this);
menuItem8.addActionListener(this);
menuItem9.addActionListener(this);
menuItem10.addActionListener(this);
menuItem11.addActionListener(this);
menuItem12.addActionListener(this);
menuItem13.addActionListener(this);
menuItem14.addActionListener(this);
menuItem15.addActionListener(this);
menuItem16.addActionListener(this);
menuItem17.addActionListener(this);
menuItem18.addActionListener(this);
menuItem19.addActionListener(this);
menuItem20.addActionListener(this);
menuItem21.addActionListener(this);
menuItem22.addActionListener(this);
menuItem23.addActionListener(this);
menuItem24.addActionListener(this);
adminButton.addActionListener(this);
payButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == payButton){
payFrame.setVisible(true);
}
if (source == splitOpt){
splitFrame.setVisible(true);
}
if (source == adminButton){
loginFrame.setVisible(true);
splitFrame.setVisible(false);
payFrame.setVisible(false);
}
if (source == passwordSubmit){
if (logInPassword.getText().equals(adminPassword)){
loginFrame.setVisible(false);
adminFrame.setVisible(true);
}
else logInPassword.setText(null);
logInPassword.append("Incorrect");
}
if (source == menItSet1){
menit1.equals(setItem1.getText());
menitprice1 = Double.parseDouble(setPrice1.getText());
}
}
}
You could just create a String and then pass the value from that String into your action listener. Please find underneath a local String (should be an Instance, but for testing I have made a local one). And then I have passed it to the button.
if (source == menItSet1){
String text = setItem1.getText();
menitprice1 = Double.parseDouble(setPrice1.getText());
menuItem1.setText(text + " " + menitprice1);
}

How to append a scrollbar for the window?

How to append a scroll pane for my window?
The program compiles properly, but the scroll pane for the window is not created. I really don't know why this is happening. I defined JScrollPane and even implemented it with scrollPane = new JScrollPane
Where is my mistake?
Below is my code:
import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
public class RegForm extends JFrame implements ItemListener{
JLabel l0,li,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13;
JButton b1,b2,b3,b4,b5;
JTextField t1,t2,t3,t4,t5,t6,t7,t8;
JTextArea a1,a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton( " Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox (" C ");
JCheckBox cb2 = new JCheckBox (" C++ ");
JCheckBox cb3 = new JCheckBox (" Java ");
JCheckBox cb4 = new JCheckBox (" Oracle ");
JCheckBox cb5 = new JCheckBox (" Android ");
JCheckBox cb6 = new JCheckBox (" iOS ");
JCheckBox cb7 = new JCheckBox (" Web Designing ");
JCheckBox cb8 = new JCheckBox (" .Net ");
JCheckBox cb9 = new JCheckBox (" Same as Contact Address ");
JScrollPane scrollPane = new JScrollPane();
RegForm()
{
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian",Font.ITALIC,20);
l0.setFont(f0);
l0.setBounds(600,10,250,50);
scrollPane.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting",Font.PLAIN,17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10,50,150,30);
scrollPane.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style",Font.PLAIN,12);
l1.setFont(f1);
l1.setBounds(10,70,100,50);
scrollPane.add(l1);
t1 = new JTextField(20);
t1.setBounds(165,85,140,20);
scrollPane.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10,100,150,50);
scrollPane.add(l2);
t2 = new JTextField(20);
t2.setBounds(165,115,140,20);
scrollPane.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15,130,120,50);
scrollPane.add(l3);
t3 = new JTextField(20);
t3.setBounds(165,145,140,20);
scrollPane.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15,160,150,50);
scrollPane.add(l4);
t4 = new JTextField(20);
t4.setBounds(165,175,140,20);
scrollPane.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10,190,100,50);
scrollPane.add(l5);
t5 = new JTextField(20);
t5.setBounds(165,205,140,20);
scrollPane.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10,220,150,50);
scrollPane.add(l6);
t6 = new JTextField(20);
t6.setBounds(165,235,140,20);
scrollPane.add(t6);
l7 = new JLabel(" * DD / MM / YYYY" );
Font f2 = new Font(" Comic Sans MS ",Font.ITALIC,12);
l7.setFont(f2);
l7.setBounds(10,260,150,50);
scrollPane.add(l7);
for(int j=1;j<=31;j++)
dd.addItem(new Integer(j));
dd.setBounds(165,275,47,20);
scrollPane.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212,275,90,20);
scrollPane.add(mm);
mm.addItemListener(this);
for(int i=1990;i<=2016;i++)
yyyy.addItem(new Integer(i));
yyyy.setBounds(302,275,70,20);
scrollPane.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15,290,50,50);
scrollPane.add(l8);
t8 = new JTextField(10);
t8.setBounds(165,305,50,20);
scrollPane.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15,320,120,50);
scrollPane.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165,335,100,20);
scrollPane.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15,360,80,50);
scrollPane.add(l10);
rb1.setBounds(165,365,80,39);
rb2.setBounds(250,365,80,39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
scrollPane.add(rb1);
scrollPane.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15,450,150,50);
scrollPane.add(l11);
cb1.setBounds(165,390,100,50);
scrollPane.add(cb1);
cb2.setBounds(285,390,100,50);
scrollPane.add(cb2);
cb3.setBounds(165,425,100,50);
scrollPane.add(cb3);
cb4.setBounds(285,425,100,50);
scrollPane.add(cb4);
cb5.setBounds(165,460,100,50);
scrollPane.add(cb5);
cb6.setBounds(285,460,100,50);
scrollPane.add(cb6);
cb7.setBounds(165,495,100,50);
scrollPane.add(cb7);
cb8.setBounds(285,495,100,50);
scrollPane.add(cb8);
cb9.setBounds(15,630,200,50);
scrollPane.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15,550,150,50);
scrollPane.add(l12);
a1 = new JTextArea (5,20);
a1.setBounds(165,545,250,80);
scrollPane.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15,675,150,50);
scrollPane.add(l13);
a2 = new JTextArea (5,20);
a2.setBounds(165,680,250,80);
scrollPane.add(a2);
cb9.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource() == yyyy){
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016-y));
t8.setEditable(false);
}
if(cb9.isSelected()){
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String[] args)
{
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(10,10,100,100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(scrollPane);
rf.setContentPane(contentPane);
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
scrollPane.add(l11);
Never add components directly to a scroll pane.
l1.setBounds(10,70,100,50);
Don't use setBounds(...). It is the job of the layout manager to set the size/location of the component.
The basic logic would be:
JPanel panel = new JPanel(); // set your layout manager for the panel.
panel.add( someComponent );
panel.add( anotherComponent );
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );
Read the Swing Tutorial for Swing basics. Every section in the tutorial has working examples. Maybe start with the section on How to Use Scroll Panes.
This works, but you'll need to layout the components better..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegForm extends JFrame implements ItemListener {
JLabel l0, li, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13;
JButton b1, b2, b3, b4, b5;
JTextField t1, t2, t3, t4, t5, t6, t7, t8;
JTextArea a1, a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton(" Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox(" C ");
JCheckBox cb2 = new JCheckBox(" C++ ");
JCheckBox cb3 = new JCheckBox(" Java ");
JCheckBox cb4 = new JCheckBox(" Oracle ");
JCheckBox cb5 = new JCheckBox(" Android ");
JCheckBox cb6 = new JCheckBox(" iOS ");
JCheckBox cb7 = new JCheckBox(" Web Designing ");
JCheckBox cb8 = new JCheckBox(" .Net ");
JCheckBox cb9 = new JCheckBox(" Same as Contact Address ");
JScrollPane scrollPane;
JPanel panel = new JPanel(new GridLayout(0,1));
RegForm() {
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian", Font.ITALIC, 20);
l0.setFont(f0);
l0.setBounds(600, 10, 250, 50);
panel.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting", Font.PLAIN, 17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10, 50, 150, 30);
panel.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style", Font.PLAIN, 12);
l1.setFont(f1);
l1.setBounds(10, 70, 100, 50);
panel.add(l1);
t1 = new JTextField(20);
t1.setBounds(165, 85, 140, 20);
panel.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10, 100, 150, 50);
panel.add(l2);
t2 = new JTextField(20);
t2.setBounds(165, 115, 140, 20);
panel.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15, 130, 120, 50);
panel.add(l3);
t3 = new JTextField(20);
t3.setBounds(165, 145, 140, 20);
panel.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15, 160, 150, 50);
panel.add(l4);
t4 = new JTextField(20);
t4.setBounds(165, 175, 140, 20);
panel.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10, 190, 100, 50);
panel.add(l5);
t5 = new JTextField(20);
t5.setBounds(165, 205, 140, 20);
panel.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10, 220, 150, 50);
panel.add(l6);
t6 = new JTextField(20);
t6.setBounds(165, 235, 140, 20);
panel.add(t6);
l7 = new JLabel(" * DD / MM / YYYY");
Font f2 = new Font(" Comic Sans MS ", Font.ITALIC, 12);
l7.setFont(f2);
l7.setBounds(10, 260, 150, 50);
panel.add(l7);
for (int j = 1; j <= 31; j++) {
dd.addItem(new Integer(j));
}
dd.setBounds(165, 275, 47, 20);
panel.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212, 275, 90, 20);
panel.add(mm);
mm.addItemListener(this);
for (int i = 1990; i <= 2016; i++) {
yyyy.addItem(new Integer(i));
}
yyyy.setBounds(302, 275, 70, 20);
panel.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15, 290, 50, 50);
panel.add(l8);
t8 = new JTextField(10);
t8.setBounds(165, 305, 50, 20);
panel.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15, 320, 120, 50);
panel.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165, 335, 100, 20);
panel.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15, 360, 80, 50);
panel.add(l10);
rb1.setBounds(165, 365, 80, 39);
rb2.setBounds(250, 365, 80, 39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
panel.add(rb1);
panel.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15, 450, 150, 50);
panel.add(l11);
cb1.setBounds(165, 390, 100, 50);
panel.add(cb1);
cb2.setBounds(285, 390, 100, 50);
panel.add(cb2);
cb3.setBounds(165, 425, 100, 50);
panel.add(cb3);
cb4.setBounds(285, 425, 100, 50);
panel.add(cb4);
cb5.setBounds(165, 460, 100, 50);
panel.add(cb5);
cb6.setBounds(285, 460, 100, 50);
panel.add(cb6);
cb7.setBounds(165, 495, 100, 50);
panel.add(cb7);
cb8.setBounds(285, 495, 100, 50);
panel.add(cb8);
cb9.setBounds(15, 630, 200, 50);
panel.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15, 550, 150, 50);
panel.add(l12);
a1 = new JTextArea(5, 20);
a1.setBounds(165, 545, 250, 80);
panel.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15, 675, 150, 50);
panel.add(l13);
a2 = new JTextArea(5, 20);
a2.setBounds(165, 680, 250, 80);
panel.add(a2);
cb9.addItemListener(this);
scrollPane = new JScrollPane(panel);
add(scrollPane);
//add(panel);
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == yyyy) {
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016 - y));
t8.setEditable(false);
}
if (cb9.isSelected()) {
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String[] args) {
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
/*JScrollPane panel = new JScrollPane();
panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setBounds(10, 10, 100, 100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(panel);
rf.setContentPane(contentPane);*/
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
Notes:
As mentioned by #camickr, setting the bounds of the components won't work reliably. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Please try to solve programming errors before you've got to dozens of components! For simple testing of layouts with many components, just add dummy components in a loop. To put that another way: For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
Safer to use logical font names that specific one, e.g. Algerian will only work on systems with that font installed!

Passing Parameters to the print method (JAVA)

I need some help with my code. What i need it to do is everytime a page is added to the Java Book different data needs to be on the page every time. I've tried it a number of different ways and i just can't work it out!
Here's my code:
dataBase data = new dataBase();
int userCountAmount = data.getAmountOfUsers();
Book bk = new Book();
PrinterJob PJ = PrinterJob.getPrinterJob();
String[] pupilName = new String[userCountAmount];
String[] parentsName = new String[userCountAmount];
int parCount = 0;
int pupCount = 0;
public void print2() {
System.out.println(pupilName.length);
System.out.println(parentsName.length);
System.out.println(userCountAmount);
String[] custData = processData(data.getAllCustomers());
PageFormat portrait = PJ.defaultPage();
int pupNameCount = 0;
int parNameCount = 0;
portrait.setOrientation(PageFormat.PORTRAIT);
for (int i = 0; i < userCountAmount; i++) {
pupilName[i] = custData[pupNameCount];
parentsName[i] = custData[parNameCount];
System.out.println(custData[pupNameCount] + " " + custData[parNameCount]);
pupNameCount = pupNameCount + 13;
parNameCount = parNameCount + 13;
bk.append(new IntroPage(), PJ.defaultPage());
parCount++;
pupCount++;
System.out.println(parCount+" " + pupCount);
}
// setWindow();
//PageFormat PF = PJ.pageDialog(PJ.defaultPage());
PJ.setPageable(bk);
// PJ.setPrintable((Printable) this);
boolean doPrint = PJ.printDialog();
//JOptionPane.showMessageDialog(null, doPrint);
if (doPrint) {
try {
PJ.print();
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
private class IntroPage implements Printable {
/**
* Method: print
* <p>
*
* #param g
* a value of type Graphics
* #param pageFormat
* a value of type PageFormat
* #param page
* a value of type int
* #return a value of type int
*/
public int print(Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//--- Set the default drawing color to black and get date
g2d.setPaint(Color.black);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
//draw tables
g2d.setPaint(Color.darkGray);
Rectangle2D.Double invoiceOut = new Rectangle2D.Double(10, 200, 400, 280);
Rectangle2D.Double invoiceDesc = new Rectangle2D.Double(10, 200, 200, 280);
Rectangle2D.Double invoiceSess = new Rectangle2D.Double(10, 200, 260, 280);
Rectangle2D.Double invoiceRate = new Rectangle2D.Double(10, 200, 330, 280);
Rectangle2D.Double invoiceTitle = new Rectangle2D.Double(10, 200, 400, 20);
Rectangle2D.Double totalAmount = new Rectangle2D.Double(340, 480, 70, 20);
g2d.draw(invoiceOut);
g2d.draw(invoiceDesc);
g2d.draw(invoiceSess);
g2d.draw(invoiceRate);
g2d.draw(invoiceTitle);
g2d.draw(totalAmount);
//table title strings
String descrp = "Description:";
String sesh = "Sessions:";
String rate = "Rate:";
String amount = "Amount:";
String titleText = "INVOICE";
String totalAmountString = "Total Amount:";
//Address Strings
String printDate = "Print Date: " + String.valueOf(dateFormat.format(date));
String line1Text = "16 here now";
String line2Text = "There";
String line3Text = "Thisshire";
String line4Text = "GU66 74S";
String phoneText = "Phone: 010101 0101010";
String mobileText = "Mobile: 010101 010101";
String emailText = "Email: here#there.com";
//to/for strings
String toString = "To: " + pupilName[pupCount-1];
String forString = "For: " + parentsName[parCount-1];
//footer strings
String footerLine1 = "Please pay by cash or cheque made payable to " + " " + " or by Internet Banking.";
String footerBold1 = "Mrs Bob Bobbins";
String iBankingDet = "company Sort code: " + " " + " Account Number: " + " " + "put your name as";
String bSortCode = "00-00-00";
String bAccountNumber = "0000000";
String iBankingDet2 = "reference!";
String noticeAlert = "Please Pay by latest on the first lesson of Term/Series.";
String customNotice = "** Thank you for your custom **";
//Set fonts
Font textFont = new Font("Tahoma", Font.PLAIN, 10);
Font toForFont = new Font("Tahoma", Font.BOLD, 10);
Font addressFont = new Font("Tahoma", Font.PLAIN, 8);
Font titleFont = new Font("Tahoma", Font.BOLD, 24);
Font textFontBold = new Font("Tahoma", Font.BOLD, 10);
//set table titles
g2d.setPaint(Color.GRAY);
g2d.setFont(addressFont);
g2d.drawString(descrp, 15, 215);
g2d.drawString(sesh, 215, 215);
g2d.drawString(rate, 275, 215);
g2d.drawString(amount, 345, 215);
g2d.drawString(totalAmountString, 285, 495);
//set title
g2d.setFont(titleFont);
g2d.drawString(titleText, 250, 20);
//set address
g2d.setFont(addressFont);
g2d.drawString(line1Text, 350, 40);
g2d.drawString(line2Text, 350, 50);
g2d.drawString(line3Text, 350, 60);
g2d.drawString(line4Text, 350, 70);
g2d.drawString(phoneText, 350, 80);
g2d.drawString(mobileText, 350, 90);
g2d.drawString(emailText, 350, 100);
g2d.drawString(printDate, 350, 120);
//draw to and for strings
g2d.setPaint(Color.darkGray);
g2d.setFont(toForFont);
g2d.drawString(toString, 10, 160);
g2d.drawString(forString, 180, 160);
//draw footer onto page
g2d.setPaint(Color.black);
g2d.setFont(textFont);
g2d.drawString(footerLine1, 10, 520);
g2d.setFont(textFontBold);
g2d.drawString(footerBold1, 220, 520);
g2d.setFont(textFont);
g2d.drawString(iBankingDet, 10, 545);
g2d.setFont(textFontBold);
g2d.drawString(bSortCode, 165, 545);
g2d.drawString(bAccountNumber, 295, 545);
g2d.setFont(textFont);
g2d.drawString(iBankingDet2, 10, 555);
g2d.setFont(textFontBold);
g2d.drawString(noticeAlert, 95, 575);
g2d.drawString(customNotice, 145, 595);
//add image to invoice
Image img;
img = new ImageIcon(this.getClass().getResource("logo.png")).getImage();
g2d.drawImage(img, -10, -10, 180, 84, null);
return (PAGE_EXISTS);
}
}
So basically, all i need to do is pass additional parameters to the print method, but this is impossible! It wouldn't implement the print class other wise!
I've tried substituting arrays and a lot more, i can't think of anything else!
You can't add arguments to the print() as it must implement the interface.
However you can add any number of arguments to the constructor, and these can be used in your method.
bk.append(new IntroPage(arg1, arg2, arg3), PJ.defaultPage());
You could add fields to the class IntroPage. Then when you create IntroPage pass appropriate arguments to initialize your fileds. And later, in print(), you can use the data from your fileds.

Automatic entry in text field

I have this GUI:
I would like after I enter a number in the Length of hot tub text box for that number to be automatically entered into the Width of hot tub text box, but only if the Round Tub radio button is selected.
public void createHotTubs()
{
hotTubs = new JPanel();
hotTubs.setLayout(null);
labelTubStatus = new JTextArea(6, 30);
hotTubs.add(labelTubStatus);
JLabel lengthLabel = new JLabel(
"Length of hot tub(ft):");
lengthLabel.setBounds(10, 15, 260, 20);
hotTubs.add(lengthLabel);
hotTubLengthText = new JTextField();
hotTubLengthText.setBounds(180, 15, 150, 20);
hotTubs.add(hotTubLengthText);
JLabel widthLabel = new JLabel(
"Width of hot tub(ft):");
widthLabel.setBounds(10, 40, 260, 20);
hotTubs.add(widthLabel);
hotTubWidthText = new JTextField();
hotTubWidthText.setBounds(180, 40, 150, 20);
hotTubs.add(hotTubWidthText);
JLabel depthLabel = new JLabel(
"Average depth the hot tub(ft):");
depthLabel.setBounds(10, 65, 260, 20);
hotTubs.add(depthLabel);
hotTubDepthText = new JTextField();
hotTubDepthText.setBounds(180, 65, 150, 20);
hotTubs.add(hotTubDepthText);
JLabel volumeLabel = new JLabel("The hot tub volume is:(ft ^3");
volumeLabel.setBounds(10, 110, 260, 20);
hotTubs.add(volumeLabel);
hotTubVolumeText = new JTextField();
hotTubVolumeText.setBounds(180, 110, 150, 20);
hotTubVolumeText.setEditable(false);
hotTubs.add(hotTubVolumeText);
final JRadioButton rdbtnRoundTub = new JRadioButton("Round Tub");
rdbtnRoundTub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
hotTubWidthText.setEditable(false);
}
});
rdbtnRoundTub.setSelected(true);
rdbtnRoundTub.setBounds(79, 150, 109, 23);
hotTubs.add(rdbtnRoundTub);
JRadioButton rdbtnOvalTub = new JRadioButton("Oval Tub");
rdbtnOvalTub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
hotTubWidthText.setEditable(true);
}
});
rdbtnOvalTub.setBounds(201, 150, 109, 23);
hotTubs.add(rdbtnOvalTub);
ButtonGroup radioBtnGroup = new ButtonGroup();
radioBtnGroup.add(rdbtnRoundTub);
radioBtnGroup.add(rdbtnOvalTub);
JButton btnCalculateVlmn = new JButton("Calculate Volume");
btnCalculateVlmn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
double width = 0, length = 0, depth = 0, volume = 0;
String lengthString, widthString, depthString;
lengthString = hotTubLengthText.getText();
widthString = hotTubWidthText.getText();
depthString = hotTubDepthText.getText();
depth = Double.valueOf(depthString);
length = Double.valueOf(lengthString);
width = Double.valueOf(widthString);
try
{
if (rdbtnRoundTub.isSelected())
{
volume = length * width * depth;
}
else
{
volume = Math.PI * length * width / 4 * depth;
}
DecimalFormat formatter = new DecimalFormat("#,###,###.###");
hotTubVolumeText.setText("" + formatter.format(volume));
}
catch (NumberFormatException e)
{
labelTubStatus
.setText("Enter all three numbers!!");
}
}
});
Add a focus listener to your length text field when it loses focus and the round tub is selected, compute and set the the width.

Categories

Resources