Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,807 members, 7,817,343 topics. Date: Saturday, 04 May 2024 at 10:38 AM

Programmers In The House, Kindly Help Me Table Like This....using Java Awt - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Programmers In The House, Kindly Help Me Table Like This....using Java Awt (1601 Views)

How To Print Diamond With A Frame Using Java / Who Can Solve This Problem Using Java / How To Check Your Account Balance And Make Deposit Using Java (2) (3) (4)

(1) (Reply) (Go Down)

Programmers In The House, Kindly Help Me Table Like This....using Java Awt by Legolast: 1:47pm On Feb 18, 2018
I want to add a table below add button

i need to do add,update and delete in java

Im currently stuck in this....need to add a table down there and..... Whem i click add it should display the data on the table

When i click on data on table...it should show the data on textfield so that can update.

Thats the only thing. Kindly help your boy sir/ma.

Thanks in advance
Re: Programmers In The House, Kindly Help Me Table Like This....using Java Awt by Legolast: 1:50pm On Feb 18, 2018
I want to add a table table like this....using java awt and add button and i need to do add,update and delete in java

Re: Programmers In The House, Kindly Help Me Table Like This....using Java Awt by kudaisi(m): 2:24pm On Feb 18, 2018
Correct me if things have changed, but last I remember awt didn't have a table class. I remember having to to stack text fields to simulate tables back then. You might want to try Java swing in which case a solution can be proffered.
Re: Programmers In The House, Kindly Help Me Table Like This....using Java Awt by Legolast: 2:56pm On Feb 18, 2018
kudaisi:
Correct me if things have changed, but last I remember awt didn't have a table class. I remember having to to stack text fields to simulate tables back then. You might want to try Java swing in which case a solution can be proffered.

im ok with anything
but cant drag n drop fro designer, Need to code it out and All the button panel etc.
Re: Programmers In The House, Kindly Help Me Table Like This....using Java Awt by kudaisi(m): 3:40pm On Feb 21, 2018
I hope its not too late, been busy with work. I was able to whip together something minimal. I hope it helps. I advise you study the code and understand how they fit together. Laying out control in Java is very flexible, the same layout can still be achieved using other types of layout managers. PS: I haven't used Swing in a while as I have been working with JavaFX as far as java is concerned, so pardon any errors (I tested the code though and it works just fine). Also the data is stored in-memory, you can create a custom table model if you want to work with a database. Cheers.

You can access the code here https://ideone.com/ojzpTS incase NL messes up the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class CustomerForm extends JPanel {

JButton btnAdd, btnUpdate, btnDelete, btnReset;
JTextField txtId, txtName, txtContact;
JTable customersTable;
DefaultTableModel tableModel;

public CustomerForm() {
super(true);
btnAdd = new JButton("Add"wink;
btnUpdate = new JButton("Update"wink;
btnDelete = new JButton("Delete"wink;
btnReset = new JButton("Reset"wink;
txtId = new JTextField();
txtName = new JTextField();
txtContact = new JTextField();
customersTable = new JTable();

JPanel fieldsPanel = new JPanel(new GridLayout(3, 2, 5,5));
fieldsPanel.add(new JLabel("Customer ID:"wink);
fieldsPanel.add(txtId);
fieldsPanel.add(new JLabel("Name"wink);
fieldsPanel.add(txtName);
fieldsPanel.add(new JLabel("Contact Number:"wink);
fieldsPanel.add(txtContact);
JPanel buttonsPanel = new JPanel(new FlowLayout());
buttonsPanel.add(btnAdd);
buttonsPanel.add(btnUpdate);
buttonsPanel.add(btnDelete);
buttonsPanel.add(btnReset);

// by default, update and delete should be disabled since no record has been selected yet
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);

tableModel = new DefaultTableModel();
customersTable.setModel(tableModel);
tableModel.addColumn("ID"wink;
tableModel.addColumn("NAME"wink;
tableModel.addColumn("CONTACT"wink;

// enable table scrolls
JScrollPane tablePane = new JScrollPane(customersTable);

this.setLayout(new GridLayout(3,0));
this.add(fieldsPanel);
this.add(buttonsPanel);
this.add(tablePane);

// padding hack for the panel
this.setBorder(new EmptyBorder(10, 10, 10, 10));

// adds text field entries to the table model
// this change in the model in turn repaints the table
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tableModel.addRow(new Object[]{txtId.getText(), txtName.getText(),txtContact.getText()});
clearTexts();
}
});

btnUpdate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tableModel.setValueAt(txtId.getText(), customersTable.getSelectedRow(), 0);
tableModel.setValueAt(txtName.getText(), customersTable.getSelectedRow(), 1);
tableModel.setValueAt(txtContact.getText(), customersTable.getSelectedRow(), 2);
}
});

// deletes the selected table row from the model
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tableModel.removeRow(customersTable.getSelectedRow());
clearTexts();
}
});

// empties the text field to enable addition of new entry
// The update and delete buttons are also disabled
btnReset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
clearTexts();
btnAdd.setEnabled(true);
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);
}
});

// updates the text fields with the selected row data
customersTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableRowSelected(evt);
}
});
}

private void clearTexts(){
txtId.setText(""wink;
txtName.setText(""wink;
txtContact.setText(""wink;
}

private void tableRowSelected(java.awt.event.MouseEvent evt) {

// disable add button when a row as been selected on the table
// we only require row selection for update and delete
btnAdd.setEnabled(false);

// re-enable update and delete button as required
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);

// get the selected row index
int selectedRowIndex = customersTable.getSelectedRow();

// set the selected table row data into the text fields
txtId.setText(tableModel.getValueAt(selectedRowIndex, 0).toString());
txtName.setText(tableModel.getValueAt(selectedRowIndex, 1).toString());
txtContact.setText(tableModel.getValueAt(selectedRowIndex, 2).toString());

}

public static void main(String[] args) {
JFrame frame = new JFrame("Manage Customers"wink;

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
frame.addWindowListener(l);

frame.add("Center", new CustomerForm());
frame.setSize(400,400);
frame.show();
}
}

(1) (Reply)

Top Dev's Share Your Programming Journey / I Need Mobile App / Why Success Is Far From Most Nigerian Programers

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.