I have a simple code for Add button like this :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.addRow (new Object[] {name.getText(),mobile.getText()});
}
This code will get text from JTextField and insert into JTable row
I want to add function right-click popup menu when i right-click on table row, and add some like add delete rename.
How can I do that ?
Start by reading the section from the Swing tutorial on How to Bring up a Popup Menu for the basics of displaying a menu and a working demo.
In the case of a JTable, you will probably want to highlight the row that was clicked on so your Actions can act on the selected row.
So you would need to add code like the following to the maybeShowPopup(...) method from the demo example in the tutorial:
if (e.isPopupTrigger())
{
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
if (! source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(e.getComponent(), e.getX(), e.getY());
}
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 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 a jTable as from the attached picture
Right click on a row starts a jPopup, with a single item "Thread Stop".
I would like to return the row number by clicking on this menu item
How to accomplish this?
Thanks.
In your MouseListener where you show your popup, simply get the row and column numbers via the JTable methods:
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
System.out.printf("row, col: [%d, %d]%n", row, col);
// show pop-up menu here
}
});
Your implementation of TableCellEditor includes the row as a parameter, but you should act only when the TableModel is updated, as shown here. TablePopupEditor is a related example.
I am displaying 2 JTables and a JEditorPane in a JFrame. Both the Tables have different data. On double clicking the table2 i want to update table1 and the editor pane. I am able update the editor pane but not the table1. I tried the add e.getClickCount() == 2 for table1 , but its not working.
Basically when i click a row(which is the thread number) in Tabel2, the editorPane and table1 should update with the thread details. which looks like-
| 3105 | BOUNDARY_CORE_FCS | 20101216 105754399 | Entering XATransaction::getInstance
on doubleClick I am able to display that in the editorPane but not able to update it in the table. Any help would be greatly appreciated. Thanks.
The Code below is addMouseListener for table2-
JTable clsNewJTable = new JTable(new RCGUITableModel(arroData, arroHeaders));//... table2
JTable m_clsJTable = RCGUI.m_clsJTable2;// ... table 1
clsNewJTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.getClickCount() == 2){
JTable clsNewJTable1 = (JTable)e.getSource(); // gets table 2
int rowIndex = clsNewJTable1.getSelectedRow();
int colIndex = clsNewJTable1.getSelectedColumn();
clsNewJTable1.getSelectedRows();
Object strCellValue = clsNewJTable1.getValueAt(rowIndex, colIndex);
doUpdateThreadsInTextArea(strCellValue); // this displays in the jeditorPane
//Should i create the new table1 here?and then update it or adding a new mouselistener to table1 is better?
clsNewJTable1.setVisible(true);
}
}
});
More code is a must. More interesting would be to see how exactly you are making the update of the components. Do you fire table data change for the changed table'd model, e.g. tablemodel.fireTableDataChanged()?
Hope this helps, Boro.