Related
My teacher wants me to create a Java program that functions like a vending machine. She wants it to have multidimensional arrays and one-dimensional arrays. I already created the program but I failed because it doesn't have any array in it. It works perfectly fine but there are no arrays in it that's why I failed. She gave me another chance until tomorrow morning. I am really bad at using arrays, to be honest, and I don't really know how to use them in GUI apps. Thank you so much in advance for your help. Below is my code and the output of the current program I have so far.
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Cursor;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
public class Finals {
private JFrame frame;
private JTextField itemField;
private JTextField priceField;
private JButton pushButton, button1, button2, button3;
private JRadioButton day1RB, day2RB, day3RB;
private JTextArea resultsTA;
//----Counter Variables for items A1, A2, and A3 for 3 days with 0 as the initial value
int A1d1=0; int A1d2=0; int A1d3=0;
int A2d1=0; int A2d2=0; int A2d3=0;
int A3d1=0; int A3d2=0; int A3d3=0;
//----Amount Variables for items A1, A2, and A3 for 3 days with 0 as the initial value
int A1d1Amount=0; int A1d2Amount=0; int A1d3Amount=0;
int A2d1Amount=0; int A2d2Amount=0; int A2d3Amount=0;
int A3d1Amount=0; int A3d2Amount=0; int A3d3Amount=0;
//----Variables for items B1, B2, and B3 for 3 days with 0 as the initial value
int B1d1=0; int B1d2=0; int B1d3=0;
int B2d1=0; int B2d2=0; int B2d3=0;
int B3d1=0; int B3d2=0; int B3d3=0;
//----Amount Variables for items B1, B2, and B3 for 3 days with 0 as the initial value
int B1d1Amount=0; int B1d2Amount=0; int B1d3Amount=0;
int B2d1Amount=0; int B2d2Amount=0; int B2d3Amount=0;
int B3d1Amount=0; int B3d2Amount=0; int B3d3Amount=0;
//----Variables used for storing total sales for all items for 3 days with 0 as the initial value
double A1total = 0; double A2total=0; double A3total=0;
double B1total = 0; double B2total=0; double B3total=0;
/**
* Launching the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Finals window = new Finals();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Creating the application.
*/
public Finals() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("Vending Machine");
frame.setResizable(false);
frame.setBounds(100, 100, 914, 334);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel itemsPanel = new JPanel();
itemsPanel.setBackground(SystemColor.activeCaption);
itemsPanel.setBounds(10, 11, 264, 191);
frame.getContentPane().add(itemsPanel);
itemsPanel.setLayout(null);
JLabel a1Label = new JLabel();
a1Label.setBounds(10, 11, 60, 57);
ImageIcon a1Icon = new ImageIcon("cookie.png");
a1Label.setIcon(a1Icon);
itemsPanel.add(a1Label);
JLabel a2Label = new JLabel();
a2Label.setBounds(102, 11, 60, 57);
ImageIcon a2Icon = new ImageIcon("gum.png");
a2Label.setIcon(a2Icon);
itemsPanel.add(a2Label);
JLabel a3Label = new JLabel();
a3Label.setBounds(195, 11, 60, 57);
ImageIcon a3Icon = new ImageIcon("pretzel.png");
a3Label.setIcon(a3Icon);
itemsPanel.add(a3Label);
JLabel b1Label = new JLabel("");
b1Label.setBounds(10, 100, 60, 57);
ImageIcon b1Icon = new ImageIcon("pretzel.png");
b1Label.setIcon(b1Icon);
itemsPanel.add(b1Label);
JLabel b2Label = new JLabel();
b2Label.setBounds(102, 100, 60, 57);
ImageIcon b2Icon = new ImageIcon("cookie.png");
b2Label.setIcon(b2Icon);
itemsPanel.add(b2Label);
JLabel b3Label = new JLabel();
b3Label.setBounds(195, 100, 60, 57);
ImageIcon b3Icon = new ImageIcon("soda.png");
b3Label.setIcon(b3Icon);
itemsPanel.add(b3Label);
JLabel a1lbl = new JLabel("A1");
a1lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
a1lbl.setBounds(27, 63, 25, 26);
itemsPanel.add(a1lbl);
JLabel a2lbl = new JLabel("A2");
a2lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
a2lbl.setBounds(120, 63, 25, 26);
itemsPanel.add(a2lbl);
JLabel a3lbl = new JLabel("A3");
a3lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
a3lbl.setBounds(213, 63, 25, 26);
itemsPanel.add(a3lbl);
JLabel b1lbl = new JLabel("B1");
b1lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
b1lbl.setBounds(27, 154, 25, 26);
itemsPanel.add(b1lbl);
JLabel b2lbl = new JLabel("B2");
b2lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
b2lbl.setBounds(120, 154, 25, 26);
itemsPanel.add(b2lbl);
JLabel b3lbl = new JLabel("B3");
b3lbl.setFont(new Font("Tahoma", Font.BOLD, 13));
b3lbl.setBounds(213, 154, 25, 26);
itemsPanel.add(b3lbl);
pushButton = new JButton("PUSH");
pushButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String selectedItem = itemField.getText();
if(selectedItem.equals("A1") && day1RB.isSelected()) {
A1d1Amount=A1d1Amount+10;
A1d1+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
}
else if(selectedItem.equals("A1") && day2RB.isSelected()) {
A1d2Amount=A1d2Amount+10;
A1d2+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
}else if(selectedItem.equals("A1") && day3RB.isSelected()) {
A1d3Amount=A1d3Amount+10;
A1d3+=1; A1total = A1d1Amount+A1d2Amount+A1d3Amount;
}
else if(selectedItem.equals("A2") && day1RB.isSelected()) {
A2d1Amount=A2d1Amount+20;
A2d1+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
}
else if(selectedItem.equals("A2") && day2RB.isSelected()) {
A2d2Amount=A2d2Amount+20;
A2d2+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
}
else if(selectedItem.equals("A2") && day3RB.isSelected()) {
A2d3Amount=A2d3Amount+20;
A2d3+=1; A2total = A2d1Amount+A2d2Amount+A2d3Amount;
}
else if(selectedItem.equals("A3") && day1RB.isSelected()) {
A3d1Amount=A3d1Amount+30;
A3d1+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
}
else if(selectedItem.equals("A3") && day2RB.isSelected()) {
A3d2Amount=A3d2Amount+30;
A3d2+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
}
else if(selectedItem.equals("A3") && day3RB.isSelected()) {
A3d3Amount=A3d3Amount+30;
A3d3+=1; A3total = A3d1Amount+A3d2Amount+A3d3Amount;
}
else if(selectedItem.equals("B1") && day1RB.isSelected()) {
B1d1Amount=B1d1Amount+15;
B1d1+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
}
else if(selectedItem.equals("B1") && day2RB.isSelected()) {
B1d2Amount=B1d2Amount+15;
B1d2+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
}
else if(selectedItem.equals("B1") && day3RB.isSelected()) {
B1d3Amount=B1d3Amount+15;
B1d3+=1; B1total = B1d1Amount+B1d2Amount+B1d3Amount;
}
else if(selectedItem.equals("B2") && day1RB.isSelected()) {
B2d1Amount=B2d1Amount+25;
B2d1+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
}
else if(selectedItem.equals("B2") && day2RB.isSelected()) {
B2d2Amount=B2d2Amount+25;
B2d2+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
}
else if(selectedItem.equals("B2") && day3RB.isSelected()) {
B2d3Amount=B2d3Amount+25;
B2d3+=1; B2total = B2d1Amount+B2d2Amount+B2d3Amount;
}
else if(selectedItem.equals("B3") && day1RB.isSelected()) {
B3d1Amount=B3d1Amount+35;
B3d1+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
}
else if(selectedItem.equals("B3") && day2RB.isSelected()) {
B3d2Amount=B3d2Amount+35;
B3d2+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
}
else if(selectedItem.equals("B3") && day3RB.isSelected()) {
B3d3Amount=B3d3Amount+35;
B3d3+=1; B3total = B3d1Amount+B3d2Amount+B3d3Amount;
}
double grandTotal = A1total+A2total+A3total+B1total+B2total+B3total;
resultsTA.setText("Item Day1 Day2 Day3 Sales Amount\n\n"
+"A1 "+A1d1+" "+A1d2+" "+A1d3+" ₱"+A1total+"0"
+"\nA2 "+A2d1+" "+A2d2+" "+A2d3+" ₱"+A2total+"0"
+"\nA3 "+A3d1+" "+A3d2+" "+A3d3+" ₱"+A3total+"0"
+"\nB1 "+B1d1+" "+B1d2+" "+B1d3+" ₱"+B1total+"0"
+"\nB2 "+B2d1+" "+B2d2+" "+B2d3+" ₱"+B2total+"0"
+"\nB3 "+B3d1+" "+B3d2+" "+B3d3+" ₱"+B3total+"0"
+"\n\nTotal ₱"+grandTotal+"0"
);
clear();
}
});
pushButton.setEnabled(false);
pushButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
pushButton.setFont(new Font("Tahoma", Font.BOLD, 16));
pushButton.setToolTipText("Press to buy.");
pushButton.setBounds(10, 207, 264, 34);
frame.getContentPane().add(pushButton);
itemField = new JTextField();
itemField.setFont(new Font("Tahoma", Font.BOLD, 16));
itemField.setEditable(false);
itemField.setBounds(293, 11, 218, 34);
frame.getContentPane().add(itemField);
itemField.setColumns(10);
priceField = new JTextField();
priceField.setFont(new Font("Tahoma", Font.BOLD, 16));
priceField.setEditable(false);
priceField.setColumns(10);
priceField.setBounds(293, 56, 218, 34);
frame.getContentPane().add(priceField);
JPanel panel = new JPanel();
panel.setBackground(SystemColor.activeCaption);
panel.setBounds(293, 101, 218, 140);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel label1 = new JLabel("Select a Day of sale first and make");
label1.setFont(new Font("Tahoma", Font.BOLD, 12));
label1.setBounds(2, 2, 216, 14);
panel.add(label1);
JButton aButton = new JButton("A");
aButton.setEnabled(false);
aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
priceField.setText(null);
pushButton.setEnabled(false);
itemField.setText("A");
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
}
});
aButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
aButton.setFont(new Font("Tahoma", Font.BOLD, 15));
aButton.setBounds(62, 47, 46, 33);
panel.add(aButton);
JButton bButton = new JButton("B");
bButton.setEnabled(false);
bButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
priceField.setText(null);
pushButton.setEnabled(false);
itemField.setText("B");
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
}
});
bButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
bButton.setFont(new Font("Tahoma", Font.BOLD, 15));
bButton.setBounds(118, 47, 46, 33);
panel.add(bButton);
button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
pushButton.setEnabled(true);
String itemVal = itemField.getText();
itemField.setText(itemVal+"1");
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
String newVal = itemField.getText();
if(newVal.equals("A1")) {
priceField.setText("₱10.00");
}
else {
priceField.setText("₱15.00");
}
}
});
button1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button1.setEnabled(false);
button1.setFont(new Font("Tahoma", Font.BOLD, 15));
button1.setBounds(10, 91, 46, 33);
panel.add(button1);
button2 = new JButton("2");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
pushButton.setEnabled(true);
String itemVal = itemField.getText();
itemField.setText(itemVal+"2");
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
String newVal = itemField.getText();
if(newVal.equals("A2")) {
priceField.setText("₱20.00");
}
else {
priceField.setText("₱25.00");
}
}
});
button2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button2.setEnabled(false);
button2.setFont(new Font("Tahoma", Font.BOLD, 15));
button2.setBounds(87, 91, 46, 33);
panel.add(button2);
button3 = new JButton("3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
pushButton.setEnabled(true);
String itemVal = itemField.getText();
itemField.setText(itemVal+"3");
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
String newVal = itemField.getText();
if(newVal.equals("A3")) {
priceField.setText("₱30.00");
}
else {
priceField.setText("₱35.00");
}
}
});
button3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button3.setEnabled(false);
button3.setFont(new Font("Tahoma", Font.BOLD, 15));
button3.setBounds(162, 91, 46, 33);
panel.add(button3);
JLabel label2 = new JLabel("a selection from the items.");
label2.setFont(new Font("Tahoma", Font.BOLD, 12));
label2.setBounds(2, 22, 171, 14);
panel.add(label2);
resultsTA = new JTextArea();
resultsTA.setEditable(false);
resultsTA.setBounds(521, 11, 368, 230);
frame.getContentPane().add(resultsTA);
day1RB = new JRadioButton("Day 1");
day1RB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
aButton.setEnabled(true);
bButton.setEnabled(true);
day2RB.setSelected(false);
day3RB.setSelected(false);
}
});
day1RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
day1RB.setFont(new Font("Tahoma", Font.BOLD, 12));
day1RB.setBounds(531, 248, 64, 23);
frame.getContentPane().add(day1RB);
day2RB = new JRadioButton("Day 2");
day2RB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
aButton.setEnabled(true);
bButton.setEnabled(true);
day1RB.setSelected(false);
day3RB.setSelected(false);
}
});
day2RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
day2RB.setFont(new Font("Tahoma", Font.BOLD, 12));
day2RB.setBounds(665, 248, 64, 23);
frame.getContentPane().add(day2RB);
day3RB = new JRadioButton("Day 3");
day3RB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
aButton.setEnabled(true);
bButton.setEnabled(true);
day1RB.setSelected(false);
day2RB.setSelected(false);
}
});
day3RB.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
day3RB.setFont(new Font("Tahoma", Font.BOLD, 12));
day3RB.setBounds(797, 249, 64, 23);
frame.getContentPane().add(day3RB);
}
//------Clearing everything after transaction
void clear() {
itemField.setText(null);
priceField.setText(null);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
pushButton.setEnabled(false);
}
}
output
As a tip to get you started, you could replace your 3 by 3 set of variabls:
int A1d1=0; int A1d2=0; int A1d3=0;
int A2d1=0; int A2d2=0; int A2d3=0;
int A3d1=0; int A3d2=0; int A3d3=0;
With a multi dimensional array with 3 columns and 3 rows:
int[][] num = new int[3][3];
Or like this:
int[][] num = {{0,0,0},
{0,0,0},
{0,0,0}};
Then to replace the first value in the second column and first row we use:
num[1][0] = newValue;
Be careful because the first value of an array is at position num[0][0] not num[1][1]. So for example, when editing/getting a value the last value in an array it would be at position num[2][2], not at num[3][3]
i need to save texts from text boxes to text file but any code i found on or made it didn't work, any idea? like any at all? which code would work. i had to use java gui where my teacher teach us about java, but nothing about GUI so ii cant even make save to text file.
public class FlightsPanel extends JPanel {
private Flight flightSelected = null;
private JTextField textFieldDepartureAirport;
private JTextField textFieldArrivalAirport;
private JTextField textFieldDepartureDate;
private JTextField textFieldArrivalDate;
private JRadioButton rdbtnBoarding;
private JRadioButton rdbtnChecking;
private JRadioButton rdbtnAvailable;
private JRadioButton rdbtnClosed;
private JTextField textFieldCost;
private DefaultListModel<String> populateFlights() {
DefaultListModel<String> list = new DefaultListModel<String>();
ArrayList<Flight> FlightList = MainMenu.getAirlineMgr().getFlightList();
for (int i = 0; i < FlightList.size(); i++) {
list.addElement(FlightList.get(i).getFlightNumber());
}
return list;
}
private void displayFlight(String number) {
flightSelected = MainMenu.getAirlineMgr().getFlightByNumber(number);
textFieldDepartureAirport.setText(flightSelected.getDepartureAirport());
textFieldArrivalAirport.setText(flightSelected.getArrivalAirport());
textFieldDepartureDate.setText(flightSelected.getDepartureDate().toString());
textFieldArrivalDate.setText(flightSelected.getArrivalDate().toString());
textFieldCost.setText(Double.toString(flightSelected.getCost()));
if (flightSelected.getFlightStatus() == Flight.Status.AVAILABLE) {
rdbtnAvailable.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.BOARDING) {
rdbtnBoarding.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.CHECKING) {
rdbtnChecking.setSelected(true);
} else {
if (flightSelected.getFlightStatus() == Flight.Status.CLOSED) {
rdbtnClosed.setSelected(true);
}
}
}
}
}
private void saveFlight() {
flightSelected.setDepartureAirport(textFieldDepartureAirport.getText());
flightSelected.setArrivalAirport(textFieldArrivalAirport.getText());
flightSelected.setCost(Double.parseDouble(textFieldCost.getText()));
// flightSelected.setDepartureDate(textFieldDepartureDate.getText());
// flightSelected.setArrivalDate(textFieldArrivalDate.getText());
if (rdbtnAvailable.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.AVAILABLE);
} else {
if (rdbtnBoarding.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.BOARDING);
} else {
if (rdbtnChecking.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.CHECKING);
} else {
if (rdbtnClosed.isSelected()) {
flightSelected.setFlightStatus(Flight.Status.CLOSED);
}
}
}
}
}
public FlightsPanel() {
setBackground(new Color(175, 238, 238));
setLayout(null);
JList list = new JList(populateFlights());
list.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
displayFlight(list.getSelectedValue().toString());
}
});
list.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
list.setBounds(141, 161, 89, 81);
add(list);
JLabel lblNewLabel = new JLabel("Available flights:");
lblNewLabel.setBounds(141, 136, 121, 14);
add(lblNewLabel);
textFieldDepartureAirport = new JTextField();
textFieldDepartureAirport.setBounds(206, 36, 112, 20);
add(textFieldDepartureAirport);
textFieldDepartureAirport.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Departure Airport:");
lblNewLabel_1.setBounds(206, 11, 112, 14);
add(lblNewLabel_1);
textFieldArrivalAirport = new JTextField();
textFieldArrivalAirport.setColumns(10);
textFieldArrivalAirport.setBounds(10, 36, 112, 20);
add(textFieldArrivalAirport);
JLabel lblArrivalAirport = new JLabel("Arrival Airport:");
lblArrivalAirport.setBounds(10, 11, 112, 14);
add(lblArrivalAirport);
textFieldDepartureDate = new JTextField();
textFieldDepartureDate.setColumns(10);
textFieldDepartureDate.setBounds(206, 91, 192, 20);
add(textFieldDepartureDate);
JLabel lblDepartureDate = new JLabel("Departure Date:");
lblDepartureDate.setBounds(206, 67, 112, 14);
add(lblDepartureDate);
textFieldArrivalDate = new JTextField();
textFieldArrivalDate.setColumns(10);
textFieldArrivalDate.setBounds(10, 91, 192, 20);
add(textFieldArrivalDate);
JLabel lblArrivalDate = new JLabel("Arrival Date:");
lblArrivalDate.setBounds(10, 66, 112, 14);
add(lblArrivalDate);
rdbtnBoarding = new JRadioButton("BOARDING");
rdbtnBoarding.setBounds(10, 211, 112, 23);
add(rdbtnBoarding);
rdbtnChecking = new JRadioButton("CHECKING");
rdbtnChecking.setBounds(10, 237, 112, 23);
add(rdbtnChecking);
rdbtnAvailable = new JRadioButton("AVAILABLE");
rdbtnAvailable.setBounds(10, 159, 112, 23);
add(rdbtnAvailable);
JLabel lblStatus = new JLabel("Status:");
lblStatus.setBounds(20, 138, 220, 14);
add(lblStatus);
rdbtnClosed = new JRadioButton("CLOSED");
rdbtnClosed.setBounds(10, 185, 112, 23);
add(rdbtnClosed);
ButtonGroup group = new ButtonGroup();
group.add(rdbtnBoarding);
group.add(rdbtnChecking);
group.add(rdbtnAvailable);
group.add(rdbtnClosed);
JButton button = new JButton("Cancel");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MainMenu.hideFlights();
}
});
button.setBounds(10, 269, 89, 23);
add(button);
JButton btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveFlight();
MainMenu.hideFlights();
}
});
btnConfirm.setBounds(351, 269, 89, 23);
add(btnConfirm);
JLabel lblCost = new JLabel("Cost:");
lblCost.setBounds(341, 11, 112, 14);
add(lblCost);
textFieldCost = new JTextField();
textFieldCost.setColumns(10);
textFieldCost.setBounds(328, 36, 112, 20);
add(textFieldCost);
}
}pic of design
You can use basic file writing from Java. The only difference here from basic file writing is that you need to get the Strings to write from the text attributes of your objects. Here is one example from your code:
import java.io.PrintWriter;
try{
PrintWriter fileWriter = new PrintWriter("myFile.txt", "UTF-8");
fileWriter.println(textFieldDepartureAirport.selectAll());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
Since your teacher did not include much about the UI items, you are able to look up the JavaDoc info about the object type that is holding the data. For example, your text boxes are declared as JTextField, so you can find more info about this class here:
https://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html
i have two jframes in my app, Addnmember.java and Frame1.java
Frame1 is the main Jframe,
i want when someone presses the Add button, the Addmember will be shown, i have done this with this code, and every thing is fine:
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
But in the new Jframe, i want to come back again when Done Bt is selected, i have done this whit this code:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
but this doesent work! mean the Jframe Loads but there is no content in it! look:
why this happens? here is my codes:
AddMember.java:
public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddMember frame = new AddMember();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddMember() {
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 331);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u0646\u0627\u0645:");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
label.setBounds(332, 60, 46, 14);
contentPane.add(label);
textField = new JTextField();
textField.setToolTipText("\u0646\u0627\u0645");
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
textField.setText("");
}
});
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText("\u0646\u0627\u0645");
textField.setBounds(236, 57, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
label_1.setBounds(328, 103, 85, 14);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_1.setText("");
}
});
textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
textField_1.setColumns(10);
textField_1.setBounds(236, 100, 86, 20);
contentPane.add(textField_1);
JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
label_2.setBounds(332, 145, 46, 14);
contentPane.add(label_2);
textField_2 = new JTextField();
textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_2.setText("");
}
});
textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
textField_2.setColumns(10);
textField_2.setBounds(236, 142, 86, 20);
contentPane.add(textField_2);
JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
label_3.setBounds(144, 61, 82, 14);
contentPane.add(label_3);
textField_3 = new JTextField();
textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_3.setText("");
}
});
textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
textField_3.setColumns(10);
textField_3.setBounds(48, 58, 86, 20);
contentPane.add(textField_3);
JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
label_4.setBounds(144, 104, 69, 14);
contentPane.add(label_4);
textField_4 = new JTextField();
textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_4.setText("");
}
});
textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
textField_4.setColumns(10);
textField_4.setBounds(48, 101, 86, 20);
contentPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_5.setText("");
}
});
textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
textField_5.setColumns(10);
textField_5.setBounds(48, 140, 86, 20);
contentPane.add(textField_5);
JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
label_5.setBounds(144, 143, 46, 14);
contentPane.add(label_5);
textField_6 = new JTextField();
textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_6.setText("");
}
});
textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.setColumns(10);
textField_6.setBounds(131, 187, 86, 20);
contentPane.add(textField_6);
JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
label_6.setBounds(227, 190, 46, 14);
contentPane.add(label_6);
btnNewButton = new JButton("Done");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(171, 227, 89, 23);
contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
And Frame1.java:
public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
private JTable table;
public Frame1() {
JOptionPane.showMessageDialog(null, "sazande :|");
this.initialize();
connection = DataBConect.dbConnect();
try {
String query = "select * from Users";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JOptionPane.showMessageDialog(null, "intialize :|");
frame.setTitle("App");
frame.setBounds(100, 100, 725, 465);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton AddB = new JButton("Add");
AddB.setBackground(Color.WHITE);
AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
AddB.setBounds(302, 392, 135, 23);
frame.getContentPane().add(AddB);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 26, 658, 359);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
}
}
thanks.
As to why your second Frame1 shows no data, I'm not 100% sure, but perhaps it has something to do with your failure to close your database connection. Regardless, it seems wasteful and even potentially dangerous to want to gather the database data twice when you've already obtained it once, and to create your Frame1 GUI twice when you've already created it once.
You should re-design your GUI. The main JFrame GUI should remain visible, and the Frame1 window should be a modal JDialog. As a modal dialog, it will prevent users from being able to interact with the parent JFrame until the dialog is no longer visible.
Note that to get started requires only a few changes:
in Frame1.java
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// frame.dispose();
AddMember addmem = new AddMember(Frame1.this); // !!
addmem.setVisible(true);
}
});
in AddMember.java
class AddMember extends JDialog {
public AddMember(JFrame frame) {
super(frame, "", ModalityType.APPLICATION_MODAL);
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
// !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dispose();
dispose(); // !!
// !! Frame1 addmem = new Frame1();
// addmem.setVisible(true);
}
});
Also, you'll want to avoid null layouts and setBounds(...) as this will lead to very rigid, hard to debug and improve programs.
I have created this Automated Library. And I need to know how to connect my Database per each JFrame.
This is my main method:
package com.Student.GUI;
public class Student_HomePage extends JFrame {
private JPanel contentPane;
private JTextField searchBox;
private static String results[];
private static Connection conn;
private static String searchText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DBConnect db = new DBConnect();
conn = db.openConnection();
Student_HomePage frame = new Student_HomePage();
frame.setVisible(true);
frame.setBounds(400,150,599,489);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student_HomePage() {
setBackground(Color.WHITE);
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\ITC302-Core Java\\java_workspace\\Pictures\\lala.png"));
setTitle("LookBook");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 489);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnHome.setEnabled(false);
btnHome.setContentAreaFilled(false);
btnHome.setBorder(null);
btnHome.setBounds(144, 24, 95, 25);
contentPane.add(btnHome);
JButton btnAuthors = new JButton("Authors");
btnAuthors.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnAuthors.setContentAreaFilled(false);
btnAuthors.setBorder(null);
btnAuthors.setBounds(241, 24, 95, 25);
contentPane.add(btnAuthors);
JButton btnBooks = new JButton("Books");
btnBooks.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnBooks.setContentAreaFilled(false);
btnBooks.setBorder(null);
btnBooks.setBounds(338, 24, 95, 25);
contentPane.add(btnBooks);
JTextArea textArea = new JTextArea();
textArea.setText("|");
textArea.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea.setEditable(false);
textArea.setBounds(227, 11, 26, 57);
contentPane.add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setText("|");
textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea_1.setEditable(false);
textArea_1.setBounds(325, 11, 26, 57);
contentPane.add(textArea_1);
searchBox = new JTextField();
searchBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
searchText = temp;
}
});
searchBox.setColumns(10);
searchBox.setBounds(37, 210, 148, 25);
contentPane.add(searchBox);
JTextArea textArea_2 = new JTextArea();
textArea_2.setText("Search title of books,\r\nauthors or genre");
textArea_2.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 14));
textArea_2.setBounds(37, 248, 188, 40);
contentPane.add(textArea_2);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
btnSearch.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\searchIcon.png"));
btnSearch.setContentAreaFilled(false);
btnSearch.setBorder(null);
btnSearch.setBackground(Color.WHITE);
btnSearch.setBounds(185, 210, 87, 25);
contentPane.add(btnSearch);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\download.jpg"));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(270, 202, 290, 215);
contentPane.add(label);
JLabel bookIcon = new JLabel("");
bookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\BookIcon.png"));
bookIcon.setBounds(101, 78, 116, 90);
contentPane.add(bookIcon);
JLabel lookBookIcon = new JLabel("");
lookBookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\image.png"));
lookBookIcon.setBounds(213, 74, 290, 135);
contentPane.add(lookBookIcon);
}
public void btnSearchActionPerformed (ActionEvent e, String searchText) {
DBConnect db = new DBConnect();
results = db.selectMatchedData(searchText, conn);
//if (results.length != 0) {
this.dispose();
Student_SearchList window = new Student_SearchList();
window.setVisible(true);
//}
//else {
//Student_Warning window = new Student_Warning();
//window.setVisible(true);
//}
}
}
And this is where I want to connect my Database:
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
This whole code is my Main.java.
how can i add text "shutdown " to textfield1
and exit frame5 when i press button22 in frame4 ????
JButton jButton22 = new JButton();
JButton jButton23 = new JButton();
JButton jButton24 = new JButton();
public Frame4() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("");
border1 = BorderFactory.createLineBorder(new Color(164, 225, 164),2);
border2 = BorderFactory.createLineBorder(new Color(164, 225, 164),2);
border3 = BorderFactory.createLineBorder(new Color(94, 85, 50),2);
this.getContentPane().setBackground(new Color(63, 138, 232));
this.setSize(new Dimension(400, 345));
this.setLocation(150,150);
this.getContentPane().setLayout(null);
jPanel1.setBackground(new Color(211, 229, 250));
jPanel1.setBorder(border3);
jPanel1.setBounds(new Rectangle(4, 47, 389, 193));
jPanel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 30));
jLabel1.setForeground(new Color(255, 137, 27));
jLabel1.setAlignmentY((float) 0.5);
jLabel1.setText(" server side ®™");
jLabel1.setBounds(new Rectangle(-44, 0, 314, 54));
jLabel2.setFont(new java.awt.Font("Dialog", 1, 15));
jLabel2.setForeground(new Color(60, 193, 60));
jLabel2.setText("status is connected");
jLabel2.setBounds(new Rectangle(107, 11, 165, 31));
jLabel4.setBounds(new Rectangle(252, 160, 124, 42));
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton20_actionPerformed(e);
}
});
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton22_actionPerformed(e);
}
});
jButton23.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton23_actionPerformed(e);
}
});
jButton24.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton24_actionPerformed(e);
}
});
void jButton20_actionPerformed(ActionEvent e) {
new Frame3().setVisible(true);
this.setVisible(false);
}
void jButton21_actionPerformed(ActionEvent e) {
System.exit(0);
}
void jButton22_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
void jButton23_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
void jButton24_actionPerformed(ActionEvent e) {
new Frame5().setVisible(true);
this.setVisible(false);
}
}
Your question is little bit confusing.
I think your question is to change the text of a textfield of another frame from the current frame.
If it is so, then you can do it simply by using functions. For example,
If you have two frames frame4 and frame5, the frame4 contains a button (say button4) and frame5 contains a textfield (say jtextfield).
You want to change the text of jtextfield in frame5 when you click the button4 in frame4.
Write a function in frame5 like,
public void change(String text) {
jtextfield.setText(text)
}
Then for changing the text from frame4, just call that function "change" .ie,
frame5 f=new frame5();
f.change("Shutdown");
where "shutdown" is the text you want to add.