paging in java swing with jtable and database - java

I am new to java and I am working on Jtable with database.i fill up my jtable by data from database ,now I need to use paging and I don't know how to implement this in my code,i used DbUtils to fill up the jtable,every think is ok but the problem is that i don't know how to use pagination, thank you in advance for any sharing of information and help
public class FenetrePers extends JFrame {
private JTextField rechNomField;
private JTextField rechPrenField;
private JTextField rechemailField;
private JTextField rechtelephField;
private JButton btnNewButton_2;
AjoutPers ajpers;
Connection cnx =null;
PreparedStatement prepared=null;
ResultSet resultat=null;
private JScrollPane scrollPane;
private JTable table;
private JButton editButton;
private JButton deleteButton;
public FenetrePers() {
cnx=ConnectionSql.ConnexionDB();
this.setExtendedState(this.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel(" Gestion du personnel");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setBounds(10, 23, 181, 21);
getContentPane().add(lblNewLabel);
rechNomField = new JTextField();
rechNomField.setBounds(27, 84, 175, 32);
getContentPane().add(rechNomField);
rechNomField.setColumns(10);
rechPrenField = new JTextField();
rechPrenField.setBounds(298, 84, 163, 32);
getContentPane().add(rechPrenField);
rechPrenField.setColumns(10);
rechemailField = new JTextField();
rechemailField.setBounds(587, 84, 163, 32);
getContentPane().add(rechemailField);
rechemailField.setColumns(10);
rechtelephField = new JTextField();
rechtelephField.setBounds(844, 84, 157, 32);
getContentPane().add(rechtelephField);
rechtelephField.setColumns(10);
JButton FindButton = new JButton("find");
FindButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
UpdateTableSearch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
FindButton.setBounds(1034, 89, 66, 27);
getContentPane().add(FindButton);
JButton btnNewButton_1 = new JButton("reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String nomS=rechNomField.getText().toString();
String prenomS=rechPrenField.getText().toString();
String emailS=rechemailField.getText().toString();
String telephS=rechtelephField.getText().toString();
if(!nomS.equals("")|| !prenomS.equals("")|| !emailS.equals("")||!telephS.equals("")) {
rechNomField.setText("");
rechPrenField.setText("");
rechemailField.setText("");
rechtelephField.setText("");
UpdateTable();
}}
});
btnNewButton_1.setBounds(1110, 89, 66, 27);
getContentPane().add(btnNewButton_1);
btnNewButton_2 = new JButton("Ajouter personnel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ajpers = new AjoutPers(FenetrePers.this);
UpdateTable();
}
});
btnNewButton_2.setBounds(1034, 24, 142, 32);
getContentPane().add(btnNewButton_2);
scrollPane = new JScrollPane();
scrollPane.setBounds(27, 150, 985, 308);
getContentPane().add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
}
});
scrollPane.setViewportView(table);

Related

How to save changes made to an array list in java

