retrieve mysql data when i change combobox item in java - java

I use the following code to retrieve data from msysql database to a JComboBox. It was working properly. My problem is when I execute this code, if I click jCombobox, only one item is shown and it displays the related data from database.. it is not getting all the ids from database to Combobox, so that i can select a particular id and display related data.
package Swings;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.*;
import javax.swing.*;
class Frame3 extends JFrame implements ActionListener,ItemListener
{
private JFrame frame;
JPanel panel=new JPanel();
JButton btn1,btn2,btn3;
JLabel l1, l2, l3, l4, l5, l6, l7;
private JComboBox SummonIdbox;
private JTextField txtcarLicenceNo;
private JTextField txtamount;
private JTextField txtdateFined;
private JTextField txtlocation;
private JTextField txtofficerInCharge;
String dbURL = "jdbc:mysql://localhost:3306/cpss";
String username ="root";
String password = "root";
Connection c = null;
Statement st = null;
ResultSet rs = null;
Frame3()
{
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("CPSS Application");
l1 = new JLabel("Update Summon Details");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("SummonId");
l3 = new JLabel("Car LicenceNo:");
l4 = new JLabel("Amount");
l5 = new JLabel("DateFined");
l6 = new JLabel("Location");
l7 = new JLabel("Officer Incharge");
SummonIdbox = new JComboBox();
txtcarLicenceNo = new JTextField();
txtamount = new JTextField();
txtdateFined = new JTextField();
txtlocation = new JTextField();
txtofficerInCharge=new JTextField();
btn1 = new JButton("Update");
btn2 = new JButton("Clear");
btn3= new JButton("Home");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
SummonIdbox.addItemListener(this);
l1.setBounds(500, 30, 400, 30);
l2.setBounds(400, 70, 200, 30);
l3.setBounds(400, 110, 200, 30);
l4.setBounds(400, 150, 200, 30);
l5.setBounds(400, 190, 200, 30);
l6.setBounds(400, 230, 200, 30);
l7.setBounds(400, 270, 200, 30);
SummonIdbox.setBounds(500, 70, 200, 30);
txtcarLicenceNo.setBounds(500, 110, 200, 30);
txtamount.setBounds(500, 150, 200, 30);
txtdateFined.setBounds(500, 190, 200, 30);
txtlocation.setBounds(500, 230, 200, 30);
txtofficerInCharge.setBounds(500, 270, 200, 30);
btn1.setBounds(450, 350, 100, 30);
btn2.setBounds(600, 350, 100, 30);
btn3.setBounds(750, 350, 100, 30);
add(l1);
add(l2);
add(SummonIdbox);
add(l3);
add(txtcarLicenceNo);
add(l4);
add(txtamount);
add(l5);
add(txtdateFined);
add(l6);
add(txtlocation);
add(l7);
add(txtofficerInCharge);
add(btn1);
add(btn2);
add(btn3);
add_summonid(SummonIdbox);
}
#Override
public void actionPerformed(ActionEvent ae ) {
if(ae.getSource() == btn2)
{
txtcarLicenceNo.setText("");
txtamount.setText("");
txtdateFined.setText("");
txtlocation.setText("");
txtofficerInCharge.setText("");
}
else if (ae.getSource() == btn3)
{
SwingMenu s=new SwingMenu();
s.setSize(800,800);
s.setVisible(true);
}
}
void add_summonid(JComboBox SummonIdbox)
{
try
{
//Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Driver is loaded");
c = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connection created");
st=c.createStatement();
rs=st.executeQuery("select Summonid from summon");
while(rs.next())
SummonIdbox.addItem(rs.getInt(1));
// c.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
#Override
public void itemStateChanged(ItemEvent ie) {
int item= (Integer) SummonIdbox.getSelectedItem();
System.out.println(item);
String sql="select carLicenceNo,amount,dateFined,location,officerIncharge from summon where summonId='"+item+"'";
try {
PreparedStatement pst = c.prepareStatement(sql);
//pst.setInt(1,item);
rs=pst.executeQuery();
while(rs.next())
{
txtcarLicenceNo.setText(rs.getString(1));
txtamount.setText(String.valueOf((rs.getDouble(2))));
txtdateFined.setText(rs.getString(3));
txtlocation.setText(rs.getString(4));
txtofficerInCharge.setText(rs.getString(5));
/*System.out.println(rs.getString(1));
System.out.println(rs.getDouble(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));*/
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Related

Connection between MySQL workbench Database and java using Intellij

I am a beginner in java coding and was stuck on a problem that I had been receiving while trying to get the text that I put inside of my Jtextfield to save inside of my MySQL database,
This is my current code, it runs fine but inserts a blank row into my database when I try to save my text
This is my main class:
public class Main{
public static void main(String[] args) {
new MyFrame();
}
}
This is my class for my main menu:
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame{
JLabel label;
JMenu Add;
JMenu remove;
JMenu items;
JMenu vendors;
JMenuBar menuBar;
JMenuItem addCustomer;
JMenuItem addVendors;
JMenuItem addProducts;
JMenuItem removeCustomer;
JMenuItem removeProduct;
JMenuItem removeVendor;
MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
label = new JLabel("Inventory Software");
label.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
menuBar = new JMenuBar();
Add = new JMenu("Add");
remove = new JMenu("Remove");
items = new JMenu("Items");
vendors = new JMenu("Vendors");
addCustomer = new JMenuItem("Add Customers");
addVendors = new JMenuItem("Add Vendors");
addProducts = new JMenuItem("Add Products");
removeCustomer = new JMenuItem("Remove Customer");
removeProduct = new JMenuItem("Remove Product");
removeVendor = new JMenuItem("Remove Vendor");
remove.add(removeCustomer);
remove.add(removeProduct);
remove.add(removeVendor);
Add.add(addCustomer);
Add.add(addProducts);
Add.add(addVendors);
menuBar.add(Add);
menuBar.add(remove);
menuBar.add(items);
menuBar.add(vendors);
addCustomer.addActionListener(new WindowAC());
addVendors.addActionListener(new WindowAV());
addProducts.addActionListener(new WindowAP());
removeCustomer.addActionListener(new WindowRC());
removeVendor.addActionListener(new WindowRV());
removeProduct.addActionListener(new WindowRP());
this.setPreferredSize(new Dimension(550, 300));
this.setJMenuBar(menuBar);
this.add(label);
this.pack();
this.setVisible(true);
this.getContentPane().setBackground(Color.WHITE);
this.setLocationRelativeTo(null);
this.setTitle("Intact Communications Inventory Software");
}
}
This is my class for my Add customer tab containing the Jtextfield:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class WindowAC implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if("Add Customers".equals(e.getActionCommand())){
JFrame windowAC = new JFrame();
JLabel label = new JLabel("Name:");
JLabel label1 = new JLabel("Address:");
JLabel label2 = new JLabel("Email:");
JLabel label3 = new JLabel("Tel: ");
JLabel label4 = new JLabel("FAX: ");
JLabel label5 = new JLabel(" ");
JTextField textField = new JTextField();
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JTextField textField3 = new JTextField();
JTextField textField4 = new JTextField();
JButton button = new JButton("Submit");
String s1, s2;
label.setBounds(50, 100, 100, 50);
label1.setBounds(50, 150, 100, 50);
label2.setBounds(50, 200, 100, 50);
label3.setBounds(50, 250, 100, 50);
label4.setBounds(50, 300, 100, 50);
label5.setBounds(50, 350, 100, 50);
textField.setBounds(150, 110, 400, 30);
textField1.setBounds(150, 160, 400, 30);
textField2.setBounds(150, 210, 400, 30);
textField3.setBounds(150, 260, 400, 30);
textField4.setBounds(150, 310, 400, 30);
button.setBounds(300, 450, 100, 70);
windowAC.add(button);
windowAC.add(label);
windowAC.add(textField);
windowAC.add(label1);
windowAC.add(textField1);
windowAC.add(label2);
windowAC.add(textField2);
windowAC.add(label3);
windowAC.add(textField3);
windowAC.add(label4);
windowAC.add(textField4);
windowAC.add(label5);
windowAC.setSize(750,750);
windowAC.getContentPane().setBackground(Color.WHITE);
windowAC.setTitle("Add Customer info");
windowAC.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windowAC.setVisible(true);
windowAC.setLocationRelativeTo(null);
s1 = textField.getText();
s2 = textField1.getText();
Connection conn = null;
Statement stmt = null;
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e1) {
System.out.println(e1);
}
conn = DriverManager.getConnection("jdbc:mysql://localhost/inventoryapp", "root", "P#kist#n1");
System.out.println("Connection is created successfully:");
stmt = conn.createStatement();
String query1 = "INSERT INTO names(first_name,last_name) VALUES ('" + s1 + "','" + s2 + "')";
stmt.executeUpdate(query1);
System.out.println("Record is inserted in the table successfully..................");
} catch (SQLException excep) {
excep.printStackTrace();
} catch (Exception excep) {
excep.printStackTrace();
} finally {
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Please check it in the MySQL Table...........");
}
}
}
My code tells me that the data from the Jtextfield has saved fine inside if my datatbase but my database only inserts a blank row instead of what I typed into my textfield

Problem in Deleting Row from JTable And Mysql

I Need to delete selected row from Jtable which connected to Mysql database
I successfully make another class to add book and it's work fine
i make this class to delete book added in the table , but i cannot find where the problem in my code , and i don't get any error
IDE : netBeans
here is the class code :
package finallibrary;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.proteanit.sql.DbUtils;
import java.sql.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
public class BookDetails extends JFrame implements ActionListener {
private JPanel contentPane;
private JTable table;
private JTextField search;
private JButton b1, b2, b3;
public static void main(String[] args) {
new BookDetails().setVisible(true);
}
public void Book() {
try {
Conn con = new Conn();
String sql = "select * from book";
PreparedStatement st = con.c.prepareStatement(sql);
ResultSet rs = st.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
con.c.close();
} catch (Exception e) {
}
}
public BookDetails() {
setTitle("Book List");
setBounds(350, 200, 890, 475);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(36, 133, 802, 268);
contentPane.add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
int row = table.getSelectedRow();
search.setText(table.getModel().getValueAt(row, 1).toString());
}
});
table.setBackground(new Color(240, 248, 255));
table.setForeground(Color.DARK_GRAY);
table.setFont(new Font("Trebuchet MS", Font.BOLD, 16));
scrollPane.setViewportView(table);
JButton b1 = new JButton("Search");
b1.addActionListener(this);
b1.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b1.setBounds(564, 89, 108, 33);
contentPane.add(b1);
JButton b2 = new JButton("Delete");
b2.addActionListener(this);
b2.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b2.setBounds(712, 89, 108, 33);
contentPane.add(b2);
JLabel l1 = new JLabel("Book Details");
l1.setForeground(SystemColor.desktop);
l1.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
l1.setBounds(302, 11, 400, 47);
contentPane.add(l1);
search = new JTextField();
search.setForeground(new Color(47, 79, 79));
search.setFont(new Font("Book Antiqua", Font.PLAIN, 17));
search.setBounds(189, 89, 357, 33);
contentPane.add(search);
search.setColumns(10);
JButton b3 = new JButton("Back");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home();
home.setVisible(true);
}
});
b3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
Home home = new Home();
home.setVisible(true);
}
});
b3.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b3.setBounds(36, 89, 103, 33);
contentPane.add(b3);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 128, 128), 3, true), "Book-Details",
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 128, 0)));
panel.setBounds(10, 52, 849, 373);
contentPane.add(panel);
panel.setBackground(Color.WHITE);
Book();
}
public void actionPerformed(ActionEvent ae) {
try {
Conn con = new Conn();
if (ae.getSource() == b1) {
String sql = "select * from book where concat(name, book_id) like ?";
PreparedStatement st = con.c.prepareStatement(sql);
st.setString(1, "%" + search.getText() + "%");
ResultSet rs = st.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
}
if (ae.getSource() == b2) {
String sql = "delete from book where name = '" + search.getText() + "'";
PreparedStatement st = con.c.prepareStatement(sql);
JDialog.setDefaultLookAndFeelDecorated(true);
int response = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
} else if (response == JOptionPane.YES_OPTION) {
int rs = st.executeUpdate();
JOptionPane.showMessageDialog(null, "Deleted !!");
} else if (response == JOptionPane.CLOSED_OPTION) {
}
st.close();
}
con.c.close();
} catch (Exception e) {
}
}
}

