N
noident
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============
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============