473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

JComboBox to table

oll3i
679 512MB
how do i add combobox to a table ?
thank YOU
Jun 14 '07 #1
7 2420
oll3i
679 512MB
can i do it just like this ?
public void addRow(int id,String ilosc,String d,JComboBox s) {
int size = theRows.size();

theRows.add(new Row(id,ilosc,d,s));
theTableModel.fireTableRowsInserted(size, size);

}
Jun 14 '07 #2
r035198x
13,262 8TB
can i do it just like this ?
public void addRow(int id,String ilosc,String d,JComboBox s) {
int size = theRows.size();

theRows.add(new Row(id,ilosc,d,s));
theTableModel.fireTableRowsInserted(size, size);

}
Well try it and see. You'll learn lots of things by trying and watching the compiler's messages in all their glory. The Java tears are sweetest in swing.
Jun 14 '07 #3
oll3i
679 512MB
and that column to which i add combobox i define as what ?
Jun 14 '07 #4
oll3i
679 512MB
okey i found some code hope it will work
Jun 14 '07 #5
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.DefaultCellEditor;
  3. import javax.swing.JComboBox;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.table.AbstractTableModel;
  9. import javax.swing.table.DefaultTableCellRenderer;
  10. import javax.swing.table.TableCellRenderer;
  11. import javax.swing.table.TableColumn;
  12.  
  13. import java.awt.BorderLayout;
  14. import java.awt.Component;
  15. import java.awt.Dimension;
  16. import java.awt.GridLayout;
  17.  
  18. /** 
  19.  * TableRenderDemo is just like TableDemo, except that it
  20.  * explicitly initializes column sizes and it uses a combo box
  21.  * as an editor for the Sport column.
  22.  */
  23. public class TableClient extends JPanel {
  24.     private boolean DEBUG = false;
  25.     private JPanel thePanel;
  26.     public TableClient() {
  27.         //super(new GridLayout(1,0));
  28.         thePanel = new JPanel(new BorderLayout());
  29.         JTable table = new JTable(new MyTableModel());
  30.         table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  31.         table.setFillsViewportHeight(true);
  32.  
  33.         //Create the scroll pane and add the table to it.
  34.         JScrollPane scrollPane = new JScrollPane(table);
  35.  
  36.         //Set up column sizes.
  37.         initColumnSizes(table);
  38.  
  39.         //Fiddle with the Sport column's cell editors/renderers.
  40.         setUpStatusColumn(table, table.getColumnModel().getColumn(3));
  41.  
  42.         //Add the scroll pane to this panel.
  43.         thePanel.add(scrollPane, BorderLayout.CENTER);
  44.     }
  45.  
  46.     /*
  47.      * This method picks good column sizes.
  48.      * If all column heads are wider than the column's cells'
  49.      * contents, then you can just use column.sizeWidthToFit().
  50.      */
  51.     private void initColumnSizes(JTable table) {
  52.         MyTableModel model = (MyTableModel)table.getModel();
  53.         TableColumn column = null;
  54.         Component comp = null;
  55.         int headerWidth = 0;
  56.         int cellWidth = 0;
  57.         Object[] longValues = model.longValues;
  58.         TableCellRenderer headerRenderer =
  59.             table.getTableHeader().getDefaultRenderer();
  60.  
  61.         for (int i = 0; i < 5; i++) {
  62.             column = table.getColumnModel().getColumn(i);
  63.  
  64.             comp = headerRenderer.getTableCellRendererComponent(
  65.                                  null, column.getHeaderValue(),
  66.                                  false, false, 0, 0);
  67.             headerWidth = comp.getPreferredSize().width;
  68.  
  69.             comp = table.getDefaultRenderer(model.getColumnClass(i)).
  70.                              getTableCellRendererComponent(
  71.                                  table, longValues[i],
  72.                                  false, false, 0, i);
  73.             cellWidth = comp.getPreferredSize().width;
  74.  
  75.             if (DEBUG) {
  76.                 System.out.println("Initializing width of column "
  77.                                    + i + ". "
  78.                                    + "headerWidth = " + headerWidth
  79.                                    + "; cellWidth = " + cellWidth);
  80.             }
  81.  
  82.             column.setPreferredWidth(Math.max(headerWidth, cellWidth));
  83.         }
  84.     }
  85.  
  86.     public void setUpStatusColumn(JTable table,
  87.                                  TableColumn statusColumn) {
  88.         //Set up the editor for the sport cells.
  89.         JComboBox comboBox = new JComboBox();
  90.         comboBox.addItem("Nie potwierdzono");
  91.         comboBox.addItem("Potwierdzono");
  92.         comboBox.addItem("Dostarczono");
  93.         comboBox.addItem("Nie dostarczono");
  94.         comboBox.addItem("Zamkniete");
  95.         comboBox.addItem("Anulowane");
  96.         statusColumn.setCellEditor(new DefaultCellEditor(comboBox));
  97.  
  98.         //Set up tool tips for the sport cells.
  99.         DefaultTableCellRenderer renderer =
  100.                 new DefaultTableCellRenderer();
  101.         renderer.setToolTipText("Kliknij aby ukazal sie combo box");
  102.         statusColumn.setCellRenderer(renderer);
  103.     }
  104.  
  105.     class MyTableModel extends AbstractTableModel {
  106.         String[] columnNames = {"ID",
  107.                 "Ilosc","Data Wyslania","Status"};
  108.         private Object[][] data = {
  109.             {"Mary", "Campione",
  110.              "Snowboarding", new Integer(5), new Boolean(false)},
  111.             {"Alison", "Huml",
  112.              "Rowing", new Integer(3), new Boolean(true)},
  113.             {"Kathy", "Walrath",
  114.              "Knitting", new Integer(2), new Boolean(false)},
  115.             {"Sharon", "Zakhour",
  116.              "Speed reading", new Integer(20), new Boolean(true)},
  117.             {"Philip", "Milne",
  118.              "Pool", new Integer(10), new Boolean(false)}
  119.         };
  120.  
  121.         public final Object[] longValues = {"Sharon", "Campione",
  122.                                             "None of the above",
  123.                                             new Integer(20), Boolean.TRUE};
  124.  
  125.         public int getColumnCount() {
  126.             return columnNames.length;
  127.         }
  128.  
  129.         public int getRowCount() {
  130.             return data.length;
  131.         }
  132.  
  133.         public String getColumnName(int col) {
  134.             return columnNames[col];
  135.         }
  136.  
  137.         public Object getValueAt(int row, int col) {
  138.             return data[row][col];
  139.         }
  140.  
  141.         /*
  142.          * JTable uses this method to determine the default renderer/
  143.          * editor for each cell.  If we didn't implement this method,
  144.          * then the last column would contain text ("true"/"false"),
  145.          * rather than a check box.
  146.          */
  147.         public Class getColumnClass(int c) {
  148.             return getValueAt(0, c).getClass();
  149.         }
  150.  
  151.         /*
  152.          * Don't need to implement this method unless your table's
  153.          * editable.
  154.          */
  155.         public boolean isCellEditable(int row, int col) {
  156.             //Note that the data/cell address is constant,
  157.             //no matter where the cell appears onscreen.
  158.             if (col != 3) {
  159.                 return false;
  160.             } else {
  161.                 return true;
  162.             }
  163.         }
  164.  
  165.         /*
  166.          * Don't need to implement this method unless your table's
  167.          * data can change.
  168.          */
  169.         public void setValueAt(Object value, int row, int col) {
  170.             if (DEBUG) {
  171.                 System.out.println("Setting value at " + row + "," + col
  172.                                    + " to " + value
  173.                                    + " (an instance of "
  174.                                    + value.getClass() + ")");
  175.             }
  176.  
  177.             data[row][col] = value;
  178.             fireTableCellUpdated(row, col);
  179.  
  180.             if (DEBUG) {
  181.                 System.out.println("New value of data:");
  182.                 printDebugData();
  183.             }
  184.         }
  185.  public JPanel getPanel() {
  186.             return thePanel;
  187.         }
  188. private void printDebugData() {
  189.             int numRows = getRowCount();
  190.             int numCols = getColumnCount();
  191.  
  192.             for (int i=0; i < numRows; i++) {
  193.                 System.out.print("    row " + i + ":");
  194.                 for (int j=0; j < numCols; j++) {
  195.                     System.out.print("  " + data[i][j]);
  196.                 }
  197.                 System.out.println();
  198.             }
  199.             System.out.println("--------------------------");
  200.         }
  201.     }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. }
  208.  
  209.  
  210.  

