Results 1 to 6 of 6
- 05-15-2011, 10:30 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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 ?
-
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.
- 05-15-2011, 11:38 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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.
-
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); } }
- 05-16-2011, 12:34 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Thank you very much for your answers and I apologize for my bad code formatting
-
Similar Threads
-
combobox
By dina in forum AWT / SwingReplies: 7Last Post: 05-11-2011, 05:31 PM -
combobox
By leader in forum New To JavaReplies: 2Last Post: 02-12-2011, 09:30 PM -
add combobox for jtable row
By anilkumar_vist in forum Advanced JavaReplies: 1Last Post: 11-18-2009, 11:19 AM -
Need Help combobox
By kwink in forum AWT / SwingReplies: 3Last Post: 03-21-2009, 10:05 AM -
combobox
By chandu.v in forum New To JavaReplies: 2Last Post: 07-02-2008, 08:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks