Start Editing a cell in JTable automaically - java

I am developing a restaurant billing system.
So here is the order panel interface
So now when i click on the menuTable the item code automatically gets added to kotTable
and when i press "Q" the focus shifts to quantity column in kotTable.
`
private void menuTableKeyTyped(java.awt.event.KeyEvent evt)
{
if(evt.getKeyChar()=='Q') {
kotTable.editCellAt(i-1,2);
}
} `
The problem is The cell doesnt start editing automatically. I need to click on that cell and then the editing starts.
i tried using DefaultCellEditor , getInputMap() and many other. But I am bit confused and the problem is not solved.. Thanx

Using a key binding, you can map the Q key to the table's "startEditing" action. More examples may be found here.
table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "startEditing");

Related

Selected Item from comboBox is always the same one [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've created a JComboBox that gets populated with an arraylist of strings. My comboBox gets populated correctly just as I want. My problem comes when getting the selected item: As I print it in a btnGo.addActionListener I realize that it is always the same one selected, even though I change the selection and click Go again. Is there a way to make the selection actually change? And can I do the same without having to click Go? Like just selecting the item from the comboBox and instantly do the action.
This is how I check what item is getting selected:
String selected = comboBox.getSelectedItem().toString();
And then I print selected in the btnGo.addActionListener
yes it's possible to execute an action when a comboBox element is selected. You just need to add an ActionListener to your comboBox ; here is a sample code :
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("item1"))
field.setText("AA");
else if(selected.toString().equals("item2"))
field.setText("BB");
}
});
getContentPane().add(comboBox);
getContentPane().add(field);
}
Please share source code in order to help you with the selection issue.

JavaFX Preventing TableMenuButton from hiding all columns

I want to use Table Menu Button (table.setTableMenuButtonVisible(true);) for hiding and showing specified columns in TableView. When I deselect all columns, [+] button hides, "No columns in table" pops in and user is unable to show any column.
I've tried to prevent hiding all columns by listening to table.getVisibleLeafColumns() and showing last hidden column, but then in ChoiceBox from Menu Button this column is unselected.
Definitely a bug (you might consider reporting it in fx' jira). The hack-around you mentioned in your question seems to work with a little trick borrowed from Swing: delay the reversion of visibility to some future:
ListChangeListener<? super TableColumn> visibleColumnsListener = c -> {
while (c.next()) {
// very last remove
if (c.wasRemoved() && !c.wasReplaced()) {
TableColumn column = c.getRemoved().get(0);
// delay reverting visibility
Platform.runLater(() -> {
column.setVisible(true);
});
}
}
};
It may be dirtier than its analogue in Swing, though, execution of the runnable is at "some unspecified time in future" and doesn't state its relation to normal (originating from the ui) events.
Reported as RT-38907 and just fixed (was duplicate: RT-37616), should bubble up in 8u40 ea in a week or two.

Transfer Items from One JComboBox to another JComboBox

Hi I'm trying to transfer one object from one combobox to another and vice versa.
To accomplish this,
I use actionListeners or ItemListeners, to no luck that they dont answer my problems, or maybe there's just something wrong in my implementation.
Assuming we have to comboboxes, combobox1 and combobox2.
Basically,
1. I first add the selected item to another combobox (combobox2)
2. I remove the selected item on the first combobox (combobox1)
When trying to debug this, i found out that everytime i am on the step of removing items, the listener of the other combobox fires, which does the same steps as above. This results into a loop, that just deletes the item, and places it back to the original combobox.
When using the ItemListener, with the proper if conditions of being selected or not, it throws a bigger error. Guys please help me..
*on edit mode/currently making an SSCE
Found this, as suggested by sir mKorbel.
It did the trick, setting the model via setModel(DefaultComboBoxModel model) method doesnt trigger the ActionListener when it tries to add the contents of the model passed, versus the addItem(Object obj) method that fires the ActionListener causing the havoc that i described on my question above.
Thanks guys, and i learned about a new thing called DefaultComboBoxModels!
jComboBox12.removeAllItems();
for (int t = 0; t < jComboBox11.getItemCount(); t++)
{
jComboBox12.addItem(jComboBox11.getItemAt(t));
}

Detecting if any cell is being edited in a JTable

I have a JTable in which users can edit cells. When the user is through editing the cell, I want to enable a button that allows the user to validate the input. However, I only want to do this when the user is no longer in edit mode. Is there an easy way for me to detect if any cell in a Jtable is currently being edited?
Thank you,
Even easier:
if (!table.isEditing())
//
It turns out that the Jtable has a way to detect if any cell is being edited. It is actually very simple. You simply check if myJtable.getCellEditor() == null. If it is not, you are editing a cell, if it is, no cells are being edited.
class TableEditor extends DefaultCellEditor{
JTextField com;
public TableEditor(){
getComponent().addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e){
com.setBackground(new Color.red);
}
});
}
}
And then
tb.setDefaultEditor(Object.class,new TableEditor());
I would use a custom TabelModel and override the setValueAt function to perform your validation. It should automatically be called whenever a user is done editing a table cell.

Making a JTable cell editable - but *not* by double clicking

I am trying to add a column to a JTable with the following behaviour (similar to Windows Explorer and similar shells):
The cell can be clicked once to select it, as usual.
The cell can be double-clicked to perform a separate action (launching an external program.)
The cell value (a string) can still be edited, by single-clicking a second time (after a pause) or by pressing F2 when the cell is highlighted.
Double-clicking must not trigger editing of the cell, but I would like to leave any other default editing triggers operational if possible.
I have tried adding a MouseListener to the table, and consuming all MouseEvents, but this does not work - if I return true from isCellEditable() then my listener never receives any click events but if I return false then F2 no longer works.
Can this be achieved using only event listeners? I would prefer not to mess with the PLAF functions if I can avoid it.
The DefaultCellEditor has a setClickCountToStart() method to control mouse clicks for editing. The default is 2. Changing this will have no effect on F2 functionality.
Therefore you can set editing to be a triple click.
Not sure exactly how to handle two single clicks to start editing but I guess you would use a Timer to keep track of the first click and then do the editing if the second single click is within you time interval.
You will have to make your own cellEditor and ovveride
public boolean isCellEditable( EventObject e )
You can distinguish between single and double click with the clickCount on the eventObject
If its a single Click and its on a selected cell you can return true otherwise return false;
retrieve row and column with
int row = ( (JTable) e.getSource() ).rowAtPoint(e.getPoint());
int column = ( (JTable) e.getSource() ).columnAtPoint(e.getPoint());
to enable F2 you can add custom inputMap en actionMap entries
similar too
table.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "doMyArrowDown");
table.getTable().getActionMap().put("doMyArrowDown", new ArrowDownAction());
and from your action you can then fire the cellediting yourself
table.editCellAt(row, column );
I have solved this by wrapping the existing CellEditor with a Proxy and intercepting calls to isCellEditable, returning false for all mouse events and delegating all other calls to the original CellEditor.
This is slightly more complex than camickr's solution but works for all editors (I have 4 in all.)

Categories