I am just a beginner in java coding and I want to know how I can save changes made to an array list through a GUI i have created. This code allows to store data, remove, add, update student information. However it is only temporarily since it is not connected to the hard disk to permanently store this information. I am wondering how I can make this code save the modified information made to it after i close it. I thank you for your time and consideration. The main class code is:
public class StudentDataStorage {
private JFrame frame;
private JTextField textStudentName;
private JTextField textStudentId;
private JTextField textCourseId;
private JTextField textCourseSubject;
void clearFields() {
textStudentName.setText("");
textStudentId.setText("");
textCourseId.setText("");
textCourseSubject.setText("");
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentDataStorage window = new StudentDataStorage();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
private ArrayList<Student> arrayList;
private ListIterator<Student> listIterator;
int size;
public StudentDataStorage() {
initialize();
arrayList = new ArrayList<>();
size = 0;
arrayList.add(new Student("Jon Rugova", 340007862, "ISTE.121.500", "CompProblemSolvingInfoDomainII"));
arrayList.add(new Student("Sindi Rryta", 125172286, "ACCT.210.501", "Management Accounting"));
arrayList.add(new Student("Arianit Sylafeta", 545919686, "MTSC.231..501", "Contemporary Science: Biology"));
arrayList.add(new Student("Enxo Muçaj", 223375558, "POLS.110.500", "American Politics"));
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 991, 656);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(6, 0, 979, 65);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Student Data Storage");
lblNewLabel.setBounds(287, 6, 393, 44);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(6, 64, 979, 315);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel lblStudentName = new JLabel("Student Name");
lblStudentName.setFont(new Font("Tahoma", Font.BOLD, 20));
lblStudentName.setBounds(164, 57, 196, 23);
lblStudentName.setHorizontalAlignment(SwingConstants.LEFT);
panel_1.add(lblStudentName);
textStudentName = new JTextField();
textStudentName.setFont(new Font("Tahoma", Font.BOLD, 20));
textStudentName.setBounds(372, 55, 421, 26);
panel_1.add(textStudentName);
textStudentName.setColumns(10);
JLabel lblIdNumber = new JLabel("Student ID");
lblIdNumber.setHorizontalAlignment(SwingConstants.LEFT);
lblIdNumber.setFont(new Font("Tahoma", Font.BOLD, 20));
lblIdNumber.setBounds(164, 101, 196, 23);
panel_1.add(lblIdNumber);
textStudentId = new JTextField();
textStudentId.setFont(new Font("Tahoma", Font.BOLD, 20));
textStudentId.setColumns(10);
textStudentId.setBounds(372, 99, 421, 26);
panel_1.add(textStudentId);
JLabel lblCourseId = new JLabel("Course ID");
lblCourseId.setHorizontalAlignment(SwingConstants.LEFT);
lblCourseId.setFont(new Font("Tahoma", Font.BOLD, 20));
lblCourseId.setBounds(164, 145, 196, 23);
panel_1.add(lblCourseId);
textCourseId = new JTextField();
textCourseId.setFont(new Font("Tahoma", Font.BOLD, 20));
textCourseId.setColumns(10);
textCourseId.setBounds(372, 143, 421, 26);
panel_1.add(textCourseId);
JLabel lblCourseSubject = new JLabel("Course Subject");
lblCourseSubject.setHorizontalAlignment(SwingConstants.LEFT);
lblCourseSubject.setFont(new Font("Tahoma", Font.BOLD, 20));
lblCourseSubject.setBounds(164, 186, 196, 23);
panel_1.add(lblCourseSubject);
textCourseSubject = new JTextField();
textCourseSubject.setFont(new Font("Tahoma", Font.BOLD, 20));
textCourseSubject.setColumns(10);
textCourseSubject.setBounds(372, 184, 421, 26);
panel_1.add(textCourseSubject);
JPanel panel_2 = new JPanel();
panel_2.setBounds(6, 378, 979, 250);
frame.getContentPane().add(panel_2);
panel_2.setLayout(null);
JButton btnFirst = new JButton("First");
btnFirst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
size = arrayList.size();
listIterator = arrayList.listIterator();
Student ss;
ss = listIterator.next();
textStudentName.setText(ss.getName());
textStudentId.setText(""+ss.getStudentId());
textCourseId.setText(ss.getCourseId());
textCourseSubject.setText(ss.getCourseSubject());
}
});
btnFirst.setFont(new Font("Tahoma", Font.BOLD, 18));
btnFirst.setBounds(151, 56, 139, 68);
panel_2.add(btnFirst);
JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textStudentName.getText().contentEquals("")||textStudentId.getText().contentEquals("")||textCourseId.getText().contentEquals("")||textCourseSubject.getText().contentEquals("")) {
JOptionPane.showMessageDialog(null, "Error: Some Fields Are Empty!");
}else {
if(listIterator.hasNext()) {
Student ss;
ss = listIterator.next();
textStudentName.setText(ss.getName());;
textStudentId.setText(""+ss.getStudentId());
textCourseId.setText(ss.getCourseId());
textCourseSubject.setText(ss.getCourseSubject());
}else {
JOptionPane.showMessageDialog(null, "You Have Reached the Last Student.");
}
}
}
});
btnNext.setFont(new Font("Tahoma", Font.BOLD, 18));
btnNext.setBounds(317, 56, 139, 68);
panel_2.add(btnNext);
JButton btnAdd = new JButton("Add Data");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textStudentName.getText().contentEquals("")||textStudentId.getText().contentEquals("")||textCourseId.getText().contentEquals("")||textCourseSubject.getText().contentEquals("")) {
JOptionPane.showMessageDialog(null, "Error: Some Fields Are Empty!");
}else {
String name = textStudentName.getText();
int studentid = Integer.parseInt(textStudentId.getText());
String courseid = textCourseId.getText();
String coursesubject = textCourseSubject.getText();
arrayList.add(new Student(name, studentid, courseid, coursesubject));
size = arrayList.size();
clearFields();
JOptionPane.showMessageDialog(null, "Student Data Added Successfully!");
}
}
});
btnAdd.setFont(new Font("Tahoma", Font.BOLD, 18));
btnAdd.setBounds(483, 56, 139, 68);
panel_2.add(btnAdd);
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textStudentName.getText().contentEquals("")||textStudentId.getText().contentEquals("")||textCourseId.getText().contentEquals("")||textCourseSubject.getText().contentEquals("")) {
JOptionPane.showMessageDialog(null, "Error: Some Fields Are Empty!");
}else {
String name = textStudentName.getText();
int studentid = Integer.parseInt(textStudentId.getText());
String courseid = textCourseId.getText();
String coursesubject = textCourseSubject.getText();
for(Student s:arrayList) {
if(studentid == s.getStudentId()) {
arrayList.remove(s);
listIterator = arrayList.listIterator();
clearFields();
JOptionPane.showMessageDialog(null, "Student Data Removed Successfully!");
break;
}
}
}
}
});
btnRemove.setFont(new Font("Tahoma", Font.BOLD, 18));
btnRemove.setBounds(649, 56, 139, 68);
panel_2.add(btnRemove);
JButton btnLast = new JButton("Last");
btnLast.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
size = arrayList.size();
listIterator = arrayList.listIterator(arrayList.size());
if(listIterator.hasPrevious()) {
Student ss;
ss = listIterator.previous();
textStudentName.setText(ss.getName());;
textStudentId.setText(""+ss.getStudentId());
textCourseId.setText(ss.getCourseId());
textCourseSubject.setText(ss.getCourseSubject());
}
}
});
btnLast.setFont(new Font("Tahoma", Font.BOLD, 18));
btnLast.setBounds(151, 144, 139, 68);
panel_2.add(btnLast);
JButton btnPrevious = new JButton("Previous");
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textStudentName.getText().contentEquals("")||textStudentId.getText().contentEquals("")||textCourseId.getText().contentEquals("")||textCourseSubject.getText().contentEquals("")) {
JOptionPane.showMessageDialog(null, "Error: Some Fields Are Empty!");
}else {
if(listIterator.hasPrevious()) {
Student ss;
ss = listIterator.previous();
textStudentName.setText(ss.getName());;
textStudentId.setText(""+ss.getStudentId());
textCourseId.setText(ss.getCourseId());
textCourseSubject.setText(ss.getCourseSubject());
}else {
JOptionPane.showMessageDialog(null, "You Have Reached the First Student.");
}
}
}
});
btnPrevious.setFont(new Font("Tahoma", Font.BOLD, 18));
btnPrevious.setBounds(317, 144, 139, 68);
panel_2.add(btnPrevious);
JButton btnUpdate = new JButton("Update Data");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textStudentName.getText().contentEquals("")||textStudentId.getText().contentEquals("")||textCourseId.getText().contentEquals("")||textCourseSubject.getText().contentEquals("")) {
JOptionPane.showMessageDialog(null, "Error: Some Fields Are Empty!");
}else {
String name = textStudentName.getText();
int studentid = Integer.parseInt(textStudentId.getText());
String courseid = textCourseId.getText();
String coursesubject = textCourseSubject.getText();
for(Student s:arrayList) {
if(studentid == s.getStudentId()) {
s.setName(name);
s.setCourseId(courseid);
s.setCourseSubject(coursesubject);
JOptionPane.showMessageDialog(null, "Changes Have Been Made Successfully!");
break;
}
}
}
}
});
btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 18));
btnUpdate.setBounds(483, 144, 139, 68);
panel_2.add(btnUpdate);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setFont(new Font("Tahoma", Font.BOLD, 18));
btnExit.setBounds(649, 144, 139, 68);
panel_2.add(btnExit);
}
}
-jon

