JComboBox as a Cell Renderer in a table column question

Discussion in 'Java' started by noident@my-deja.com, Jul 11, 2006.

  1. Guest

    Greetings!
    I am trying to use a JComboBox as a Cell Renderer for a JTable column.
    The combo box displays, but I can't open it, with either mouse or
    keyboard.
    Below is a piece of code that compiles and demonstrates the problem,
    creating a 1x1 table.
    I'm not really a Java Programmer, I'm a sysadmin, but my users refuse
    to use vi so I'm writing a GUI for them. Please help :)

    //========== ComboTest.java ==========
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;

    class ComboTest extends JFrame
    {
    static final int windowHeight = 100;
    static final int leftWidth = 100;
    static final int rightWidth = 100;
    static final int windowWidth = leftWidth + rightWidth;

    JTable table;

    public ComboTest()
    {
    super("ComboTest");
    // Create a one-row, one-column table - very simple
    table = new JTable(1,1);

    // Set our JComboBox as the renderer
    TableColumn column = table.getColumnModel().getColumn(0);
    column.setCellRenderer(new ComboRenderer());

    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) {System.exit(0);}
    });

    pack();

    getContentPane().add(new JScrollPane(table));

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = windowWidth + 10;
    int h = windowHeight + 10;
    setLocation(screenSize.width/3 - w/2, screenSize.height/2 - h/2);
    setSize(w, h);
    setVisible(true);
    }


    public static void main(String[] args)
    {
    new ComboTest();
    }
    }


    class ComboRenderer extends JComboBox implements TableCellRenderer
    {

    public ComboRenderer()
    {
    addItem("aaa");
    addItem("bbb");
    }

    public Component getTableCellRendererComponent(
    JTable table, Object value,
    boolean isSelected, boolean hasFocus,
    int row, int column)
    {
    System.err.println("popup: " + (isPopupVisible() ? "visible" :
    "invisible"));
    return this;
    }
    }
    //========end ComboTest.java============
     
    , Jul 11, 2006
    #1
    1. Advertising

  2. Vova Reznik Guest

    Changing item means editing a table.
    You need to implement custom editor.

    Simple fix will be:
    after line
    column.setCellRenderer(new ComboRenderer());
    add line
    column.setCellEditor(new DefaultCellEditor(new ComboRenderer()));

    and
    setSelectedItem(value);
    in method getTableCellRendererComponent



    wrote:
    > Greetings!
    > I am trying to use a JComboBox as a Cell Renderer for a JTable column.
    > The combo box displays, but I can't open it, with either mouse or
    > keyboard.
    > Below is a piece of code that compiles and demonstrates the problem,
    > creating a 1x1 table.
    > I'm not really a Java Programmer, I'm a sysadmin, but my users refuse
    > to use vi so I'm writing a GUI for them. Please help :)
    >
    > //========== ComboTest.java ==========
    > import javax.swing.*;
    > import javax.swing.table.*;
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > class ComboTest extends JFrame
    > {
    > static final int windowHeight = 100;
    > static final int leftWidth = 100;
    > static final int rightWidth = 100;
    > static final int windowWidth = leftWidth + rightWidth;
    >
    > JTable table;
    >
    > public ComboTest()
    > {
    > super("ComboTest");
    > // Create a one-row, one-column table - very simple
    > table = new JTable(1,1);
    >
    > // Set our JComboBox as the renderer
    > TableColumn column = table.getColumnModel().getColumn(0);
    > column.setCellRenderer(new ComboRenderer());
    >
    > addWindowListener(new WindowAdapter(){
    > public void windowClosing(WindowEvent e) {System.exit(0);}
    > });
    >
    > pack();
    >
    > getContentPane().add(new JScrollPane(table));
    >
    > Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    > int w = windowWidth + 10;
    > int h = windowHeight + 10;
    > setLocation(screenSize.width/3 - w/2, screenSize.height/2 - h/2);
    > setSize(w, h);
    > setVisible(true);
    > }
    >
    >
    > public static void main(String[] args)
    > {
    > new ComboTest();
    > }
    > }
    >
    >
    > class ComboRenderer extends JComboBox implements TableCellRenderer
    > {
    >
    > public ComboRenderer()
    > {
    > addItem("aaa");
    > addItem("bbb");
    > }
    >
    > public Component getTableCellRendererComponent(
    > JTable table, Object value,
    > boolean isSelected, boolean hasFocus,
    > int row, int column)
    > {
    > System.err.println("popup: " + (isPopupVisible() ? "visible" :
    > "invisible"));
    > return this;
    > }
    > }
    > //========end ComboTest.java============
    >
     
    Vova Reznik, Jul 11, 2006
    #2
    1. Advertising

  3. Guest

    Vova, thank you very much for your reply. That fixes my problem!
    I wouldn't have been able to work it out on my own, not with my level
    of Java/GUI experience.
     
    , Jul 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M

    Table cell renderer

    John M, Jan 27, 2004, in forum: Java
    Replies:
    1
    Views:
    671
    Christian Kaufhold
    Jan 28, 2004
  2. Chris
    Replies:
    0
    Views:
    8,278
    Chris
    Jun 14, 2005
  3. Branko Kaucic
    Replies:
    4
    Views:
    13,546
    Branko Kaucic
    Sep 13, 2005
  4. Beaned
    Replies:
    0
    Views:
    495
    Beaned
    Aug 25, 2006
  5. Albert
    Replies:
    0
    Views:
    421
    Albert
    Mar 26, 2009
Loading...

Share This Page