Results 1 to 6 of 6
  1. #1
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default Getting the value from comboBox in Jtable

    I have a table with 3 raws and 2 columns.
    For example

    Name LastName Age
    Martin Martin 5
    Brad Brad
    Peter Peter 15

    The Age column is comboBox.
    So that's what I am trying to do. I want to get all the values from Age and add them. In my example -> 5+15=20. How to do it ? I suppose I need a function which get the value from comboBox ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by skp123 View Post
    I have a table with 3 raws and 2 columns.
    For example

    Name LastName Age
    Martin Martin 5
    Brad Brad
    Peter Peter 15

    The Age column is comboBox.
    So that's what I am trying to do. I want to get all the values from Age and add them. In my example -> 5+15=20. How to do it ? I suppose I need a function which get the value from comboBox ?
    If I understand you correctly, you may be confusing concepts. The combo box should be only the table cell editor. The actual value is stored in the table model and can be obtained as any data stored in the table model can be obtained by calling getValueAt(row, col) on the model.

  3. #3
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Now I understand. Below is the code I have written so far,but it needs some improvements.
    I suppose I need to add some kind of comboBox Listener in order to be able to use getValueAt(row, col) function.And now I am asking for some more help.


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.table.TableColumn;
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    
    class NameTable extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	
    	 public JTable table;
    	 public DefaultTableModel tableModel;
    	 private JComboBox comboBox; 
     
    	 public NameTable()
    	 {
    		 
    		 super(new BorderLayout());
    		 tableModel = new DefaultTableModel();
    		 
    		    tableModel = new DefaultTableModel();
    		    tableModel.addColumn("Name");
    		    tableModel.addColumn("Family Name");
    		    tableModel.addColumn("Age");
    		    
    		    table = new JTable(tableModel);
    	        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    	        
    		    JScrollPane scrollPane = new JScrollPane(table);
    		    
    		    add(scrollPane,BorderLayout.PAGE_START);
    		    
    	        setUpAgeColumn(table.getColumnModel().getColumn(2));
    	        
    			String[] line = {"Martin" ,"Martin"};
    			String[] line1 = {"Brad" ,"Brad"};
               
    			tableModel.addRow(line);
    			tableModel.addRow(line1);
    
    			
    
    	 }
    	 	 
    	    public void setUpAgeColumn(TableColumn ageColumn) {
    
    	    	comboBox = new JComboBox();
    comboBox.addItem("0");
    comboBox.addItem("5");
    comboBox.addItem("10");
    comboBox.addItem("15");
    comboBox.addItem("20");
    comboBox.addItem("30");
    comboBox.addItem("35");
    
    
    ageColumn.setCellEditor(new DefaultCellEditor(comboBox));
    
    DefaultTableCellRenderer renderer =
    new DefaultTableCellRenderer();
    renderer.setToolTipText("Click for combo box");
    ageColumn.setCellRenderer(renderer);
    }
    	    	  
    
    }
    
    public class SumAge
    {
    	
    	public static void main(String[] args) {
    JFrame frame = new JFrame("View-Customer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    NameTable view1 = new NameTable();
    
    
    view1.setOpaque(true); 
    frame.setContentPane(view1);
    
    
    frame.pack();
    frame.setVisible(true);
    
    
    	}
    
    }
    Last edited by skp123; 05-15-2011 at 11:40 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by skp123 View Post
    Now I understand. Below is the code I have written so far,but it needs some improvements.
    I suppose I need to add some kind of comboBox Listener in order to be able to use getValueAt(row, col) function.And now I am asking for some more help.
    No, you need to add a button to your GUI, perhaps called sumAgesButton that when pressed will iterate through the rows summing the ages. You also need to fix the indentation in your code so that it's consistent and readable because many won't try to read it if you don't try to make it easy to read, I know that I won't in the future. For example something that looks more like so:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.table.TableColumn;
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    class NameTable extends JPanel {
    
       private static final long serialVersionUID = 1L;
    
       public JTable table;
       public DefaultTableModel tableModel;
       private JComboBox comboBox;
    
       public NameTable() {
    
          super(new BorderLayout());
          tableModel = new DefaultTableModel();
    
          tableModel = new DefaultTableModel();
          tableModel.addColumn("Name");
          tableModel.addColumn("Family Name");
          tableModel.addColumn("Age");
    
          table = new JTable(tableModel);
          table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    
          JScrollPane scrollPane = new JScrollPane(table);
    
          add(scrollPane, BorderLayout.PAGE_START);
    
          setUpAgeColumn(table.getColumnModel().getColumn(2));
    
          String[] line = {"Martin", "Martin"};
          String[] line1 = {"Brad", "Brad"};
    
          tableModel.addRow(line);
          tableModel.addRow(line1);
    
       }
    
       public void setUpAgeColumn(TableColumn buyColumn) {
    
          comboBox = new JComboBox();
          comboBox.addItem("0");
          comboBox.addItem("5");
          comboBox.addItem("10");
          comboBox.addItem("15");
          comboBox.addItem("20");
          comboBox.addItem("30");
          comboBox.addItem("35");
    
          buyColumn.setCellEditor(new DefaultCellEditor(comboBox));
    
          DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
          renderer.setToolTipText("Click for combo box");
          buyColumn.setCellRenderer(renderer);
       }
    
    }

  5. #5
    skp123 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Thank you very much for your answers and I apologize for my bad code formatting

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As an aside, you could do your addition if the user changes the cells value, but to do that you'd need to add a TableModelListener to your table model.

Similar Threads

  1. combobox
    By dina in forum AWT / Swing
    Replies: 7
    Last Post: 05-11-2011, 05:31 PM
  2. combobox
    By leader in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 09:30 PM
  3. add combobox for jtable row
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2009, 11:19 AM
  4. Need Help combobox
    By kwink in forum AWT / Swing
    Replies: 3
    Last Post: 03-21-2009, 10:05 AM
  5. combobox
    By chandu.v in forum New To Java
    Replies: 2
    Last Post: 07-02-2008, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •