ashok rajendran

Greenhorn
+ Follow
since Mar 31, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ashok rajendran

I have JcheckBox and JTable in my JPanel. When user clicks or presses F2 to edit any cell value of the JTable a comboBox will appear with possible values. (This comboBox is coming from table CellEditor). When user presses ctrl+tab from the table focus should transfer to JComboBox all time. It is working only when the user presses ctrl+tab from the table cell which is not in editing mode. If the user presses ctrl+tab from the table cell which is in editing mode (i.e. focus is on the ComboBox of the cellEditor) it does not work. Please help me to find the solution.

I give a sample code here for your reference.
public class Frame1 extends JFrame {
public Frame1()
{
super();
this.setLayout( null );
this.setSize( new Dimension(400, 300) );

JTextField ch = new JTextField();
ch.setVisible(true);
ch.setBounds(10, 10, 10, 10);
this.add(ch, null);

DefaultTableModel tmodel = new DefaultTableModel(3, 1);
tmodel.setValueAt("0 0 1",0,0);
tmodel.setValueAt("1 0 1",1,0);
tmodel.setValueAt("2 0 1",2,0);

JTable custLayersTable = new JTable(tmodel);
custLayersTable.getColumnModel().getColumn(0).
setCellEditor(new ComboEditor());
custLayersTable.setBounds(new Rectangle(40, 40, 280, 145));
custLayersTable.setSurrendersFocusOnKeystroke(true);
this.add(custLayersTable, null);
}

public static void main(String[] args)
{
Frame1 a = new Frame1();
a.setVisible(true);
}
}

final class ComboEditor extends AbstractCellEditor
implements TableCellEditor
{
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
Vector<String> layerValSet = new Vector<String>();
for(int i=0; i<3; i++)
layerValSet.add(row+" "+column+" "+i);
mComboModel = new DefaultComboBoxModel(layerValSet);
mComboModel.setSelectedItem(value);
mEditorComp = new JComboBox(mComboModel);
return mEditorComp;
}

public Object getCellEditorValue()
{
return mEditorComp.getSelectedItem();
}

private DefaultComboBoxModel mComboModel;
private JComboBox mEditorComp;
}
12 years ago
I missed to inform the following in the post..
When i focus is on JComboBox pressing ctrl+tab does not take the focus to anywhere. The only change i can see after pressing ctrl+tab is that the focus lost from the JComboBox, but the JComboBox remains as it was before.
12 years ago
I have a check box and a JTable in my swing app. This JTable cells will become a JComboBox when user wants to edit the value. User can edit the cell either by clicking on that or by pressing F2 and UP/DOWN keys.
I want to navigate out from the JTable by pressing ctrl+tab.(Which is prescribed in sun java doc as well). So, when i press ctrl+tab from JTable the focus should come to the check box which is first component (and next component to JTable) in my app.
It does not work for me when my focus is on the JcomboBox. It works only when the focus is on the cell which is not in edit mode, i mean plain JTable cell. (If you press esc/tab key the JComboBox will disappear and your focus will be on plain JTable cell)
Please help me out to get my requirement.

Here is my sample app for you reference.
public class Frame1 extends JFrame
{
public Frame1()
{
super();
this.setLayout( null );
this.setSize( new Dimension(400, 300) );

JTextField ch = new JTextField();
ch.setVisible(true);
ch.setBounds(10, 10, 10, 10);
this.add(ch, null);

DefaultTableModel tmodel = new DefaultTableModel(3, 1);
tmodel.setValueAt("0 0 1",0,0);
tmodel.setValueAt("1 0 1",1,0);
tmodel.setValueAt("2 0 1",2,0);

JTable custLayersTable = new JTable(tmodel);
custLayersTable.getColumnModel().getColumn(0).
setCellEditor(new ComboEditor());
custLayersTable.setBounds(new Rectangle(40, 40, 280, 145));
this.add(custLayersTable, null);
}

public static void main(String[] args)
{
Frame1 a = new Frame1();
a.setVisible(true);
}
}

// This is the table cell editor which returns combo box to JTable.
final class ComboEditor extends AbstractCellEditor
implements TableCellEditor
{
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
Vector<String> layerValSet = new Vector<String>();
for(int i=0; i<3; i++)
layerValSet.add(row+" "+column+" "+i);
mComboModel = new DefaultComboBoxModel(layerValSet);
mComboModel.setSelectedItem(value);
mEditorComp = new JComboBox(mComboModel);
return mEditorComp;
}

public Object getCellEditorValue()
{
return mEditorComp.getSelectedItem();
}

