Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JComboBox as a Cell Renderer in a table column question

Reply
Thread Tools

JComboBox as a Cell Renderer in a table column question

 
 
noident@my-deja.com
Guest
Posts: n/a
 
      07-11-2006
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============

 
Reply With Quote
 
 
 
 
Vova Reznik
Guest
Posts: n/a
 
      07-11-2006
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



http://www.velocityreviews.com/forums/(E-Mail Removed) 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============
>

 
Reply With Quote
 
 
 
 
noident@my-deja.com
Guest
Posts: n/a
 
      07-11-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: jcombobox custom renderer Albert Java 0 03-26-2009 07:16 AM
Different height at JComboBox editor and renderer Beaned Java 0 08-25-2006 05:38 PM
[JTable] Alignment in custom cell renderer Branko Kaucic Java 4 09-13-2005 07:03 AM
JTable Cell Editor - JComboBox column Chris Java 0 06-14-2005 09:09 PM
Table cell renderer John M Java 1 01-28-2004 04:03 PM



Advertisments