Java multiple menu item with an event listener?

I have two menu items here ,one is for the student information and the another one is for the teacher information.i have added the menu for student and teacher both.
I am struck in the event listener how to add the action to the menu and menu1,suppose when the user click on menu 1 then student information should be displayed and for the menu2 the teacher information action should be performed.i am confused where to add the action listenr and perform the operation for the student and teacher.
I am new to the the event and action listner in java.kindly suggest the solution
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Searchdb extends JFrame implements ActionListener {
//Initializing Components
JLabel lb,lbd,lb1, lb2, lb3, lb5;
JTextField tf1, tf2,tf3,tf5,tfd;
JButton btn;
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Student");
JMenu menu1 = new JMenu("Teacher");
//Creating Constructor for initializing JFrame components
Searchdb() {
//Providing Title
super("Fetching Roll Information");
setJMenuBar(menuBar);
menuBar.add(menu);
menuBar.add(menu1);
lb5 = new JLabel("Roll Number:");
lb5.setBounds(20, 20, 100, 20);
tf5 = new JTextField(20);
tf5.setBounds(130, 20, 200, 20);
lbd = new JLabel("Date:");
lbd.setBounds(20, 50, 100, 20);
tfd = new JTextField(20);
tfd.setBounds(130, 50, 200, 20);
btn = new JButton("Submit");
btn.setBounds(50, 50, 100, 20);
btn.addActionListener(this);
lb = new JLabel("Fetching Student Information From Database");
lb.setBounds(30, 80, 450, 30);
lb.setForeground(Color.black);
lb.setFont(new Font("Serif", Font.PLAIN, 12));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
lb1 = new JLabel("Name:");
lb1.setBounds(20, 120, 100, 20);
tf1 = new JTextField(50);
tf1.setBounds(130, 120, 200, 20);
lb2 = new JLabel("Fathername:");
lb2.setBounds(20, 150, 100, 20);
tf2 = new JTextField(100);
tf2.setBounds(130, 150, 200, 20);
lb3 = new JLabel("State:");
lb3.setBounds(20, 180, 100, 20);
tf3 = new JTextField(50);
tf3.setBounds(130, 180, 200, 20);
setLayout(null);
//Add components to the JFrame
add(lb5);
add(tf5);
add(lbd);
add(tfd);
add(btn);
add(lb);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
//Set TextField Editable False
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
}
public void actionPerformed(ActionEvent e) {
//Create DataBase Coonection and Fetching Records
try {
String str = tf5.getText();
Datestri = tfd.getText();//Getting the unable to convert String to Date error
System.out.println(str);
System.out.println(stri);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#//host:port/servicename","username","password");
PreparedStatement st = con.prepareStatement("select Name,Fathername,State from student_db where roll_number=? and medium=?");
System.out.println(st);
st.setString(1, str);
st.setDate(2, stri);
//Excuting Query
ResultSet rs = st.executeQuery();
System.out.println(rs);
if (rs.next()) {
String s = rs.getString(1);
String s1 = rs.getString(2);
String s2 = rs.getString(3);
//Sets Records in TextFields.
tf1.setText(s);
tf2.setText(s1);
tf3.setText(s2);
} else {
JOptionPane.showMessageDialog(null, "Student not Found");
}
//Create Exception Handler
} catch (Exception ex) {
System.out.println(ex);
}
}
public void actionPerformed(ActionEvent e) {
//Create DataBase Coonection and Fetching Records
//Teacher information should be retrieved from the db
}
//Running Constructor
public static void main(String args[]) {
new Searchdb();
}
}
Thanks for any help in advance.
Hope, This will help
For JMenu you can use MenuListener
class MenuListenerAdapter implements MenuListener {
#Override
public void menuSelected(MenuEvent e) {
System.out.println("Menu Selected");
}
#Override
public void menuDeselected(MenuEvent e) {
System.out.println("Menu Deselected");
}
#Override
public void menuCanceled(MenuEvent e) {
System.out.println("Menu Canceled");
}
}
Then add MenuListener on menu
menu.addMenuListener(new MenuListenerAdapter());
You can use MouseListener,
Find your solution below, check out console when you click on Student and Teacher Menu
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
public class Searchdb extends JFrame implements ActionListener {
//Initializing Components
JLabel lb, lbd, lb1, lb2, lb3, lb5;
JTextField tf1, tf2, tf3, tf5, tfd;
JButton btn;
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Student");
JMenu menu1 = new JMenu("Teacher");
//Creating Constructor for initializing JFrame components
Searchdb() {
//Providing Title
super("Fetching Roll Information");
setJMenuBar(menuBar);
menuBar.add(menu);
menuBar.add(menu1);
menu.addMouseListener(new MouseListenerForStudentAndTeacher());
menu1.addMouseListener(new MouseListenerForStudentAndTeacher());
lb5 = new JLabel("Roll Number:");
lb5.setBounds(20, 20, 100, 20);
tf5 = new JTextField(20);
tf5.setBounds(130, 20, 200, 20);
lbd = new JLabel("Date:");
lbd.setBounds(20, 50, 100, 20);
tfd = new JTextField(20);
tfd.setBounds(130, 50, 200, 20);
btn = new JButton("Submit");
btn.setBounds(50, 50, 100, 20);
btn.addActionListener(this);
lb = new JLabel("Fetching Student Information From Database");
lb.setBounds(30, 80, 450, 30);
lb.setForeground(Color.black);
lb.setFont(new Font("Serif", Font.PLAIN, 12));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
lb1 = new JLabel("Name:");
lb1.setBounds(20, 120, 100, 20);
tf1 = new JTextField(50);
tf1.setBounds(130, 120, 200, 20);
lb2 = new JLabel("Fathername:");
lb2.setBounds(20, 150, 100, 20);
tf2 = new JTextField(100);
tf2.setBounds(130, 150, 200, 20);
lb3 = new JLabel("State:");
lb3.setBounds(20, 180, 100, 20);
tf3 = new JTextField(50);
tf3.setBounds(130, 180, 200, 20);
setLayout(null);
//Add components to the JFrame
add(lb5);
add(tf5);
add(lbd);
add(tfd);
add(btn);
add(lb);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
//Set TextField Editable False
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
}
public void actionPerformed(ActionEvent e) {
// removed code, you can add your code later on
}
public static void main(String args[]) {
new Searchdb();
}
private class MouseListenerForStudentAndTeacher extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == menu) {
System.out.println("Student Menu Clicked");
}
if (e.getSource() == menu1) {
System.out.println("Teacher Menu Clicked");
}
}
}
}

