Results 1 to 2 of 2

Thread: JTable

  1. #1
    ivvgangadhar is offline Member
    Join Date
    Nov 2008
    Posts
    13
    Rep Power
    0

    Default JTable

    Hi

    I have a JTable with two columns and multiple rows.I want to add ComboBox for some columns(Not for every column).If i am using
    tcolumn.setCellEditor(new DefaultCellEditor(luCombo));
    its updating combobox for every column.Can any one help me please.

    Regards,
    Gangadhar

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    First, JTable is really designed as one piece of a framework, not as a ready to use component. It has several classes that provide pieces of functionality, all of which have default implementations. But the design really is intended for the developer to build each of the component classes according to their needs. If you intend to do serious Swing development, take the time to build up the various components from their interfaces, starting with TableModel.

    As far as what displays each cell, look at the TableCellRenderer and TableCellEditor interfaces. Note that one class can implement both interfaces; you don't need separate classes. Also, note that just ONE instance of each interface is instantiated for the table. The table uses the ONE instance over and over, especially TableCellRenderer. Read the API carefully for things like repaint() and revalidate() that need to be stubbed out.

    If you use one class for both the rendered and the editor, which makes things easier, remember you need one instance for the renderer and one for the editor.

    You control what Component is returned to paint each cell. In the renderer, you should return the same small group of instances (one for each thing you want to display) over and over. Don't instantiate new instances on every call! TableCellRenderer is called hundreds of times a second when the mouse is moving over the table.

    As far as the editor, it is only used to paint the one cell that is currently being edited. It is especially important to not keep instantiating whatever Component you return from the editor, since that will cause the values the user types in to go away! stopEditing() is your opportunity to save the value (or to return false, to force editing to continue). cancelEdiing() should throw the new value away.

    A final note. The default renderer (not the editor) is a subclass of JLabel. The renderer, and the editor as well, if it is implemented correctly, can actually return *themselves* as the Component. I say this only to point out that you *don't* have to do that. Extend Object, instantiate whatever components (JLabel, JComboBox, etc.) you want to return, and keep them in instance fields. All the renderer or editor has to do is set the value.

    I mentioned implementing both interfaces in one class. What I have done is to call the renderer's method from the editor's to set up the component; I then do additional setup needed for editing. I have a very complicated requirement that made me use JPanel as the return Component; I add other components to the JPanel as required. It causes a lot of problems, so I suggest avoiding that solution if you can. (I'm a contractor, and I was given a design and not asked for my input.)

    I you're overwhelmed , welcome to Swing. It's not a toy, but it provides you low-level access to almost everything, so you can do almost anything. Take the time to dig into JTable, it's a very common component, and if you understand it, you will understand the others as well.

    Good luck!

Similar Threads

  1. Jtable
    By prashant in forum Java Applets
    Replies: 0
    Last Post: 07-19-2009, 07:04 PM
  2. Add a row in JTable
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 04-15-2009, 08:48 PM
  3. Regarding JTable
    By adeeb in forum AWT / Swing
    Replies: 12
    Last Post: 06-19-2008, 07:39 AM
  4. Regarding JTable
    By adeeb in forum SWT / JFace
    Replies: 0
    Last Post: 06-18-2008, 06:13 PM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

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