private DefaultComboBoxModel mComboModel;
private JComboBox mEditorComp;
}
12 years ago
I have a simple JTable in my application. When the user clicks on a cell to edit the value a Jcombo box should appear. This combo Box is created in a table cell editor.
I have to stop the cell editing (Since my original application has some rules to execute on the "change value" event of the table.) when the user selects a value from the combo box. Since Java swing1.5 does not provide any api to catch the selection event (bug filed in jdk. bugid - 4199622), I am doing some workaround to catch the event using keyListener and ItemListener. (I haven't given the complete work around here, since it may confuse people.)
In the key event listener, I stop the cell editing for the key entries ENTER and SPACE bar. (Both of these are considered as a selection key in swing.) When i press ENTER key to select an item from the drop down, it works well. When I press the SPACE bar to select an item it throws IllegalStateException . Exception is as follows.
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location.

Any help on tis will be really appreciated.

Here is my sample code... After running the sample code, press F2 or click on the cell to get the drop down box..

// Use this as a main class
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Frame1 extends JFrame {

public Frame1() {
super();
this.setLayout( null );
this.setSize( new Dimension(400, 300) );

DefaultTableModel tmodel = new DefaultTableModel(3, 1);
tmodel.setValueAt("0 0 1",0,0);
tmodel.setValueAt("1 0 1",1,0);
tmodel.setValueAt("2 0 1",2,0);

JTable custLayersTable = new JTable(tmodel);
custLayersTable.getColumnModel().getColumn(0).
setCellEditor(new ComboEditor());
custLayersTable.setBounds(new Rectangle(40, 40, 280, 145));
this.add(custLayersTable, null);
}

public static void main(String[] args)
{
Frame1 a = new Frame1();
a.setVisible(true);
}
}

//Editor class
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

final class ComboEditor extends AbstractCellEditor
implements TableCellEditor
{
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
Vector<String> layerValSet = new Vector<String>();
for(int i=0; i<3; i++)
layerValSet.add(row+" "+column+" "+i);

mComboModel = new DefaultComboBoxModel(layerValSet);
mComboModel.setSelectedItem(value);
mEditorComp = new JComboBox(mComboModel);

mEditorComp.addKeyListener(new KeyAdapter() {
public void keyPressed( KeyEvent event ) {
super.keyPressed(event);
if((event.getKeyCode() == KeyEvent.VK_ENTER ||
event.getKeyCode() == KeyEvent.VK_SPACE) &&
mEditorComp.isPopupVisible())
{
stopCellEditing();
}
}
});
return mEditorComp;
}

public Object getCellEditorValue()
{
return mEditorComp.getSelectedItem();
}

private DefaultComboBoxModel mComboModel;
private JComboBox mEditorComp;
}
12 years ago
I have a JTable with 3 columns. The columns are radio button, jlabel and Jcombo Box. Each of them are retrieved using renderer. The first two columns are generated using super.getRendererComponent method. The combo box for last column is generated explicitly in the getRendererComponent method of DefaultCellRendererImpl class.

I give a simple layout of the program i have here.

// renderer for the 1st column of my table. It extends DefaultTableCellRenderer. We create a JRadiobutton in the getTableCellRendererComponent() method and return that component. So this column will be showed as radio button
final TableCellRenderer Col1Renderer = new RadioBtnTableCellRenderer();

//renderer for 2nd and 3rd column. It extends DefaultTableCellRenderer. In the getTableCellRendererComponent() method, we call super.getTableCellRendererComponent() method if it is 2nd column (It displays like a label) and we create a JcomboBox and return the component if it is a 3rd column.
final TableCellRenderer nameValRenderer = new NameValueCellRenderer();

// This model is returning values for each column appropriately
//Here column 0(1st) and 2(3rd) is marked as editable
CustLayersTableModel tm = new CustLayersTableModel();

// Set-up the JTable with custom renderers
custLayersTable = new JTable(tm)
{
// Override the cell render to show the layers name, value
// after tip layer as disabled
public TableCellRenderer getCellRenderer(int row, int column)
{
if ( column == LAYER_VAL_COL ||
column == LAYER_NAME_COL)
{
return nameValRenderer;
}
else if ( column == TIP_LAYER_COL )
{
return tipRenderer;
}

return super.getCellRenderer(row, column);
}
};

TableColumnModel colModel = custLayersTable.getColumnModel();

//The 1st column is configured with cellEditor class TipLayerCellEditor. This class extends AbstractCellEditor and implements TableCellEditor. In the getTableCellEditorComponent method we are creating a radiobutton with the values and returning the component.
TableColumn tipCol = colModel.getColumn(0);
tipCol.setCellEditor(new TipLayerCellEditor());

//The 3nd column is configured with cellEditor class LayerValueCellEditor. This class extends AbstractCellEditor and implements TableCellEditor. In the getTableCellEditorComponent method we are creating a comboBox with some values and returning the component.
TableColumn valCol = colModel.getColumn(2);
valCol.setCellEditor(new LayerValueCellEditor(this));

This code is working fine. We are able to edit the comboBox by clicking on that comboBox. When we do tab from other cells the focus is not coming to the combo Box to edit. Even after clicking the comboBox we can select the values only through mouse not by keyboard.

Please help me to bring the focus to the combo box of the third column
12 years ago