button not showing in Jframe

btn3 is not showing when I run it in Netbeans.
when i ran it in NotePad it shows third button.
i cant really figure what the problem is.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("nxt");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(300, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
NewJFrame nw = new NewJFrame();
nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}}
if you put setlayout(null) at first then setVisible(true),the btn3 will show in jframe. like this:
setLayout(null);
setVisible(true);
setSize(700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You need to add a JPanel. To this panel you add your components.
And don't forget to add your panel to your frame like this:
JPanel contentPane = new JPanel();
content.add(new JButton("Test"));
this.setDefaultCloseOperation(3);
this.setContentPane(contentPanel);
this.setSize(Width, Height);
this.setResizable(false);
this.setVisible(true);
this code will work... please define the codes of main frame at beginning of the
constructor, such as
setTitle();
setSize();
setLayout();
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Regform2 extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2, btn3;
JPasswordField p1, p2;
Regform2() {
setTitle("Registration Form in Java");
setSize(700, 700);
setLayout(null);
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
l5 = new JLabel("Confirm Password:");
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField();
tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn3 = new JButton("Next");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
l4.setBounds(80, 150, 200, 30);
l5.setBounds(80, 190, 200, 30);
l6.setBounds(80, 230, 200, 30);
l7.setBounds(80, 270, 200, 30);
l8.setBounds(80, 310, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
p1.setBounds(300, 150, 200, 30);
p2.setBounds(300, 190, 200, 30);
tf5.setBounds(300, 230, 200, 30);
tf6.setBounds(300, 270, 200, 30);
tf7.setBounds(300, 310, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
btn3.setBounds(290, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
add(btn3);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
int x = 0;
String s1 = tf1.getText();
String s2 = tf2.getText();
char[] s3 = p1.getPassword();
char[] s4 = p2.getPassword();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = tf5.getText();
String s6 = tf6.getText();
String s7 = tf7.getText();
if (s8.equals(s9)) {
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
} else {
JOptionPane.showMessageDialog(btn1, "Password Does Not Match");
}
} else if (e.getSource() == btn2) {
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
} else {
this.dispose();
//NewJFrame nw = new NewJFrame();
//nw.setVisible(true);
}
}
public static void main(String args[]) {
new Regform2();
}
}

how to exit upper frame by click on ok and how to add back button when two page in single frame

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Prhr extends JFrame implements ActionListener,ItemListener
{
JLabel l1;
JLabel l2;
JTextField ar;
JTextField t;
JButton b1;
JButton b2;
JButton b3,b4;
JComboBox c1,c2;
JLabel l3;
Connection con;
Statement st;
JLabel l4;
JLabel l5;
JLabel l6;
JLabel l7,l8,l9,l10,l11,l12,l13;
Font f,f2,f1;
String s3,s4;
public Prhr()
{
c1=new JComboBox();
c1.addItem("....");
//*****************************************
try{
System.out.println("in");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/amit","root","1234");
System.out.println(con);
st=con.createStatement();
System.out.println(st);
ResultSet rs= st.executeQuery("select * from CHR");
while(rs.next())
{
c1.addItem(rs.getString("prnm"));
}
}catch(Exception e1)
{
e1.printStackTrace();
}
//****************************************
//JPanel p=new JPanel();
//j.add(p);
f2=new Font("Tahoma", 1, 20);
setFont(f2);
setTitle("CREATE PROCESS HIERARCHY");
f1=new Font("Tahoma", 1, 18);
//setFont(f1);
l1=new JLabel("Process Name");
l2=new JLabel("Description");
l3=new JLabel("Provide Process Detail");
l8=new JLabel("Parent Process");
l9=new JLabel("Processs Type");
ar=new JTextField();
t=new JTextField();
c2=new JComboBox();
b1=new JButton("Preview & Save");
b2=new JButton("Clear");
b3=new JButton("Save as Draft");
setLayout(null);
f=new Font("Tahoma", 1, 20);
//c1.addItem("....");
c2.addItem("Process");
//c1.addItem("1c1");
//c1.addItem("2c1");
c2.addItem("Activity");
//c2.addItem("2c2");
add(l3);
l3.setBounds(170, 70, 250, 20);
add(l1);
l3.setFont(f1);
l1.setBounds(100,130,100,20);
add(t);
t.setBounds(210, 130, 200, 20);
add(l2);
l2.setBounds(100, 175, 100, 20);
add(l8);
l8.setBounds(100, 240, 100, 20);
add(l9);
l9.setBounds(100, 285, 100, 20);
add(ar);
ar.setBounds(210, 175, 200, 40);
add(c1);
c1.setBounds(210, 240, 200, 20);
add(c2);
c2.setBounds(210, 285, 200, 20);
add(b1);
b1.setBounds(50, 365, 129, 20);
add(b2);
b2.setBounds(220, 365, 70, 20);
add(b3);
b3.setBounds(340, 365, 120, 20);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
t.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= 65) && (c <= 90)||(c>=97)&&(c<=122) ||
/*(c == KeyEvent.VK_BACK_SPACE) ||*/(c==32)||
(c == KeyEvent.VK_DELETE))) {
// getToolkit().beep();
e.consume();
}
}
});
ar.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!((c >= 65) && (c <= 90)||(c>=97)&&(c<=122) ||
/*(c == KeyEvent.VK_BACK_SPACE) ||*/(c==32)
)) {
// getToolkit().beep();
e.consume();
}
}
});
//b4.addActionListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
s3=(String)c1.getSelectedItem();
System.out.println(s3);
s4=(String)c2.getSelectedItem();
System.out.println(s4);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource()==b1)
{
System.out.println("in b1");
final String s1=t.getText();
final String s2=ar.getText();
int q=s1.length();
int w=s2.length();
if(q<=20||q>0&&w<=200||w>0)
{
System.out.println(s3);
System.out.println(s4);
JFrame j=new JFrame();
j.setVisible(true);
//j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(500,500);
JPanel p=new JPanel();
j.add(p);
p.setLayout(null);
l4=new JLabel("Process Name");
l5=new JLabel(s1);
l6=new JLabel("Description");
l7=new JLabel(s2);
l10=new JLabel("Parent Process");
l11=new JLabel(s3);
l12=new JLabel("Process Type");
l13=new JLabel(s4);
b4=new JButton("OK");
p.add(l4);
l4.setBounds(100,130,100,20);
p.add(l5);
l5.setBounds(210, 130, 200, 20);
p.add(l6);
l6.setBounds(100, 175, 100, 20);
p.add(l7);
l7.setBounds(210, 175, 200, 40);
p.add(l10);
l10.setBounds(100, 240, 100, 20);
p.add(l11);
l11.setBounds(210, 240, 200, 20);
p.add(l13);
l13.setBounds(210, 285, 200, 20);
p.add(l12);
l12.setBounds(100, 285, 100, 20);
p.add(b4);
b4.setBounds(220, 365, 70, 20);
//**************88
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent d) {
if(d.getSource()==b4)
{
//System.exit(1);
dispose();
try
{
dispose();
System.out.println("in");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/amit","root","1234");
System.out.println(con);
st=con.createStatement();
System.out.println(st);
int i=st.executeUpdate("insert into CHR values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')");
if(i>1)
{
con.close();
st.close();
}
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("inserted");
ResultSet rs= st.executeQuery("select * from CHR");
while(rs.next())
{
c1.addItem(rs.getString("prnm"));
}
}catch(Exception e)
{
e.printStackTrace();
}
//j.dispose();
}//b4 if close
}
});
}
else
{
String message = "\"Pls enter Name within 20 Chatarter\"\n"
+ "OR\n"
+ "Pls enter Description within 200 character";;
JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
JOptionPane.ERROR_MESSAGE);
}
}
else if (a.getSource()==b2)
{
System.out.println("in b2");
t.setText("");
ar.setText("");
c1.setSelectedItem("....");
c2.setSelectedItem("Process");
}
else if(a.getSource()==b3)
{
System.out.println("in b3");
}
}
public static void main(String args[])
{
Prhr pr=new Prhr();
pr.setVisible(true);
pr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pr.setSize(500,500);
}
}
I see in your actionPerformed method you are creating a new JFrame. I assume that is the second frame you are talking about. You might want to use a JDialog, but hard to say since I've no idea what your app does.
But to answer your question, to close a JFrame call:
j.setVisible(false);

Categories