Select two conditions for different panels to then decide two values to select the users game

I need to select two values from two of my frames (Key and Level) to decide what level of test the user will be completing that will then update the label on the start panel to say if the user has selected anything and what they have selected.
I have used window builder in eclipse to help my do this but when i click the button where i have set the value it doesn't change on the label and the label saying if the user can start of not doesn't change
here is my code..(I know its probably really messy im new to all this):
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
public class Test {
private JFrame frame;
int key = 0;
int level=0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel Home = new JPanel();
Home.setBounds(6, 6, 438, 266);
frame.getContentPane().add(Home);
JPanel SelectKey = new JPanel();
SelectKey.setBounds(6, 6, 438, 266);
frame.getContentPane().add(SelectKey);
JPanel SelectLevel = new JPanel();
SelectLevel.setBounds(6, 6, 438, 266);
frame.getContentPane().add(SelectLevel);
SelectLevel.setLayout(null);
JPanel Start = new JPanel();
Start.setBounds(6, 6, 438, 266);
frame.getContentPane().add(Start);
Start.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setBounds(169, 141, 100, 29);
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home.setVisible(true);
SelectKey.setVisible(false);
SelectLevel.setVisible(false);
Start.setVisible(false);
}
});
Home.setLayout(null);
Home.add(btnHome);
JButton btnSelectKey = new JButton("Select Key");
btnSelectKey.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home.setVisible(false);
SelectKey.setVisible(true);
SelectLevel.setVisible(false);
Start.setVisible(false);
}
});
btnSelectKey.setBounds(159, 67, 117, 29);
Home.add(btnSelectKey);
JButton btnSelectLevel = new JButton("Select Level");
btnSelectLevel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SelectKey.setVisible(false);
SelectLevel.setVisible(true);
Home.setVisible(false);
Start.setVisible(false);
}
});
btnSelectLevel.setBounds(159, 37, 117, 29);
Home.add(btnSelectLevel);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SelectKey.setVisible(false);
SelectLevel.setVisible(false);
Home.setVisible(false);
Start.setVisible(true);
}
});
btnStart.setBounds(159, 92, 117, 29);
Home.add(btnStart);
JLabel lblHome = new JLabel("Home");
lblHome.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblHome.setBounds(197, 18, 61, 16);
Home.add(lblHome);
JButton btnNewButton = new JButton("Home");
btnNewButton.setBounds(169, 185, 100, 29);
btnNewButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Home.setVisible(true);
SelectKey.setVisible(false);
SelectLevel.setVisible(false);
Start.setVisible(false);
}
});
SelectKey.setLayout(null);
SelectKey.add(btnNewButton);
JButton btnKeyA = new JButton("Key A");
btnKeyA.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
key = 1;
System.out.print(key);
}
});
btnKeyA.setBounds(157, 44, 117, 29);
SelectKey.add(btnKeyA);
JButton btnKeyB = new JButton("Key B");
btnKeyB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
key = 2;
System.out.print(key);
}
});
btnKeyB.setBounds(157, 90, 117, 29);
SelectKey.add(btnKeyB);
JButton btnKeyC = new JButton("Key C");
btnKeyC.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
key = 3;
System.out.print(key);
}
});
btnKeyC.setBounds(157, 129, 117, 29);
SelectKey.add(btnKeyC);
JLabel lblSelectKey = new JLabel("Select Key");
lblSelectKey.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblSelectKey.setBounds(168, 19, 117, 23);
SelectKey.add(lblSelectKey);
JButton btnBeginnner = new JButton("Beginnner");
btnBeginnner.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
level = 1;
System.out.print(level);
}
});
btnBeginnner.setBounds(158, 67, 117, 29);
SelectLevel.add(btnBeginnner);
JButton btnIntermediate = new JButton("Intermediate");
btnIntermediate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
level=2;
System.out.print(level);
}
});
btnIntermediate.setBounds(158, 113, 117, 29);
SelectLevel.add(btnIntermediate);
JButton btnAdvanced = new JButton("Advanced");
btnAdvanced.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
level = 3;
System.out.print(level);
}
});
btnAdvanced.setBounds(158, 166, 117, 29);
SelectLevel.add(btnAdvanced);
JButton btnHome_1 = new JButton("Home");
btnHome_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home.setVisible(true);
SelectKey.setVisible(false);
SelectLevel.setVisible(false);
Start.setVisible(false);
}
});
btnHome_1.setBounds(158, 207, 117, 29);
SelectLevel.add(btnHome_1);
JLabel lblSelectLevel = new JLabel("Select Level");
lblSelectLevel.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblSelectLevel.setBounds(162, 19, 158, 36);
SelectLevel.add(lblSelectLevel);
JLabel lblStart = new JLabel("Start");
lblStart.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblStart.setBounds(186, 25, 61, 16);
Start.add(lblStart);
JButton btnHome_2 = new JButton("Home");
btnHome_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home.setVisible(true);
SelectKey.setVisible(false);
SelectLevel.setVisible(false);
Start.setVisible(false);
}
});
btnHome_2.setBounds(155, 216, 117, 29);
Start.add(btnHome_2);
JLabel lblSelectkeylabel = new JLabel("SelectKeyLabel");
lblSelectkeylabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblSelectkeylabel.setBounds(175, 64, 97, 41);
lblSelectkeylabel.setText(Integer.toString(key));
Start.add(lblSelectkeylabel);
JLabel lblSelectlevellabel = new JLabel("Selectlevellabel");
lblSelectlevellabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblSelectlevellabel.setBounds(169, 107, 146, 41);
lblSelectlevellabel.setText(Integer.toString(level));
Start.add(lblSelectlevellabel);
JLabel lblChoicedone = new JLabel("Choicedone");
lblChoicedone.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
lblChoicedone.setBounds(175, 147, 61, 16);
if (key==0 || level ==0) {
lblChoicedone.setText("Please go back and choose a key and a level");
}
else {
lblChoicedone.setText("Please Start");
}
Start.add(lblChoicedone);
JButton btnStart_1 = new JButton("Start");
btnStart_1.setBounds(155, 185, 117, 29);
Start.add(btnStart_1);
}
}

