i want to get the value of first column of selected row so i can remove the flower form a cart when somebody removes a row which means removing the first occurence of flower object from a map
cd u please help me?
cd u please help me?
package Zad21; import javax.swing.*; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import javax.swing.table.AbstractTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.util.*; import java.util.List; public class Table extends JComponent{ private JPanel thePanel; private JTable theTable; private LocalTableModel theTableModel; private AbstractAction theRemoveRowAction; private List<Row> theRows; String[] columnNames = {"Nazwa", "Ilosc", "Cena"}; private class Row { private String theFirstField; private int theSecondField; private double theThirdField; public Row(String n,int i,double c) { theFirstField = n; theSecondField = i; theThirdField= c; } public String getFirstField() { return theFirstField; } public int getSecondField() { return theSecondField; } public double getThirdField() { return theThirdField; } } public class LocalTableModel extends AbstractTableModel { public String getColumnName(int column) { return columnNames[column]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int rowIndex, int columnIndex) { return false; } public int getRowCount() { return theRows.size(); } public int getColumnCount() { return 3; } public Object getValueAt(int rowIndex, int columnIndex) { Row row = theRows.get(rowIndex); if (columnIndex == 0) { return row.getFirstField(); } else if(columnIndex == 1) { return row.getSecondField(); } else {return row.getThirdField();} } } public Table() { thePanel = new JPanel(new BorderLayout()); JToolBar toolBar = new JToolBar(); toolBar.setFloatable(false); theRemoveRowAction = new AbstractAction("Remove row") { public void actionPerformed(ActionEvent e) { removeRow(); } }; toolBar.add(theRemoveRowAction); thePanel.add(toolBar, BorderLayout.NORTH); theRows = new LinkedList<Row>(); theTableModel = new LocalTableModel(); theTable = new JTable(theTableModel); theTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { validateSelection(); } }); thePanel.add(new JScrollPane(theTable), BorderLayout.CENTER); validateSelection(); //reload(); } JPanel getPanel() { return thePanel; } public void addRow(String nazwkaKwiata,int ilosc,double cena) { int size = theRows.size(); for(int i=0;i<ilosc;i++){ theRows.add(new Row(nazwkaKwiata, 1 ,cena)); theTableModel.fireTableRowsInserted(size, size); } } public void removeRow() { int index = theTable.getSelectedRow(); if (index == -1) return; theRows.remove(index); theTableModel.fireTableRowsDeleted(index, index); index = Math.min(index, theRows.size() - 1); if (index >= 0) { theTable.getSelectionModel().setSelectionInterval(index, index); } } public int GetIndexRowSelected(){return theTable.getSelectedRow();} private void validateSelection() { theRemoveRowAction.setEnabled(theTable.getSelectedRow() != -1); } }
package Zad21; import javax.swing.*; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import javax.swing.table.AbstractTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.util.*; import java.util.List; public class Table extends JComponent{ private JPanel thePanel; private JTable theTable; private LocalTableModel theTableModel; private AbstractAction theRemoveRowAction; private List<Row> theRows; String[] columnNames = {"Nazwa", "Ilosc", "Cena"}; private class Row { private String theFirstField; private int theSecondField; private double theThirdField; public Row(String n,int i,double c) { theFirstField = n; theSecondField = i; theThirdField= c; } public String getFirstField() { return theFirstField; } public int getSecondField() { return theSecondField; } public double getThirdField() { return theThirdField; } } public class LocalTableModel extends AbstractTableModel { public String getColumnName(int column) { return columnNames[column]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int rowIndex, int columnIndex) { return false; } public int getRowCount() { return theRows.size(); } public int getColumnCount() { return 3; } public Object getValueAt(int rowIndex, int columnIndex) { Row row = theRows.get(rowIndex); if (columnIndex == 0) { return row.getFirstField(); } else if(columnIndex == 1) { return row.getSecondField(); } else {return row.getThirdField();} } } public Table() { thePanel = new JPanel(new BorderLayout()); JToolBar toolBar = new JToolBar(); toolBar.setFloatable(false); theRemoveRowAction = new AbstractAction("Remove row") { public void actionPerformed(ActionEvent e) { removeRow(); } }; toolBar.add(theRemoveRowAction); thePanel.add(toolBar, BorderLayout.NORTH); theRows = new LinkedList<Row>(); theTableModel = new LocalTableModel(); theTable = new JTable(theTableModel); theTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { validateSelection(); } }); thePanel.add(new JScrollPane(theTable), BorderLayout.CENTER); validateSelection(); //reload(); } JPanel getPanel() { return thePanel; } public void addRow(String nazwkaKwiata,int ilosc,double cena) { int size = theRows.size(); for(int i=0;i<ilosc;i++){ theRows.add(new Row(nazwkaKwiata, 1 ,cena)); theTableModel.fireTableRowsInserted(size, size); } } public void removeRow() { int index = theTable.getSelectedRow(); if (index == -1) return; theRows.remove(index); theTableModel.fireTableRowsDeleted(index, index); index = Math.min(index, theRows.size() - 1); if (index >= 0) { theTable.getSelectionModel().setSelectionInterval(index, index); } } public int GetIndexRowSelected(){return theTable.getSelectedRow();} private void validateSelection() { theRemoveRowAction.setEnabled(theTable.getSelectedRow() != -1); } }
Comment