Thursday, 15 December 2016

How to Insert Data From JTextfield to Jtable in Java Swing in Eclipse



import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DemoShow {

private JFrame frame;
private JTextField name;
private JTextField password;
private JTextField empid;
private JTextField empsal;
 

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DemoShow window = new DemoShow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public DemoShow() {
initialize();
}

/**
* Initialize the contents of the frame.
*/JTable table = new JTable();
private void initialize() {


frame = new JFrame();
frame.setBounds(430, 200, 500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

Object [] colomns={"Name","Password","emp ID","Sal"};
DefaultTableModel model= new DefaultTableModel();
model.setColumnIdentifiers(colomns);
table.setModel(model);



name = new JTextField();
name.setBounds(10, 11, 133, 20);
frame.getContentPane().add(name);
name.setColumns(10);

password = new JTextField();
password.setBounds(10, 42, 133, 20);
frame.getContentPane().add(password);
password.setColumns(10);

empid = new JTextField();
empid.setBounds(10, 73, 133, 20);
frame.getContentPane().add(empid);
empid.setColumns(10);

empsal = new JTextField();
empsal.setBounds(10, 104, 133, 20);
frame.getContentPane().add(empsal);
empsal.setColumns(10);

JLabel lblName = new JLabel("Name");
lblName.setBounds(151, 11, 95, 20);
frame.getContentPane().add(lblName);

JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(151, 42, 95, 20);
frame.getContentPane().add(lblPassword);

JLabel lblEmpId = new JLabel("Emp id");
lblEmpId.setBounds(151, 73, 95, 20);
frame.getContentPane().add(lblEmpId);

JLabel lblEmpSalary = new JLabel("emp salary");
lblEmpSalary.setBounds(151, 104, 95, 20);
frame.getContentPane().add(lblEmpSalary);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 135, 464, 115);
frame.getContentPane().add(scrollPane);

JButton btnAdd = new JButton("Add");
Object [] row = new Object[4];
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
row[0]=name.getText();
row[1]=password.getText();
row[2]=empid.getText();
row[3]=empsal.getText();

model.addRow(row);
}
});
btnAdd.setBounds(230, 89, 89, 23);
frame.getContentPane().add(btnAdd);


}
}

Related Posts

How to Insert Data From JTextfield to Jtable in Java Swing in Eclipse
4/ 5
Oleh

Subscribe via email

Like the post above? Please subscribe to the latest posts directly via email.

1 comments:

Tulis comments
avatar
26 November 2020 at 20:49

as I do the same but pass the data from a registry window to a window with the table, please and I had many problems

Reply