Issue
I am beginner of Eclipse. I am creating a simple crud system. Successfully load the JTable
when select the row selected row records will display on the relevant text fields for edit purpose. What I tried so far I attached below.
I have selected event here is select the JTable
right click event handler mouse clicked but nothing will displayed.
scrollPane = new JScrollPane();
scrollPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
df = (DefaultTableModel)table_1.getModel();
int selected = table_1.getSelectedRow();
int id = Integer.parseInt(df.getValueAt(selected, 0).toString());
txtname.setText(df.getValueAt(selected, 1).toString());
txtmobile.setText(df.getValueAt(selected, 2).toString());
txtcourse.setText(df.getValueAt(selected, 3).toString());
btnsave.setEnabled(false);
}
Solution
Note that clicking right mouse button on a JTable
does not select the row.
Does this do what you want?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class StudCrud implements Runnable, ListSelectionListener {
private JFrame frame;
private JTable table;
private JTextField nameTextField;
private JTextField phoneTextField;
private JTextField addressTextField;
@Override // javax.swing.event.ListSelectionListener
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
TableModel model = table.getModel();
Object obj = model.getValueAt(selectedRow, 1);
nameTextField.setText(obj == null ? "" : obj.toString());
obj = model.getValueAt(selectedRow, 2);
phoneTextField.setText(obj == null ? "" : obj.toString());
}
}
}
@Override // java.lang.Runnable
public void run() {
showGui();
}
private JPanel createButtons() {
JPanel buttonsPanel = new JPanel(new GridLayout(0, 4, 5, 0));
JButton saveButton = new JButton("Save");
buttonsPanel.add(saveButton);
JButton updateButton = new JButton("Update");
buttonsPanel.add(updateButton);
JButton deleteButton = new JButton("Delete");
buttonsPanel.add(deleteButton);
JButton exitButton = new JButton("Exit");
buttonsPanel.add(exitButton);
return buttonsPanel;
}
private JPanel createForm() {
JPanel form = new JPanel(new GridBagLayout());
form.setBorder(BorderFactory.createLineBorder(Color.GRAY));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
JLabel nameLabel = new JLabel("Student Name");
form.add(nameLabel, gbc);
gbc.gridx = 1;
nameTextField = new JTextField(10);
form.add(nameTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JLabel phoneLabel = new JLabel("Phone");
form.add(phoneLabel, gbc);
gbc.gridx = 1;
phoneTextField = new JTextField(10);
form.add(phoneTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
JLabel addressLabel = new JLabel("Address");
form.add(addressLabel, gbc);
gbc.gridx = 1;
addressTextField = new JTextField(10);
form.add(addressTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
form.add(createButtons(), gbc);
return form;
}
private JScrollPane createTable() {
table = new JTable(createTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
private TableModel createTableModel() {
Object[] columnNames = {"id", "name", "mobile", "course"};
Object[][] data = {{"1", "suren", "883", "php"},
{"2", "asd", "232", "sda"},
{"3", "nisa", "8999", "bads"},
{"4", "yt", "45", "fgh"}};
TableModel model = new DefaultTableModel(data, columnNames);
return model;
}
private void showGui() {
frame = new JFrame("Student Crud");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
((BorderLayout) frame.getContentPane().getLayout()).setHgap(10);
frame.add(createForm(), BorderLayout.LINE_START);
frame.add(createTable(), BorderLayout.LINE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new StudCrud());
}
}
Use the most appropriate listener, which, according to my understanding of your problem, is ListSelectionListener.
Maybe also read How to Use Tables as well as Laying Out Components Within a Container.
This is how it should look.
Answered By - Abra
Answer Checked By - Katrina (JavaFixing Volunteer)