When an item is selected from a combobox that is longer than the combobox is wide, the end of the string is truncated to show only the beginning portion of the string that will fit.
When the combobox is set to editable, it is the end of the string which is shown, with the begining truncated (which makes sense, as it is as if the user had typed it)
Is there a way to set the caret position to the start of the entry to display the beginning of the value, whilst still allowing the combobox to be editable? Is this something to be achieved by writing a custom editor for the combobox?
This is one way:
JComboBox comboBox = new JComboBox( ... )
{
#Override
public void setSelectedItem(Object item)
{
super.setSelectedItem( item );
ComboBoxEditor editor = getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.setCaretPosition(0);
}
};
Related
I use mouse to drag a selection area in some JTable cells, the selection area is the yellow, so can anyone told me exactly how to clear the contents of selected cells by pressing "Delete" key in keyboard or a JButton?
The captured pic of selected cells:
Create an Acton to find the selected cells and clear the text. The easiest way is to loop through each cell in the table.
The basics of the Action would be something like:
Action clearAction = new Action()
{
#Override
public void actionPerformed(ActionEvent e)
{
for (each row in the table)
for (each column in the row)
if (table.isCellSelected(...))
table.setValueAt("", ...);
}
}
Then you create a button to invoke the Action:
JButton clearButton = new JButton( "Clear" );
clearButton.addActionListener( clearAction );
If you also want to use the Delete key then you can use Key Bindings to share the same Action.
The basic logic to add a new Key Binding to the JTable would be:
String keyStrokeAndKey = "DELETE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, keyStrokeAndKey);
table.getActionMap().put(keyStrokeAndKey, action);
Check out Key Bindings for more information.
I have putted the Arraylist into a JList and i want to get the value/index of the Arraylist when the mouse is clicked on the Jlist.
i have tried with these lines but it always shows -1 as index on the console for every elements clicked.
here is the part of my code..
list2.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
JPanel MousePanel=new JPanel();
JList listp=new JList(PatientDetailArlist.toArray());
int index = listp.getSelectedIndex();
System.out.println("Index Selected: " + index);
String sp = (String) listp.getSelectedValue();
System.out.println("Value Selected: " + sp.toString());
MousePanel.add(listp);
tab.add("tab4",MousePanel);
visibleGui();
}
You add a MouseListener to "list2" which is your JList.
list2.addMouseListener(new MouseListener(){
But then in your code for some reason you create a new JList? Well that JList is not visible on the GUI so there is no way it could have a selected index.
JList listp=new JList(PatientDetailArlist.toArray());
int index = listp.getSelectedIndex();
All you need in the listener code is:
int index = list2.getSelectedIndex();
Or even better is to get the JList component that was clicked from the MouseEvent:
JList list = (JList)e.getSource();
int index = list.getSelectedIndex();
However, that is still not a good solution. What if the user uses the keyboard to select an item? A proper design of a GUI should allow the user to use the mouse or the keyboard.
So you should not be using a MouseListener. Instead you should be using a ListSelectionListener to listen for changes in selection of an item in the list.
Read the section from the Swing tutorial on How to Write a ListSelectionListener for more information and examples to get you started.
I am trying to make a JTable with popup help menus for each column section. For instance, you right click on the first column title and a JTextArea pops up that explains what the column is for and what type of data should be put into it. I have the following code establishing the JTable and the mouselistener event. Is there a way I can write an If statement using ColumnAtPoint() so that if the right click happens at Column 1, then it opens up my JTextArea? Then I can create a second and third separate JTextAreas for my other columns.
final DefaultTableModel tblModel = new DefaultTableModel(null, colHdrs);
final JTable table = new JTable(tblModel);
table.getTableHeader().addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e))
{
JOptionPane.showMessageDialog(null, textArea1, "Type", JOptionPane.PLAIN_MESSAGE);
}
}
});
Try to use JToolTip I think it might be much more suited for your use ;) :)!
You can also add e.g. to a
JLabel label = new JLabel("My Number Label");
a tooltip text like :
label.setToolTipText("Only Numbers From 1-10 are allowed!");
This is also possible for other swing stuff, you can try it :).
The text will appear as soon as you hover over the label .
As a beginner,i am creating a jtable with some functionalities like adding and removing contents. I would like to know how to make a rename functionality to my application that on selecting this menu should highlight all the contents of the cell as in an editing mode. Thanks in advance
Continuing from your previous post.... Did you want something like below, when when you hit edit in the context menu, you can edit in some popup window?
→
You pretty much already have to tools for this functionality (in your code). For the new Action, you simply need to show a JOptionPane input dialog with the value of the selected cell. The return input of the JOptionPane will be the value you set back to the table. Something like. Keep in mind though, depending on the type of data, you may want to do some logical parsing or conversion. Below I just take the value as a String.
class EditCellAction extends AbstractAction {
private JTable table;
public EditCellAction(JTable table) {
putValue(NAME, "Edit");
this.table = table;
}
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String newValue = JOptionPane.showInputDialog(table,
"Enter a new value:", table.getValueAt(row, col));
((DefaultTableModel) table.getModel()).setValueAt(
newValue, row, col);
}
}
If you don't want the popup, and just want to programmatically start the cell editing, you can simple use table.editCellAt(row, col) to start the editing, and use the underlying text field of the cell editor to select the field contents. Something like below (tested and works)
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
table.editCellAt(row, col);
JTextField field = (JTextField) ((DefaultCellEditor) table
.getCellEditor()).getComponent();
field.requestFocus();
field.setSelectionStart(0);
int endSelection =
(!field.getText().isEmpty()) ? field.getText().length() -1 : 0;
field.setSelectionEnd(endSelection);
}
Keep in mind though, if the cell is editable, the user can just double click the cell to edit it. I guess this adds some more functionality
I have grid layout witch some fields added like that:
private Component userDetailsTab(final User user) {
final GridLayout details = new GridLayout(2, 1);
details.setMargin(true);
details.setSpacing(true);
details.addComponent(createDetailLabel(Messages.User_Name));
final Component username = createDetailValue(user.getName());
details.addComponent(username);
...
I have also Layout click listener which replace labels on text field, it looks like that:
final TextField tf = new TextField();
details.addListener(new LayoutClickListener() {
private static final long serialVersionUID = -7374243623325736476L;
#Override
public void layoutClick(LayoutClickEvent event) {
Component com = event.getChildComponent();
if (event.getChildComponent() instanceof Label) {
Label label = (Label)event.getChildComponent();
details.replaceComponent(com, tf);
tf.setValue(label.getValue());
}
}
});
In future I want to enable click on label, edit it and write changes to database after clicking somewhere else (on different label for example).
Now when I click on 1st label and then on 2nd label, effect is: 1st has value of 2nd and 2nd is text field witch value of 2nd. Why it's going that way? What should i do to after clicking on 1st and then 2nd get 1st label witch value of 1st?
You don't need to swap between Labels and TextFields, you can just use a TextField and style it look like a Label when it's not focused.
When I tried to create click-to-edit labels, it created a ton of extra work for me. I'd discourage it (and do as Patton suggests in the comments).
However, if you're going to insist on trying to create in-place editing, you will want to do the following:
Create a new class that extends a layout (e.g. HorizontalLayout), which can swap out a label for a text field
use LayoutClickListener to removeComponent(myLabel) and addComponent(myTextField)
use BlurListener to swap back to the label
use ValueChangeListener on the text field to copy its value to the label
This is a still a bad idea because:
Users cannot see affordances as easily (they can't tell what's editable)
Users cannot use the keyboard to tab to the field they want to edit
It adds unncessary complexity (maintenance time, etc).
I would recommend, if you want in-place editing, just show the text field, and save the new value with the BlurListener.