table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    table

    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?
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    can you be a bit more elaborate, what technology are you using and what the problem actually is...
    Originally posted by oll3i
    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?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      I have a cart to which i add flowers bought by a client then the list of flowers is displayed in a table when i remove a row from a table i also need to remove that flower from a cart

      Code:
      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);
      	}
      
      	
       
      	
       
      	
      }
      then in other class i have
      String k=table.getValu eAt(rowindex,1) ;but eclipse says that the method getValueAt (int,int)is undefined for type Table

      Comment

      • hirak1984
        Contributor
        • Jan 2007
        • 316

        #4
        you can do tht by using session as scope of the variable.
        whenever you delete row invalidate the session.
        Originally posted by oll3i
        I have a cart to which i add flowers bought by a client then the list of flowers is displayed in a table when i remove a row from a table i also need to remove that flower from a cart

        Code:
        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);
        	}
        
        	
         
        	
         
        	
        }
        then in other class i have
        String k=table.getValu eAt(rowindex,1) ;but eclipse says that the method getValueAt (int,int)is undefined for type Table

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          but cd you tell my why eclipse says that the method getValueAt (int,int)is undefined for type Table

          in String k=table.getValu eAt(rowindex,1) ;

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            i solved the problem thank you

            Comment

            Working...