but i dont want to have data defined in table model i need to pass the data to table how do ia do that?
how do i add eg Object[] d to Object[][] data ?
Jun 15 '07 #6
oll3i
679 512MB
i made it :) but now i need to listen for the changes in that combobox
Jun 15 '07 #7
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.DefaultCellEditor;
  2. import javax.swing.JComboBox;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.JScrollPane;
  6. import javax.swing.JTable;
  7. import javax.swing.event.TableModelEvent;
  8. import javax.swing.event.TableModelListener;
  9. import javax.swing.table.AbstractTableModel;
  10. import javax.swing.table.DefaultTableCellRenderer;
  11. import javax.swing.table.TableCellRenderer;
  12. import javax.swing.table.TableColumn;
  13.  
  14.  
  15.  
  16. import java.awt.BorderLayout;
  17. import java.awt.Component;
  18. import java.awt.Dimension;
  19. import java.awt.GridLayout;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. /** 
  24.  * TableRenderDemo is just like TableDemo, except that it
  25.  * explicitly initializes column sizes and it uses a combo box
  26.  * as an editor for the Sport column.
  27.  */
  28. public class TableClient extends JPanel implements TableModelListener {
  29.     private boolean DEBUG = false;
  30.     private JPanel thePanel;
  31.     private List<OrderClient> data;
  32.     private MyTableModel model;
  33.     public TableClient() {
  34.         data = new ArrayList<OrderClient>();
  35.         //super(new GridLayout(1,0));
  36.         thePanel = new JPanel(new BorderLayout());
  37.         model = new MyTableModel();
  38.  
  39.  
  40.         JTable table = new JTable(new MyTableModel());
  41.         table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  42.         table.setFillsViewportHeight(true);
  43.         table.getModel().addTableModelListener(this);
  44.         //Create the scroll pane and add the table to it.
  45.         JScrollPane scrollPane = new JScrollPane(table);
  46.  
  47.         //Set up column sizes.
  48.         ////initColumnSizes(table);
  49.  
  50.         //Fiddle with the Sport column's cell editors/renderers.
  51.         setUpStatusColumn(table, table.getColumnModel().getColumn(3));
  52.  
  53.         //Add the scroll pane to this panel.
  54.         thePanel.add(scrollPane, BorderLayout.CENTER);
  55.     }
  56.     public JPanel getPanel() {
  57.         return thePanel;
  58.     }
  59.  
  60.     public void addData(OrderClient oc){
  61.         data.add(oc);
  62.         int size = data.size();     
  63.         model.fireTableRowsInserted(size, size);
  64.     }
  65.     public void tableChanged(TableModelEvent e) {
  66.         int row = e.getFirstRow();
  67.         int column = e.getColumn();
  68.         MyTableModel model = (MyTableModel)e.getSource();
  69.         String columnName = model.getColumnName(column);
  70.         Object data = model.getValueAt(row, column);
  71.  
  72.         // Do something with the data...
  73.     }
  74.  
  75.  
  76.     /*
  77.      * This method picks good column sizes.
  78.      * If all column heads are wider than the column's cells'
  79.      * contents, then you can just use column.sizeWidthToFit().
  80.      */
  81.  
  82.  
  83.     public void setUpStatusColumn(JTable table,
  84.                                  TableColumn statusColumn) {
  85.         //Set up the editor for the sport cells.
  86.         JComboBox comboBox = new JComboBox();
  87.         comboBox.addItem("Nie potwierdzono");
  88.         comboBox.addItem("Potwierdzono");
  89.         comboBox.addItem("Dostarczono");
  90.         comboBox.addItem("Nie dostarczono");
  91.         comboBox.addItem("Zamkniete");
  92.         comboBox.addItem("Anulowane");
  93.         statusColumn.setCellEditor(new DefaultCellEditor(comboBox));
  94.  
  95.         //Set up tool tips for the sport cells.
  96.         DefaultTableCellRenderer renderer =
  97.                 new DefaultTableCellRenderer();
  98.         renderer.setToolTipText("Kliknij aby ukazal sie combo box");
  99.         statusColumn.setCellRenderer(renderer);
  100.     }
  101.  
  102.     class MyTableModel extends AbstractTableModel {
  103.         String[] columnNames = {"ID",
  104.                 "Ilosc","Data Wyslania","Status"};
  105.  
  106.  
  107.  
  108.         public int getColumnCount() {
  109.             return columnNames.length;
  110.         }
  111.  
  112.         public int getRowCount() {
  113.             return data.size();
  114.         }
  115.  
  116.         public String getColumnName(int col) {
  117.             return columnNames[col];
  118.         }
  119.  
  120.         public Object getValueAt(int row, int col) {
  121.             OrderClient orderclient = data.get(row);
  122.             if(col==0)return orderclient.getID();
  123.             else if (col==1) return orderclient.getQuantity();
  124.             else if (col==2) return orderclient.getDate();
  125.             else return orderclient.getStatus();
  126.         }
  127.         public Integer getID(int row){
  128.             OrderClient orderclient = data.get(row);
  129.             return orderclient.getID();}
  130.  
  131.  
  132.           public String getStatus(int row){
  133.             OrderClient orderclient = data.get(row);
  134.             return orderclient.getStatus();}
  135.         /*
  136.          * JTable uses this method to determine the default renderer/
  137.          * editor for each cell.  If we didn't implement this method,
  138.          * then the last column would contain text ("true"/"false"),
  139.          * rather than a check box.
  140.          */
  141.         public Class getColumnClass(int c) {
  142.             return getValueAt(0, c).getClass();
  143.         }
  144.  
  145.         /*
  146.          * Don't need to implement this method unless your table's
  147.          * editable.
  148.          */
  149.         public boolean isCellEditable(int row, int col) {
  150.             //Note that the data/cell address is constant,
  151.             //no matter where the cell appears onscreen.
  152.             if (col != 3) {
  153.                 return false;
  154.             } else {
  155.                 return true;
  156.             }
  157.         }
  158.         public void setValueAt(String value, int row, int col) {
  159.             OrderClient orderclient = data.get(row);
  160.             orderclient.setStatus(value);       
  161.             fireTableCellUpdated(row, col);  
  162.         }
  163.         /*
  164.          * Don't need to implement this method unless your table's
  165.          * data can change.
  166.          */
  167.  
  168.  
  169.  
  170. }
  171. }
  172.  
  173.  
