Odświeżanie tabeli z danymi Swing.

0

Mam tabelę z danymi, z której mogę usunąć wiersz, jednak po usunięciu wiersza, tabela nadal posiada wszystkie wiersze. Dopiero restart programu pomaga. Co zrobić aby po akcji usunięcia tabela się odświeżała sama?

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (id == null) {
					JOptionPane.showMessageDialog(null,
							"Musisz zaznaczyć rekord do usunięcia", "Błąd",
							JOptionPane.WARNING_MESSAGE);
				} else {
					rec.deleteRent(id);
				}
			}
		});

Właśnie po tej metodzie deleteRent(id), próbowałem wszelkich repaintów, validatów, ale nic nie działa.;/

public class RentListJFrame extends JFrame {

	public RentListJFrame(Receiver rec, int clientId) {
		super("Lista wypożyczeń");
		this.clientId = clientId;
		getContentPane().setLayout(null);
		setBounds(200, 150, 960, 472);
		RentListJFrame.rec = rec;
		rentListInitialize();
	}

	private void rentListInitialize() {
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		rentList = rec.rentList(clientId);
		String[] kolumny = { "Id", "Data początku", "Data końca", "Klient",
				"Samochód", "Punkt wypożyczenia", "Punkt zwrotu" };
		final DefaultTableModel model = new DefaultTableModel();
		jTable = new JTable(model) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		model.setColumnIdentifiers(kolumny);

		int ile = model.getRowCount();
		for (int i = ile - 1; i >= 0; i--) {
			model.removeRow(i);
		}
		for (Rent r : rentList) {
			Object[] objects = new Object[7];
			objects[0] = r.getId();
			objects[1] = r.getStartDate();
			objects[2] = r.getEndDate();
			objects[3] = findClient(r.getClientId());
			objects[4] = findCar(r.getCarId());
			objects[5] = findCity(r.getCollectionCityId());
			objects[6] = findCity(r.getReturnCityId());
			model.addRow(objects);
			model.fireTableDataChanged();
		}
		scrollPane = new JScrollPane(jTable);
		jTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				int rec = jTable.getSelectedRow();
				id = (jTable.getValueAt(rec, 0).toString());
				startDate = (jTable.getValueAt(rec, 1).toString());
				lblNewLabel.setText("Początek: " + startDate);
				lblNewLabel.setVisible(true);
				endDate = (jTable.getValueAt(rec, 2).toString());
				lblNewLabel_1.setText("Koniec: " + endDate);
				lblNewLabel_1.setVisible(true);
				client_id = (jTable.getValueAt(rec, 3).toString());
				lblNewLabel_2.setText("Klient: " + client_id);
				lblNewLabel_2.setVisible(true);
				car_id = (jTable.getValueAt(rec, 4).toString());
				lblNewLabel_3.setText("Samochód: " + car_id);
				lblNewLabel_3.setVisible(true);
				collectionCity_id = (jTable.getValueAt(rec, 5).toString());
				lblNewLabel_4.setText("Punkt odbioru: " + collectionCity_id);
				lblNewLabel_4.setVisible(true);
				returnCity_id = (jTable.getValueAt(rec, 6).toString());
				lblNewLabel_5.setText("Punkt zwrotu: " + returnCity_id);
				lblNewLabel_5.setVisible(true);
			}
		});
		scrollPane.setSize(600, 297);
		scrollPane.setLocation(43, 35);
		getContentPane().add(scrollPane);

		lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(692, 232, 200, 14);
		lblNewLabel.setVisible(false);
		getContentPane().add(lblNewLabel);

		lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setBounds(692, 262, 200, 14);
		lblNewLabel_1.setVisible(false);
		getContentPane().add(lblNewLabel_1);

		lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setBounds(692, 292, 200, 14);
		lblNewLabel_2.setVisible(false);
		getContentPane().add(lblNewLabel_2);

		lblNewLabel_3 = new JLabel("New label");
		lblNewLabel_3.setBounds(692, 322, 200, 14);
		lblNewLabel_3.setVisible(false);
		getContentPane().add(lblNewLabel_3);

		lblNewLabel_4 = new JLabel("New label");
		lblNewLabel_4.setBounds(692, 352, 200, 14);
		lblNewLabel_4.setVisible(false);
		getContentPane().add(lblNewLabel_4);

		lblNewLabel_5 = new JLabel("New label");
		lblNewLabel_5.setBounds(692, 382, 200, 14);
		lblNewLabel_5.setVisible(false);
		getContentPane().add(lblNewLabel_5);

		ImageIcon img = new ImageIcon("Images\\rent.png");
		lblNewLabel_6 = new JLabel(img);
		lblNewLabel_6.setBounds(692, 36, 128, 128);
		getContentPane().add(lblNewLabel_6);

		JButton btnNewButton = new JButton("Usu\u0144");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (id == null) {
					JOptionPane.showMessageDialog(null,
							"Musisz zaznaczyć rekord do usunięcia", "Błąd",
							JOptionPane.WARNING_MESSAGE);
				} else {
					rec.deleteRent(id);
				}
			}
		});
		btnNewButton.setBounds(43, 348, 89, 23);
		getContentPane().add(btnNewButton);
	}

Pomoże ktoś?

2

Wywołaj na modelu fireTableDataChanged lub fireTableRowsDeleted.

0

Ani jedno ani drugie nie działa:

			public void actionPerformed(ActionEvent arg0) {
				if (id == null) {
					JOptionPane.showMessageDialog(null,
							"Musisz zaznaczyć rekord do usunięcia", "Błąd",
							JOptionPane.WARNING_MESSAGE);
				} else {
					rec.deleteRent(id);
					model.fireTableRowsDeleted(0, ile);
					model.fireTableDataChanged();
				}
			}
		});
1
rec.deleteRent(id);
model.fireTableRowsDeleted(0, ile);
  1. Usuwasz z jakiegoś obiektu rec, jaki on ma związek z modelem?
  2. Usuwasz rekord o numerze id, model informuje o usunięciu rekordów z zakresu [0,ile].
0
bogdans napisał(a):
rec.deleteRent(id);
model.fireTableRowsDeleted(0, ile);
  1. Usuwasz z jakiegoś obiektu rec, jaki on ma związek z modelem?
  2. Usuwasz rekord o numerze id, model informuje o usunięciu rekordów z zakresu [0,ile].
  1. Żadnego z modelem. Ta metoda jest przypisana pod przycisk i usuwa rekord. Po wykonaniu tej metody właśnie chcę odświeżyć tabelę, żeby wyświetliła się bez tego rekordu.
0

Zrób może

setVisible( true )

lub .repaint()

 Lub coś podobnego pomoże ?
0

Ok poradziłem sobie z tym, dzięki za uwagi @bogdans, nakierowało mnie to.

1 użytkowników online, w tym zalogowanych: 0, gości: 1