Solved

Need a Java JTable or grid with typical editing facilities

Posted on 2009-05-07
13
783 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:amp834
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24333659
try JDNC!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24333666
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 24333875

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.
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 3

Expert Comment

by:hazgoduk
ID: 24337661
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();
            }
        };
    }
}

Open in new window

0
 

Author Comment

by:amp834
ID: 24340295
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.
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24341324
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
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);

Open in new window

0
 

Author Comment

by:amp834
ID: 24376751
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?
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24378341
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
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);
    }
}

Open in new window

0
 

Author Comment

by:amp834
ID: 24379116
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!
0
 
LVL 3

Accepted Solution

by:
hazgoduk earned 250 total points
ID: 24380650
You don't have to override the listener, just use add key listener. Using the code below along with the algorithms above, you should be able to get the functionality you require
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.RED);
 
        addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyReleased(KeyEvent evt)
            {
                copypaste(evt);
            }
        });
    }
 
    public void copypaste(KeyEvent evt)
    {
        if(evt.isControlDown())
        {
            if(evt.getKeyCode() == 67)
            {
                System.out.println("ctrl+c");
            }
            else if(evt.getKeyCode() == 86)
            {
                System.out.println("ctrl+v");
            }
 
            evt.consume();
        }
    }
}

Open in new window

0
 

Author Comment

by:amp834
ID: 24541438
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?
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24542553
Put in a new blank row at the end before you paste? Or use moveRow
0
 

Author Closing Comment

by:amp834
ID: 31579302
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your phone running out of space to hold pictures?  This article will show you quick tips on how to solve this problem.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question