How to save Data to mysql [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
I ve been working on a phonebook program on eclipse java
but and idk how to save information from a textfield to mysql and then be able use it in search, such as writing the name and finding all the info of the specific contact
I also want to save it to my sql
thats my code:
public class Phone {
private JFrame frame;
private JTextField firstfield;
private JTextField phonefield;
private JTextField emailfield;
private JTextField lastfield;
private JPanel MainScreen;
private JPanel newcontact;
private JPanel searchscreen;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Phone window = new Phone();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Phone() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel MainScreen = new JPanel();
frame.getContentPane().add(MainScreen, "name_6058038854379");
MainScreen.setLayout(null);
MainScreen.setVisible(true);
final JPanel newcontact = new JPanel();
newcontact.setLayout(null);
frame.getContentPane().add(newcontact, "name_791613670731");
newcontact.setVisible(false);
final JPanel searchscreen = new JPanel();
frame.getContentPane().add(searchscreen, "name_6068807854350");
searchscreen.setVisible(false);
JButton newrecord = new JButton("New Record");
newrecord.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
newcontact.setVisible(true);
MainScreen.setVisible(false);
}
});
newrecord.setBounds(63, 99, 116, 52);
MainScreen.add(newrecord);
JButton Search = new JButton("Search");
Search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchscreen.setVisible(true);
newcontact.setVisible(false);
}
});
Search.setBounds(243, 99, 102, 52);
MainScreen.add(Search);
JLabel myphonebook = new JLabel(" My Phonebook");
myphonebook.setBounds(162, 44, 102, 14);
MainScreen.add(myphonebook);
JLabel first = new JLabel("First Name:");
first.setBounds(118, 83, 66, 14);
newcontact.add(first);
JLabel last = new JLabel("Last Name:");
last.setBounds(118, 108, 66, 14);
newcontact.add(last);
JLabel Phone = new JLabel("Phone:");
Phone.setBounds(118, 143, 66, 14);
newcontact.add(Phone);
JLabel Emailadress = new JLabel("Email Adress:");
Emailadress.setBounds(118, 180, 66, 14);
newcontact.add(Emailadress);
firstfield = new JTextField();
firstfield.setColumns(10);
firstfield.setBounds(220, 80, 86, 20);
newcontact.add(firstfield);
lastfield = new JTextField();
lastfield.setColumns(10);
lastfield.setBounds(220, 108, 86, 20);
newcontact.add(lastfield);
phonefield = new JTextField();
phonefield.setColumns(10);
phonefield.setBounds(220, 140, 86, 20);
newcontact.add(phonefield);
emailfield = new JTextField();
emailfield.setColumns(10);
emailfield.setBounds(220, 177, 86, 20);
newcontact.add(emailfield);
JLabel lblNewContact = new JLabel("New Contact");
lblNewContact.setBounds(166, 30, 86, 14);
newcontact.add(lblNewContact);
JButton Save = new JButton("Save");
Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String firstname=firstfield.getText();
String lastname=lastfield.getText();
String phonenumber=phonefield.getText();
String emailadress=emailfield.getText();
}
});
Save.setBounds(81, 232, 89, 23);
newcontact.add(Save);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newcontact.setVisible(false);
MainScreen.setVisible(true);
}
});
cancel.setBounds(283, 232, 89, 23);
newcontact.add(cancel);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_6081212161880");
}
}
Write this into your savecommand. You should only create a database an table and change the uppercased words.
try
{
// create a mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://LOCATION/DATABASE";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");
// create a sql date object so we can use it in our INSERT statement
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
// the mysql insert statement
String query = " insert into TABLE (first_name, last_name, phone_number, email_adress)"
+ " values (?, ?, ?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, firstname);
preparedStmt.setString (2, lastname);
preparedStmt.setString (3, phonenumber);
preparedStmt.setString (4, emailadress);
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}

