Reputation: 517
I'm having a very strange issue using Tables with RMI. The client is an implementation of a time slot booking system, which I've implemented as a Table.
I'm faced with two problems. The first one was causing the table to update after a change had been made. A solution seemed to be
private void cleanUp() {
panel.removeAll();
panel.setVisible(false);
panel.revalidate();
showTable();
}
And that does appear to be working. (Or maybe causing my issue, I'm not sure)
The problem I have now is to do with a JTextField inside the method that calls the actual booking.
private JTextField txtClientname;
txtClientname = new JTextField();
txtClientname.setText("ClientName");
And then in the confirm button listener -
callBookingSlot(buttonAction, txtClientname.getText());
The really strange thing is that this works initially, once. By working I mean putting the correct value extracted from the JTextField into the table. The first time round it will put in the value of what the user typed in to the field. Any subsequent goes and it will only put in the String "ClientName"
Anyone have any ideas? The issue doesn't appear to be RMI related, I've tried it without the RMI and the value taken from the text field still behaves the same way. I know I should probably be looking at fireTableUpdated etc, which I am, but it would be great if this was one of those ones easily fixed.
Edit - more info
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class StackOverFlowGUI {
private static JFrame frame;
private static JTable table;
private static JPanel panel;
private JTextField txtClientname;
private static JFrame bookingPopup = new JFrame();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
StackOverFlowGUI window = new StackOverFlowGUI();
window.frame.setVisible(true);
}
});
}
public StackOverFlowGUI() {
initialize();
}
private void initialize() {
panel = new JPanel();
frame = new JFrame();
frame.setBounds(100, 100, 700, 751);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
panel.setBounds(10, 11, 674, 576);
frame.getContentPane().add(panel);
showTable();
}
private void showTable() {
table = new JTable();
panel.add(table);
panel.setVisible(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(true);
showBookingPopup(2, 2);
}
private void showBookingPopup(int row, int col) {
bookingPopup.setBounds(100, 100, 220, 185);
bookingPopup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bookingPopup.getContentPane().setLayout(null);
txtClientname = new JTextField();
txtClientname.setText("ClientName");
txtClientname.setBounds(10, 11, 184, 20);
bookingPopup.getContentPane().add(txtClientname);
txtClientname.setColumns(10);
bookingPopup.setVisible(true);
JPanel panel = new JPanel();
panel.setBounds(10, 65, 184, 33);
bookingPopup.getContentPane().add(panel);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here - works first time
System.out.println(txtClientname.getText());
//Continues to work if I don't call cleanUp - but then main window will not update
cleanUp();
}
});
btnSubmit.setBounds(10, 113, 89, 23);
bookingPopup.getContentPane().add(btnSubmit);
}
private void cleanUp() {
panel.removeAll();
panel.setVisible(false);
panel.revalidate();
showTable();
}
}
Upvotes: 1
Views: 714
Reputation: 285403
Your posted code has several major problems including:
setBounds(...)
and avoiding use of the Swing layout managersFor example:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DataIntoTable extends JPanel {
private static final String[] COL_NAMES = {"First Name", "LastName"};
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTable table = new JTable(model);
private JTextField firstNameField = new JTextField(10);
private JTextField lastNameField = new JTextField(10);
private JPanel dialogPanel = new JPanel();
public DataIntoTable(final JFrame frame) {
setLayout(new BorderLayout());
add(new JScrollPane(table));
dialogPanel.add(new JLabel("First Name:"));
dialogPanel.add(firstNameField);
dialogPanel.add(new JLabel("Second Name:"));
dialogPanel.add(lastNameField);
dialogPanel.add(new JButton(new AbstractAction("Submit") {
@Override
public void actionPerformed(ActionEvent arg0) {
String[] rowData = {firstNameField.getText(), lastNameField.getText()};
model.addRow(rowData);
firstNameField.setText("");
lastNameField.setText("");
}
}));
JDialog dialog = new JDialog(frame, "Enter Name", false);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Data Into Table");
DataIntoTable mainPanel = new DataIntoTable(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 1