Disable CTRL+C on JTable (after duoble click on a cell) - java

I've been asked to edit a java desktop application developed by others. I've never worked on Java desktop application before.
There's a JTable and I have to prevent the user from doing CTRL+C and copy the value of a cell. I did it.
The problem is that when the user double clicks a cell and the cursor appears (as you can see from the pic)
I'm not able to prevent the user to do CTRL+C. How can I do it?

The problem is that when the user double clicks a cell and the cursor appears
Focus is no longer on the JTable. It is on the editor of the cell which happens to be a JTextField. So you need to remove the copy functionality of the text field.
You do this by removing the Key Binding for the "Control C" on the text field:
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Object.class);
JTextField textField = (JTextField)editor.getComponent();
InputMap im = textField.getInputMap();
im.put(KeyStroke.getKeyStroke("control C"), "none");
You will need to do this for each editor type you have in your table. For example if you have Integer values then you would need to get the editor for the Integer.class and remove its key binding as well.
Note, the same approach can be used for the table, except you use the following InputMap for the table:
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

Take away their keyboard!
Okay, that may be problematic. If you don't need the users to edit the table, you could try making the table non-editable. Rather than regurgitate one of many examples on this, I'll leave you this guide on doing it and a StackOverflow question on how to Disable user edit in JTable .
Alternatively, you could look into overriding the keybindings. I've never done that personally, so I leave you a link or two and a StackOverflow question on JTable Key Bindings .
You could also do something clever by accessing the System's Clipboard from java.awt.toolkit. Maybe have it that whenever a user is editing a cell, their clipboard is set to an empty string or (if you don't want to screw over people editing cells but not trying to copy them) you could combine that with the keybindings so they think they're copying data but they end up pasting "Sorry. Copying is not allowed in this application."
Hope something here works.

Related

When i navigate to some field using tab and enter some text text getting disappeared?

I have a table of forms which accepts first name, last name etc. When I navigate through using the tab key and enter some text in first name, all of the text disappears and that row gets selected. I have tried almost all techniques.
Is there any way we can avoid that using coding?
If I use the mouse then it wont give that error.
Assuming an otherwise correct use of JTable, you may need to terminate the edit when focus is lost, as suggested here and here:
table.putClientProperty("terminateEditOnFocusLost", true);

String Tokenizer.nextElement for JComboBox

I'm using
int TxtAge = Integer.parseInt(tfAge.getText().trim());
to get value from my textfield and search it in database.
Then, I'm using Integer age = Integer.parseInt(stringTokenizer.nextElement().toString()); to go to next attributes in my database.
I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement() for JComboBox? Is is the same with TextField?
String sex=(String) stringTokenizer.nextElement();
I tried this code but still failed :(
You seem to have left out the relevant portions of your code, i.e. how you are handling setting/getting items in the JComboBox. Whether you read these values from a database, a file or have them hardcoded is irrelevant to the question.
Since you do ask whether it is the same as with a JTextField, I can at least answer this; it is not the same. The question indicates that you're quite new to Swing. You would probably benefit from working through a basic Swing tutorial, just to get a grip on how to work with these basic GUI elements. For JComboBox, check out Oracles own How to Use Combo Boxes.
Anyways, when working with JComboBox, you will need to first populate it with the values that users can choose from and set the currently selected value. Retrieving the currently selected value is just a simple method call.
Further, you have the possibility of making a combobox editable. This means that the user can edit the text in the combo box to something that was not pre-populated. By default, this option is turned off.
I'll provide some examples.
Initialize:
JComboBox sexComboBox = new JComboBox();
sexComboBox.addItem("Not selected");
sexComboBox.addItem("Male");
sexComboBox.addItem("Female");
sexComboBox.addItem("Do not want to disclose");
By default, the first item you added is selected. To select another one, you need to add one of the following lines:
sexComboBox.setSelectedIndex(1); // zero-based index, "Male" is selected item
sexComboBox.setSelectedItem("Female"); // sets the selected item to "Female"
To enable user to edit the contents to something that was not pre-defined, just add the line:
sexComboBox.setEditable(true);
To retrieve the currently selected value:
String selectedItem = (String) sexComboBox.getSelectedItem();

JTable custom cell renderer focus problem

I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.
Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit Enter. So If I click the 'Save & Close' button, it'll show me an error that password field is empty.
Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. When I had to add more and more stuff, I changed it to a table like this.
For now I have put a label to let people know that they should hit the ENTER.. This is not nice. Another big issue. Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. In Windows system look, there's not much visible difference whether the field is focused or not.
I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.
So your problem is, that you are still editing the cell. So you have to stop the editing and then the cell will be changed.
At your button you can get the cell who is being edited with
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with
if(cellEditor!=null){
cellEditor.stopCellEditing();
}
and then you can save the value
Tell the table to automatically commit when losing focus:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Get correct editing behaviour in JTable using java DefaultCellEditor

You can go into a cell in a Jtable either by clicking on it, or by going into it using cursor keys/tabs. With defaultCellEditor and a JtextField if you go in using cursor keys the caret is put at the end of the existing text field, whereas if you double click the field it will highlight the last word.
Whereas spreadsheets seem to work (such as Open Office Calc) the same way for a double clcking, but if you tab into the field and start editing the field is cleared and the first character pressed becomes the first value in the field and so on.
I want my app to work the same way as the spreadsheet. By subclassing DefaultCellEditor and adding
final Caret caret = editField.getCaret();
caret.setDot(0);
editField.setText("");
I can get it to work how I want when tabbing but it also clears the field on double click which I dont want.
So please how can I determine if cell editing has been triggered by keyboard or mouse ?
override the isCellEditable(EventObject anEvent) method too.
So that you can capture the event which is going to trigger (or not) the table edition and act the way you want

How to Mirror Edits across a Table Cell and Text Field

The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.

Categories