amp834
asked on
Need a Java JTable or grid with typical editing facilities
I'm looking for a table or grid (DefaultTableModel or a Vector of fields model is ok) that allows easy insert, delete, copy/paste (or select + drag/drop), like a text editor, but with columned data. The table will probably have 20 to 500 items.
Can anyone suggest a simple library or some code? (Preferably free).
I don't want to code the insert, copy, paste, drag/drop stuff manually (or if I do, do it once and use it for different kinds tables). Copy/paste to editor as csv would be useful, too.
May need to do drag/drop from one table to another place (or from a place into the table) without having to rewrite everything (something modular would be good).
Something with simple "Notepad" like editing, but for columned data. (I would consider just using a text area, but there may be several fields that the user could easily confuse together).
Any suggestions?
Can anyone suggest a simple library or some code? (Preferably free).
I don't want to code the insert, copy, paste, drag/drop stuff manually (or if I do, do it once and use it for different kinds tables). Copy/paste to editor as csv would be useful, too.
May need to do drag/drop from one table to another place (or from a place into the table) without having to rewrite everything (something modular would be good).
Something with simple "Notepad" like editing, but for columned data. (I would consider just using a text area, but there may be several fields that the user could easily confuse together).
Any suggestions?
try JDNC!
JIDE has some quality components:
http://www.jidesoft.com/products/component.htm
and Smardec:
http://www.smardec.com/products/advanced-swing-components.html
You will have to try them out in order to see if they fit your needs. I doubt you will get by without any coding though.
You can use table.setCellEditor to change the behaviour when you double click in a cell.
//For specific columns (if you want different editors in different colums
TableColumn col = null;
col = table.getColumn("colname");
col.setCellEditor(new TableTextAreaEditor());
//To do the entire table
table.setCellEditor(new TableTextAreaEditor());
/*
* Edit this to implement other features you want in the editor
* KeyListeners can be added to provide ctrl+c etc
*/
public class TableTextAreaEditor extends DefaultCellEditor
{
/** Creates a new instance of TableTextAreaEditor */
public TableTextAreaEditor()
{
super(new JTextField());
final JTextArea textArea = new JTextArea();
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(null);
editorComponent = scrollPane;
delegate = new DefaultCellEditor.EditorDelegate()
{
@Override
public void setValue(Object value)
{
textArea.setText((value != null) ? value.toString() : "");
}
@Override
public Object getCellEditorValue()
{
return textArea.getText();
}
};
}
}
ASKER
Hi hazgoduk. Thanks for the response.
I need Copy/Paste of entire rows, so the user can easily edit the table as if it was a text file, but the lines have columns. Plus an easy way to add rows, insert empty rows, etc.
It's a lot of code to add, I'm sure someone has already done this many times!, and I'm hoping to get a library or sample coe.
I need Copy/Paste of entire rows, so the user can easily edit the table as if it was a text file, but the lines have columns. Plus an easy way to add rows, insert empty rows, etc.
It's a lot of code to add, I'm sure someone has already done this many times!, and I'm hoping to get a library or sample coe.
It's pretty simple. Add a keypressed listener to the table. You only need a couple of bits of code that are quite basic.
Copy would be loop through the table.getSelectedRows/cols and store the values to the clipboard in whatever format you want
Paste would be start with table.getselectedrow/col. can either start from col 0 in your table on the selected row and just do 0,1,2,3 so 4 values would fill a row starting from 0 and a 5th value would go to the next row or you could do: they've selected row 2,3 and cols 1,2, values get pasted in row2col1, row2col2, row3col1, row3col2 etc
Copy would be loop through the table.getSelectedRows/cols
Paste would be start with table.getselectedrow/col. can either start from col 0 in your table on the selected row and just do 0,1,2,3 so 4 values would fill a row starting from 0 and a 5th value would go to the next row or you could do: they've selected row 2,3 and cols 1,2, values get pasted in row2col1, row2col2, row3col1, row3col2 etc
if(evt.isControlDown() && evt.getKeyCode() == 67)
{
System.out.println("ctrl+c");
}
if(evt.isControlDown() && evt.getKeyCode() == 86)
{
System.out.println("ctrl+v");
//not sure how you're expecting them to paste, but would have to have some sort of seperating string. For example "a||b||c"
//clipboardtext.split("||")
}
//To add rows (for a blank row simply put "", "" etc)
//For n cols just do new Object[table.getColumnCount()] and a for loop
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(new Object[]{"a", "b", "c", "d"});
model.moveRow(table.getRowCount()-1, table.getRowCount()-1, desiredRow);
ASKER
hi hazgoduk. Is there a way to provide this functionality modularly, so I don't have to code it into every table, except maybe to call a function to enable it? Maybe a subclass?
Of course. I've recently started doing just this to provide standard functionality to basic form elements. For example, I have created a text field that sets a default value and changes the background colour if the text doesn't match the default, and also provided an isChanged function to it. Applying this to 50 or 100 text fields in a project takes ages and is a waste of time.
The following code provides a table you can add to forms. All you need to do is add any code you want in all of your tables into here. Overwrite functions that you want to or just add your own like any other object. The best thing about this method is changing this ExtendedTable class updates the entire code base.
If there are some functions you want in some tables but not others, just extend this object
The following code provides a table you can add to forms. All you need to do is add any code you want in all of your tables into here. Overwrite functions that you want to or just add your own like any other object. The best thing about this method is changing this ExtendedTable class updates the entire code base.
If there are some functions you want in some tables but not others, just extend this object
public class ExtendedTable extends JTable
{
/**
* Creates a new instance of ExtendedTable
*/
public ExtendedTable()
{
super();
//Example: you might want all your tables to have certain properties in by default
setGridColor(Color.WHITE);
}
}
ASKER
I can make the extended table as you just described. Do I then override a listener?
Can you give sample code for how to modularize the ctrl-c, ctrl-v code you incuded earlier?
If it can be done with just extending a class and providing some override code, it would be great!
Can you give sample code for how to modularize the ctrl-c, ctrl-v code you incuded earlier?
If it can be done with just extending a class and providing some override code, it would be great!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the long delay, I finally got back into this and almost have it working. I also found code to put the data onto the clipboard in an Excel format (tab delimited), so can do copy, cut, paste-overwrite, paste-insert, and even copy in/out from/to text file or Excel!
I made Paste-insert insert before the current row, which is the normal expected behavior.
To insert at the bottom of the table is a trick--the table doesn't let the user to past the last line. Either I have to put a "past at end" (I'd rather avoid this--it's one more thing the end-user would have to learn), or put a dummy last line. Any other ideas for that?
I made Paste-insert insert before the current row, which is the normal expected behavior.
To insert at the bottom of the table is a trick--the table doesn't let the user to past the last line. Either I have to put a "past at end" (I'd rather avoid this--it's one more thing the end-user would have to learn), or put a dummy last line. Any other ideas for that?
Put in a new blank row at the end before you paste? Or use moveRow
ASKER
Thanks for your help. It works well, I also found code for copy/paste to copy into a tab-delimited format, so it can copy and paste to/from spreadsheets easily, too.