Swing gui disappeared

I am working on a simple GUI with options to add,remove,write to disk etc.. While i was coding my program i came to a problem where the GUI start to disappear and opening only blank frame in the design tab/editor(also in run time), i tried to undo multiple times and it was coming back but this time i am to a point where i cannot do undo anymore. I posted all the code i have. What could be the solution to this problem ?
package nikola.lozanovski.bitola;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class VozenRed extends JFrame {
public VozenRed() {
}
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton Delete;
private JButton Transporter;
private JTextField IdBox;
private JTextField InitialBox;
private JTextField FinalBox;
private JTextField Hbox;
private JTextField PriceBox;
private JTextField AgencyBox;
private JTextField SaveBox;
private JTextField Mbox;
private JTextField DeleteBox;
private JTextField DestinationsBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
VozenRed frame = new VozenRed();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #return
*/
public void pishi(){ //tried deleting from here
Hashtable<Broj, Elementi> h1=new Hashtable<Broj, Elementi>();
try
{
Broj B=new Broj(IdBox.getText());
Elementi elem=new Elementi(InitialBox.getText(), FinalBox.getText(), Hbox.getText(), Mbox.getText(), PriceBox.getText(), AgencyBox.getText());
h1.put(B , elem);
}
catch(Exception e)//i tried NullPointerException still the same
{
System.out.println("asd");//not realy what i meant
}
} //deleting to here, still no frame
public void VozenRed1() {
setTitle("Transport me");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 502, 396);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton InitialDestination = new JButton("Initial Destination");
InitialDestination.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Input the initial destination, I.E. My Home", "Initial Destination", 2);
}
});
InitialDestination.setBounds(255, 63, 194, 23);
contentPane.add(InitialDestination);
JButton FinalDestination = new JButton("Final Destination");
FinalDestination.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Input final destination, I.E. Your Home", "Final Destination", 2);
}
});
FinalDestination.setBounds(255, 97, 194, 23);
contentPane.add(FinalDestination);
JButton TimeDeparture = new JButton("Departure Time");
TimeDeparture.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Input time, Hours in first box, Minutes in second box, I.E. 00:00", "Departure Time", 2);
}
});
TimeDeparture.setBounds(255, 128, 194, 23);
contentPane.add(TimeDeparture);
JButton TicketPrice = new JButton("Ticket Price");
TicketPrice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Input price, digits only, I.E. 100","TicketPrice",2);
}
});
TicketPrice.setBounds(255, 159, 194, 23);
contentPane.add(TicketPrice);
Transporter = new JButton("Transport Agency");
Transporter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Input transporter name, I.E. MyTrans","Transporter",2);
}
});
Transporter.setBounds(255, 190, 194, 23);
contentPane.add(Transporter);
JButton SaveRoute = new JButton("Save Route");
SaveRoute.setBounds(255, 320, 194, 23);
contentPane.add(SaveRoute);
Delete = new JButton("Delete");
Delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
Delete.setBounds(255, 286, 194, 23);
contentPane.add(Delete);
JButton Destinations = new JButton("Destinations");
Destinations.setBounds(255, 252, 194, 23);
contentPane.add(Destinations);
JLabel InputLabel = new JLabel("Input Elements");
InputLabel.setBounds(44, 11, 211, 14);
contentPane.add(InputLabel);
JLabel CommandsLabel = new JLabel("Additional Commands");
CommandsLabel.setBounds(44, 222, 201, 14);
contentPane.add(CommandsLabel);
JButton IdentificationNumber = new JButton("Identification Number");
IdentificationNumber.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Enter Identification number , only numbers, I.E. 0123","IdentificationNumber", 2);
}
});
IdentificationNumber.setBounds(255, 29, 194, 23);
contentPane.add(IdentificationNumber);
JButton ClearAll = new JButton("Clear All");
ClearAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IdBox.setText(null);
InitialBox.setText(null);
FinalBox.setText(null);
Hbox.setText(null);
Mbox.setText(null);
PriceBox.setText(null);
AgencyBox.setText(null);
}
});
ClearAll.setFont(new Font("Tahoma", Font.BOLD, 11));
ClearAll.setBounds(255, 218, 194, 23);
contentPane.add(ClearAll);
JLabel lblHelpButtons = new JLabel("Help Buttons");
lblHelpButtons.setBounds(314, 11, 76, 14);
contentPane.add(lblHelpButtons);
JButton HelpDestinations = new JButton("");
HelpDestinations.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Outputs Destinations in a new box","Destinations", 2);
}
});
HelpDestinations.setBounds(459, 252, 17, 23);
contentPane.add(HelpDestinations);
JButton HelpDelete = new JButton("");
HelpDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Deletes selected Identification","Delete", 2);
}
});
HelpDelete.setBounds(459, 286, 17, 23);
contentPane.add(HelpDelete);
JButton HelpSave = new JButton("");
HelpSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Choose Directory to save","Save Document", 2);
}
});
HelpSave.setBounds(459, 320, 17, 23);
contentPane.add(HelpSave);
JButton HelpClearAll = new JButton("");
HelpClearAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Clears all the boxes above","Clear All", 2);
}
});
HelpClearAll.setBounds(459, 218, 17, 23);
contentPane.add(HelpClearAll);
JButton IdentificationHelpButton = new JButton("");
IdentificationHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
IdBox.setText(null);
}
});
IdentificationHelpButton.setBounds(10, 29, 17, 23);
contentPane.add(IdentificationHelpButton);
JButton InitialDestinationHelpButton = new JButton("");
InitialDestinationHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
InitialBox.setText(null);
}
});
InitialDestinationHelpButton.setBounds(10, 60, 17, 23);
contentPane.add(InitialDestinationHelpButton);
JButton FinalDestinationHelpButton = new JButton("");
FinalDestinationHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FinalBox.setText(null);
}
});
FinalDestinationHelpButton.setBounds(10, 97, 17, 23);
contentPane.add(FinalDestinationHelpButton);
JButton DeparturetimeHelpButton = new JButton("");
DeparturetimeHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Hbox.setText(null);
Mbox.setText(null);
}
});
DeparturetimeHelpButton.setBounds(10, 129, 17, 23);
contentPane.add(DeparturetimeHelpButton);
JButton TicketPriceHelpButton = new JButton("");
TicketPriceHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PriceBox.setText(null);
}
});
TicketPriceHelpButton.setBounds(10, 159, 17, 23);
contentPane.add(TicketPriceHelpButton);
JButton TransporterHelpButton = new JButton("");
TransporterHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AgencyBox.setText(null);
}
});
TransporterHelpButton.setBounds(10, 190, 17, 23);
contentPane.add(TransporterHelpButton);
JLabel label = new JLabel(" :");
label.setBounds(113, 132, 39, 14);
contentPane.add(label);
JButton SaveDocumentButton = new JButton("");
SaveDocumentButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SaveBox.setText(null);
}
});
SaveDocumentButton.setBounds(10, 320, 17, 23);
contentPane.add(SaveDocumentButton);
IdBox = new JTextField();
IdBox.setBounds(44, 30, 201, 20);
contentPane.add(IdBox);
IdBox.setColumns(10);
InitialBox = new JTextField();
InitialBox.setColumns(10);
InitialBox.setBounds(44, 64, 201, 20);
contentPane.add(InitialBox);
FinalBox = new JTextField();
FinalBox.setColumns(10);
FinalBox.setBounds(44, 98, 201, 20);
contentPane.add(FinalBox);
Hbox = new JTextField();
Hbox.setColumns(10);
Hbox.setBounds(44, 129, 86, 20);
contentPane.add(Hbox);
PriceBox = new JTextField();
PriceBox.setColumns(10);
PriceBox.setBounds(44, 160, 201, 20);
contentPane.add(PriceBox);
AgencyBox = new JTextField();
AgencyBox.setColumns(10);
AgencyBox.setBounds(44, 191, 201, 20);
contentPane.add(AgencyBox);
SaveBox = new JTextField();
SaveBox.setColumns(10);
SaveBox.setBounds(44, 321, 201, 20);
contentPane.add(SaveBox);
Mbox = new JTextField();
Mbox.setColumns(10);
Mbox.setBounds(145, 129, 100, 20);
contentPane.add(Mbox);
DeleteBox = new JTextField();
DeleteBox.setColumns(10);
DeleteBox.setBounds(44, 287, 201, 20);
contentPane.add(DeleteBox);
DestinationsBox = new JTextField();
DestinationsBox.setColumns(10);
DestinationsBox.setBounds(44, 253, 201, 20);
contentPane.add(DestinationsBox);
JButton DeleteHelpButton = new JButton("");
DeleteHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteBox.setText(null);
}
});
DeleteHelpButton.setBounds(10, 286, 17, 23);
contentPane.add(DeleteHelpButton);
JButton DestinationHelpButton = new JButton("");
DestinationHelpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DestinationsBox.setText(null);
}
});
DestinationHelpButton.setBounds(10, 252, 17, 23);
contentPane.add(DestinationHelpButton);
}
}
Looks like your code never calls all that UI stuff in VozenRed1().
You create the VozenRed object, which calls the empty constructor, then you set it to be visible. Never calling VozenRed1().
I think you've missed initComponents() in the constructor of your jframe.