my setValueAt doesnt work what should i change to make it work ?
Jun 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: L L | last post by:
Dear all, Is it possible to set selected item a table row or in other ways, the editable JComboBox 's JTextField- can that be replace by a one row table? If possible, any direction of what...
3
by: Hal Vaughan | last post by:
I have a JComboBox with a list of numbers, from 1 digit to 5 digits. Numbers with more than 3 digits have a comma in them. I've been aligning them with leading spaces. Is there any simple and...
0
by: Yasser | last post by:
Hi All, I developed an application (JFrame) to connect to mysql database. The application will select price from itemsTable and add each each item to a JComboBox. If the user change the prices in...
1
by: Robert | last post by:
When I select a normal cell with a default editor for strings and press a letter key, the letter is automaticaly typed in the cell, but when I select a cell with an editable JComboBox editor and...
2
by: jerico | last post by:
Hi, I am developing an application for which I need to increase the size of the JComboBox.But it is not getting increased.I used the following technique: Container cont; JComboBox valueTypeBox ;...
2
jeffbroodwar
by: jeffbroodwar | last post by:
Hi, I tried to use JCombobox (i'm using netbeans 5.5) how can i use this jComboBox1? how can i load elements? I tried this code but i'm receiving errors.... ...
8
by: dycharles | last post by:
I have a JComboBox loaded with 20,000 data, my problem is when i scroll down or scroll up, it is slower than the JComboBox with the smaller data. What can i do to make my JComboBox run faster or like...
2
by: thesti | last post by:
hi, How to, if user change the selected item of a JComboBox, it will enable another JComboBox. it's like a validation that the user has to select the year first, then the month, than the date. ...
2
by: HaifaCarina | last post by:
i have this unfinished java program and i can't figure out what is the problem.. please help... /** * @(#)Answer3.java * * * @author * @version 1.00 2008/1/17
2
by: javatech007 | last post by:
I am trying to use comboboxes in a ticket payment screen for train tickets. I have two combo boxes. One for picking a route and the other for picking a time. What i want is when you pick a route in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.