Setting value in a cell in JTable where editor for cell is a JComboBox

Hi,
I have a column in a JTable where it holds a comboBox . As the comboBox has different
values depending on the row I've a cell editor for each row. That's fine. My problem is that one of the
values on the comboBox launches a dialog where the user selects a new value to add to the comboBox.
The comboBox gets updated with this new value and I call setValueAt on the table. Both get updated
but the cell doesn't display the new value added in the JTable. I click it and the comboBox has the
value and I've to select it to get it displayed in the cell.
Here's the code for updating the tableModel -
public void updateCellDetails(String value, int row)
JComboBox combo = (JComboBox) rowEditor.getTableCellEditorComponent(
                    this,
                    value,
                    true,
                    row,
                    4);
if (combo != null)
combo.insertItemAt(value, combo.getItemCount() - 1);
combo.setSelectedItem(value);
rowEditor.setEditorAt(row, new DefaultCellEditor(combo));
MyTableModel tableModel = (MyTableModel) getModel();
tableModel.setValueAt(value, row, 4);
Thanks

Not sure if this'll help, but did you ever fire an editing-stopped event? If such an event is never fired, then editing-cancelled will be fired when you lose focus from the editor and the original value will be restored.

Similar Messages

  • JTable - different editor for each cell?

    I have a JTable where two columns have combo box editors. When the user makes a selection in one combo box, the other combo box's options must change, as its allowable options depend on selection in the other combo box.
    So in short, I need a way of changing the combo box options for ONLY a specific cell, NOT the entire column. I currently only know how to do this for a whole column:
    // first put stuff in myComboBox, and then...
    myJTable.getColumnModel().getColumn().setCellEditor( new DefaultCellEditor( myComboBox ) );
    Is there any way to change the values in the combo box of a specific cell, rather than the entire column?
    I apologize if this is a newbish question... I am fairly new to Swing, but I didn't see anything addressing this in the documentation.
    -Vern

    The solution is like the one I gave you in one of
    your previous postings. In that posting you wanted a
    renderer for a specific cell. In this posting you
    want a editor for a specific cell. The solution is
    the same, only the method name changes.Okay, thanks. Sorry if it seems like I'm a slow learner. ;-)
    -Vern

  • Setting value of JScollbar programmatically

    How can I set JScrollbar's value programmatically.
    I am using JTextField & JScrollbar and adjustment listener registered with jscrollbar. whenever user adjust the scrollbar, I'm taking that value to set the textfield. But before setting the value I have to check whether the scrollbar's value is according to my need.If it is not then I should set particular value to the scrollbar(programmatically). But my problem is that all these checking are inside adjustment listener & whenever I set any different value for that scrollbar I goes into loop as it again calls the adjustment listener(with no effect of the new set value).
    Can anybody tell me the solution for it? Thanks.

    Thanks for all your help!!!
    My problem has been solved.
    It was going into loop because of JOptionPane(strange).
    I was using JOptionPane to show error message if the value is not in the required range. After clicking OK butn of optionpane it was coming back to original frame & after coming out of adjustmentValueChanged() it was again going to the same method [as if I(user) had clicked the scrollbar ].
    When I removed the JOptionPane, it ran correctly(setting the required new value also with no looping).
    But I can't understand why so. What made it to run continuously after coming back to original frame.
    Do anybody knows this? Just tell me, then how can I display the error?(because I can't use JOptionPane for this & also with JDialog it's showing the same error).
    Thank u.

  • Problem in Setting value in cells of jTable

    I am trying to set values in the table by editing the cells, the problem is that once i enter some values in the cells and hit enter or click on next cell it gives an exception of stack overflow, i tried tracing i am getting all the values of the particular row and colums and the value which is entered,
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    Object[] row = (Object[]) data.get(rowIndex);
    row[columnIndex] = aValue;
    super.fireTableCellUpdated(rowIndex, columnIndex);
    but it is not executing the super.fireTableCellUpdated() function.
    i will be thankful if you provide me with the solution for the above problem or provide me any other way.
    Thank's in Advance.

    Use the DefaultTableModel, then you don't have to write custom code for the setValueAt() method.
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

  • Set color of specific cells in JTable.

    I have a JTable where I want to set the color for specific cells. Is this possible to do, and if so, how?

    Well, one way is to create a custom renderer that know the background color of each cell in the table. The Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer]How to Use Table has an example you might be able to build on.
    Or, this posting may give you another approach. The key to both solutions is to somehow specify the color for every cell:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=610474

  • Setting cell editor for individual cell in JTable

    Hi there,
    I want to provide individual cell editor for my JTable. Basically, my JTable shows property names and values. I want to show different cell editors for different properties.
    I followed the advice in this post:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=423318
    but I have a question:
    I looked at the code of DefaultCellEditor. It just has a single editor component (the one provided in the constructor), so all the methods use the same component and certain aspects are customized for the type of component. Again, there can be only one type of component at a time.The problem that I am facing is that I will have different components for different row/column of the same table. So how do I implement some of the methods (for example, getCellEditorValue()), when I have multiple editor components?
    Also, how do I commit changes made by the user?
    I am extremely confused.
    Someone please help!
    Thanks.

    Actually, that's what I am currently doing.
    Here is my cell editor class:
    public class ObjectPropertyEditor extends DefaultCellEditor
           public ObjectPropertyEditor()
              super(new JTextField());
              Vector list = new Vector();
              list.add("Yes");
              list.add("No");
             myCombo = new JComboBox(list);
          public Component getTableCellEditorComponent(JTable table, Object value,
              boolean isSelected, int row, int column)
             String colName = (String)table.getValueAt(row,0);
             if(colName.equalsIgnoreCase("Leaf-Node?")) //if it is the "Leaf" property, return the combo box as the editor
                 return myCombo;
            else  //for all other properties, use JTextField of the super class
                return super.getTableCellEditorComponent(table,value,isSelected,row,column);
        private JComboBox myCombo;
    }The problem I have is that when I select a new item from the combo box, the new selection is not reflected in the tableModel. I don't know how I can achive that. I think I need the functionalities that DefaultCellEditor gives to its delegate when its constructor arguments is a combo box. But how can I get two different sets of functionalities (JTextField and JComboBox) ?
    Please help!
    Thanks.

  • Shading part of a JTable Cell dependent upon the value of the cell

    Hi
    Was hoping some one woudl be able to provide some help with this. I'm trying to create a renderer that will "shade" part of a JTable cell's background depending upon the value in the cell as a percentage (E.g. if the cell contains 0.25 then a quarter of the cell background will be shaded)
    What I've got so far is a renderer which will draw a rectangle whose width is the relevant percentage of the cell's width. (i.e. the width of the column) based on something similar I found in the forum but the part I'm struggling with is getting it to draw this rectangle in any cell other than the first cell. I've tried using .getCellRect(...) to get the x and y position of the cell to draw the rectangle but I still can't make it work.
    The code for my renderer as it stands is:
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JLabel;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    public class PercentageRepresentationRenderer extends JLabel implements TableCellRenderer{
         double percentageValue;
         double rectWidth;
         double rectHeight;
         JTable table;
         int row;
         int column;
         int x;
         int y;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
              if (value instanceof Number)
                   this.table = table;
                   this.row = row;
                   this.column = column;
                   Number numValue = (Number)value;
                   percentageValue = numValue.doubleValue();
                   rectHeight = table.getRowHeight(row);
                   rectWidth = percentageValue * table.getColumnModel().getColumn(column).getWidth();
              return this;
         public void paintComponent(Graphics g) {
            x = table.getCellRect(row, column, false).x;
            y = table.getCellRect(row, column, false).y;
              setOpaque(false);
            Graphics2D g2d = (Graphics2D)g;
            g2d.fillRect(x,y, new Double(rectWidth).intValue(), new Double(rectHeight).intValue());
            super.paintComponent(g);
    }and the following code produces a runnable example:
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    public class PercentageTestTable extends JFrame {
         public PercentageTestTable()
              Object[] columnNames = new Object[]{"A","B"};
              Object[][] tableData = new Object[][]{{0.25,0.5},{0.75,1.0}};
              DefaultTableModel testModel = new DefaultTableModel(tableData,columnNames);
              JTable test = new JTable(testModel);
              test.setDefaultRenderer(Object.class, new PercentageRepresentationRenderer());
              JScrollPane scroll = new JScrollPane();
              scroll.getViewport().add(test);
              add(scroll);
         public static void main(String[] args)
              PercentageTestTable testTable = new PercentageTestTable();
              testTable.pack();
              testTable.setVisible(true);
              testTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }If anyone could help or point me in the right direction, I'd appreciate it.
    Ruanae

    This is an example I published some while ago -
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Fred120 extends JPanel
        static final Object[][] tableData =
            {1, new Double(10.0)},
            {2, new Double(20.0)},
            {3, new Double(50.0)},
            {4, new Double(10.0)},
            {5, new Double(95.0)},
            {6, new Double(60.0)},
        static final Object[] headers =
            "One",
            "Two",
        public Fred120() throws Exception
            super(new BorderLayout());
            final DefaultTableModel model = new DefaultTableModel(tableData, headers);
            final JTable table = new JTable(model);
            table.getColumnModel().getColumn(1).setCellRenderer( new LocalCellRenderer(120.0));
            add(table);
            add(table.getTableHeader(), BorderLayout.NORTH);
        public class LocalCellRenderer extends DefaultTableCellRenderer
            private double v = 0.0;
            private double maxV;
            private final JPanel renderer = new JPanel(new GridLayout(1,0))
                public void paintComponent(Graphics g)
                    super.paintComponent(g);
                    g.setColor(Color.CYAN);
                    int w = (int)(getWidth() * v / maxV + 0.5);
                    int h = getHeight();
                    g.fillRect(0, 0, w, h);
                    g.drawRect(0, 0, w, h);
            private LocalCellRenderer(double maxV)
                this.maxV = maxV;
                renderer.add(this);
                renderer.setOpaque(true);
                renderer.setBackground(Color.YELLOW);
                renderer.setBorder(null);
                setOpaque(false);
                setHorizontalAlignment(JLabel.CENTER);
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
                final JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                if (value instanceof Double)
                    v = ((Double)value).doubleValue();
                return renderer;
        public static void main(String[] args) throws Exception
            final JFrame frame = new JFrame("Fred120");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new Fred120());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    }

  • To set a value in matrix cell which is linked

    In sales order if I enter  form no of TAX TAB as "form c" , each cell of the TAX Code column of the matrix of contents tab should be set the value as "CST". I have tried to set the value, but it is showing "Form item not editable". I have tried to make the cell as editable but still the error message is coming and it is not setting the defined value. How can this be solved?
    Thankx in advance

    Hi Priya Manoj
    Some notes you can find on this [Thread: Set Value in Itemcode in Purchase Order Form|Set Value in Itemcode in Purchase Order Form;.
    There are I has posted some examples in vbcode.
    Hope the notes can help you.
    Regards
    Sierdna S.
    Edited by: Sierdna S on Oct 22, 2008 9:45 AM

  • How do I set a maximum value to a cell in Numbers?

    I have a cell which references a percentage in the previous cell, example: =TRUNC(C2*1000,0)
    So, the cell that it references (C2) is a percentage, anything from 0% up. The issue that I have is that I want to limit the cell's response to the percentage by setting the maximum at 1000. So, if the percentage in the cell is 104%, I don't want the cell to return 1040, I want it to return a maximum of 1000. The MAX function in Numbers is just for searching cells to find the highest numeric value, and there is no IF THEN statements that I can make in the cell, you can do IF, but it just returns a true or false value. I hope I provided enough info for someone to be able to help me out. I have been trying to figure out if there is a mathematical way that I can limit the response to 0-1000 and still have it be accurate to the percentage but I have not come across anything yet.
    Thanks

    Hello
    =IF(C>1,1000,TRUNC(C*1000,0))
    will do the trick.
    Yvan KOENIG (from FRANCE mardi 13 mai 2008 17:47:01)

  • Set value to system matrix (38) cell

    Hello,
    Thanks for your reply.
    I need to set value for some UDF on a system matrix (38). Crurrently ,I use following code:
    public void SetValue(string columnUniqueId, int matrixRowIndex, string value);
    (I'm using the coresuite framwork)
    The coresponding way in SDK would be:
    oMatrix.Columns.Item("columnUniqueId").Cells.Item("matrixRowIndex").Specific.value = "value"
    This works but when I have more than 10 UDF to fill together, the performance is bad. It takes nearly 3-4 seconds to load one item(row).
    Is there any fast way to set the value? Or is there any other suggestion how to do it?

    No, the problem is not by loading of UDFs by a existing document.
    But when you create a new document and you add a new postion to the document by choosing an item. Then our AddOn copies the UDF values from the item master data into the corresponding UDFs in the postion. At this moment I have to write them manually.
    The best would be when the UDFs in the Item Master Data would be autiomatically created also  in the postion of documents. Because in this way they would be automatically loaded by SBO itself.
    I think SBO has a mistake in its design by here.

  • Edit a JTable cell, click save button, value isn't stored in JTable

    I'm editing a JTable cell, just using default editor provided by JTable
    I click a save button elsewhere in my GUI
    The value typed into the cell so far isn't stored in the TableModel yet and so doesn't get saved.
    How can I make sure the value gets stored in the JTable in the above scenario when the JButton is pressed but before saving.
    Preferably I'd like the code with the JButton not to have to know about the JTable at all.
    Cheers,
    D

    I the forums 100s of times.Many thanks. If they had a decent search on this forum I might have found it as I did try.
    Come to think of it - Sun have completely fcukd up this forum.

  • Cell value spanning multiple rows in JTable

    Hi,
    I have a JTable where I want a single column value alone to span multiple rows.
    Something like
    Course No. | Location | Cost
    | loc1 | 1000
    1 ---------------------------------------------
    | loc2 | 2000
    How can I create a JTable like this?
    Thanks for the help.

    I have a link for that,
    http://www2.gol.com/users/tame/
    go in swing examples, JTable #4.
    Hope it helps :)

  • Setting the footer with the value of a cell - Excel 2010-2013

    Hi,
    is it possible to set the footer of a workbook with the value of a cell without using VBA code or macro?
    Thanks

    No; it is not possible to enter a formula that refers to cells in the header or footer. A VBA macro is the only way to set the header or footer to the value of a cell.
    Regards, Hans Vogelaar (http://www.eileenslounge.com)

  • Where the set value come from

    where is the set value come from, can the value come from Servlet
    create or replace
    PACKAGE BODY TRIGGER_test as
    function setStudentNum(sn in varchar2) return varchar2 as
    Begin
      staff_num := sn;
      return ('Success');
    Exception
    When OTHERS then
      return ('Error');
    End setStudentNum;
    function getStudentNum return varchar2 as
    Begin
      if (staff_num is not null) then
       return (staff_num);
      else
       return ('undefine');
      end if;
    End getStudentNum;
    end TRIGGER_test;

    Hjava wrote:
    >i am assuming that the function trigger_test.setStudentNum() gets executed first which will initialize the staff_num variabl
    when and how?
    Some 'other' bit of code somewhere.  How do we know, as we don't have your application/process code.  There will be something somewhere that calls the setStudentName function, so that when the getStudentName function is called it can return a value.  Values don't just magically appear.
    The only time there may be some code that initializes a value (other than in the declaration of the variable itself) is in a package body execution section e.g.
    SQL> create or replace PACKAGE TRIGGER_test as
      2    function setStudentNum(sn in varchar2) return varchar2;
      3    function getStudentNum return varchar2;
      4  end TRIGGER_test;
      5  /
    Package created.
    SQL> create or replace PACKAGE BODY TRIGGER_test as
      2    staff_num varchar2(30);
      3    function setStudentNum(sn in varchar2) return varchar2 as
      4    Begin
      5      staff_num := sn;
      6      return ('Success');
      7    End setStudentNum;
      8
      9    function getStudentNum return varchar2 as
    10    Begin
    11      return (staff_num);
    12    End getStudentNum;
    13  begin
    14    staff_num := 'undefined';
    15  end TRIGGER_test;
    16  /
    Package body created.
    SQL> select trigger_test.getStudentNum() from dual;
    TRIGGER_TEST.GETSTUDENTNUM()
    undefined
    SQL> select trigger_test.setStudentNum('1234') from dual;
    TRIGGER_TEST.SETSTUDENTNUM('1234')
    Success
    SQL> select trigger_test.getStudentNum() from dual;
    TRIGGER_TEST.GETSTUDENTNUM()
    1234
    As you can see, the first time the package is instantiated in memory (the first time we call any function in it), the package body execution section is called which sets the variable to "undefined" in this example, before the function itself is called to return what the value is.

  • Setting a range based on a value of a cell

    what i am trying to do is add a range of cells in a column ending with the current row and beginning with a variable number of previous rows.
    in english
    the beginning row = (the current row - the value of a cell in the previous column in the current row).
    if column to be totaled = B, current row = 6, the value in A6 = 3
    the range would be = B(6 - 3) or B3:B6 in other words =SUM(B3:B6)
    How do i get there?

    lost,
    As I understand this problem, you have a series of numbers in column B and you want to add the last n items (number shown in column A of current row) to the current row. In column C of the current row place the formula:
    =ADDRESS(ROW()-A7+1,2)&":B"&ROW()
    then in column D your result: =IF(ISBLANK(C),"",SUM(INDIRECT(C)))
    pw

Maybe you are looking for