Opening a new window (JFrame) with e.getsource

I am having a problem with opening a new window. If I comment out the if(e.getsource() == btnAddBook) the action performed opens the window. As soon as I add the if statement in and try to open the window, nothing happens.
/* this is the code that adds the button and the action listner
JButton btnAddBook = new JButton("Add Book");
btnAddBook.setFont(new Font("Tahoma", Font.BOLD, 8));
btnAddBook.setBounds(10, 327, 86, 23);
btnAddBook.addActionListener(this);
getContentPane().add(btnAddBook);
*/
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == btnAddBook){
ChildWindowAdd child = new ChildWindowAdd(this);
this.setVisible(true);
child.setVisible(true);
child.setSize(450,400);
child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
}
class ChildWindowAdd extends JFrame implements ActionListener {
LibraryDatabase parent;
Container c;
JLabel lblTitle, lblaName, lblISBN, lblDate,lbl1,lbl5,lbl10;
JTextField txtTitle, txtaName, txtISBN, txtDate;
JButton btnSave;
JSlider sRating;
JCheckBox chkSci, chkFant, chkRomance, chkAction, chkThriller, chkHorror;
public ChildWindowAdd(LibraryDatabase parent){
this.parent = parent;
getContentPane().setLayout(null);
txtTitle = new JTextField();
txtTitle.setBounds(46, 29, 146, 20);
getContentPane().add(txtTitle);
txtTitle.setColumns(10);
JLabel lblTitle = new JLabel("Book Title");
lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblTitle.setBounds(36, 11, 72, 14);
getContentPane().add(lblTitle);
JLabel lblaName = new JLabel("Author Name");
lblaName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblaName.setBounds(36, 60, 89, 14);
getContentPane().add(lblaName);
txtaName = new JTextField();
txtaName.setBounds(46, 85, 146, 20);
getContentPane().add(txtaName);
txtaName.setColumns(10);
JLabel lblIsbnNumber = new JLabel("ISBN Number");
lblIsbnNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblIsbnNumber.setBounds(36, 116, 86, 14);
getContentPane().add(lblIsbnNumber);
txtISBN = new JTextField();
txtISBN .setBounds(46, 143, 146, 20);
getContentPane().add(txtISBN );
txtISBN .setColumns(10);
JLabel lblDate = new JLabel("Date Added yyyy/mm/dd");
lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDate.setBounds(36, 174, 177, 20);
getContentPane().add(lblDate);
txtDate = new JTextField();
txtDate.setBounds(46, 199, 146, 20);
getContentPane().add(txtDate);
txtDate.setColumns(10);
JPanel genreP = new JPanel();
genreP.setBounds(239, 29, 158, 194);
genreP.setBorder(new TitledBorder(null, "Genre's", TitledBorder.LEADING, TitledBorder.TOP, null, null));
getContentPane().add(genreP);
genreP.setLayout(null);
final JCheckBox chkSci = new JCheckBox("Sci - Fi");
chkSci.setBounds(23, 22, 97, 23);
genreP.add(chkSci);
final JCheckBox chkFant = new JCheckBox("Fantasy");
chkFant.setBounds(23, 48, 97, 23);
genreP.add(chkFant);
final JCheckBox chkRomance = new JCheckBox("Romance");
chkRomance.setBounds(23, 74, 97, 23);
genreP.add(chkRomance);
final JCheckBox chkAction = new JCheckBox("Action");
chkAction.setBounds(23, 100, 97, 23);
genreP.add(chkAction);
final JCheckBox chkThriller = new JCheckBox("Thriller");
chkThriller.setBounds(23, 126, 97, 23);
genreP.add(chkThriller);
final JCheckBox chkHorror = new JCheckBox("Horror");
chkHorror.setBounds(23, 152, 97, 23);
genreP.add(chkHorror);
final JSlider sRating = new JSlider();
sRating.setBounds(118, 280, 200, 26);
getContentPane().add(sRating);
JLabel lbl1 = new JLabel("1");
lbl1.setBounds(118, 257, 7, 14);
getContentPane().add(lbl1);
JLabel lbl10 = new JLabel("10");
lbl10.setBounds(301, 257, 17, 14);
getContentPane().add(lbl10);
JLabel lbl5 = new JLabel("5");
lbl5.setBounds(214, 257, 7, 14);
getContentPane().add(lbl5);
JButton btnSave = new JButton("Save Book");
btnSave.addActionListener(this);
btnSave.setBounds(164, 317, 110, 23);
getContentPane().add(btnSave);
JLabel lblRating = new JLabel("Rate the book");
lblRating.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblRating.setBounds(118, 226, 95, 20);
getContentPane().add(lblRating);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PrintWriter fileWriter = null;
try {
fileWriter = new PrintWriter(new BufferedWriter(new FileWriter("c:\\temp\\database.dat", true)));
} catch (IOException e) {
e.printStackTrace();
}
fileWriter.println( txtTitle.getText()+" , "+txtaName.getText()+" , "+txtISBN.getText()+" , "+txtDate.getText()+" , "+String.valueOf(chkSci.isSelected())+" , "+String.valueOf(chkFant.isSelected())+" , "+String.valueOf(chkRomance.isSelected())+" , "+String.valueOf(chkAction.isSelected())+" , "+String.valueOf(chkThriller.isSelected())+" , "+String.valueOf(chkHorror.isSelected())+" , "+sRating.getValue());
fileWriter.close();
}
});
}
#Override
public void actionPerformed(ActionEvent arg0) {
parent.setVisible(true);
this.dispose();
}
public void actionPerformed1(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Check on the basis of button text
((JButton)source).getText().equals("Add Book")
here is complete code
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton && ((JButton) source).getText().equals("Add Book")) {
...
}
}
This worked for me. I had to match the String as the e.getsource was not working.
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String test = ((JButton)source).getText();
if(test == "Add Book"){
ChildWindowAdd child = new ChildWindowAdd(this);
this.setVisible(true);
child.setVisible(true);
child.setSize(450,400);
child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

Categories