Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

how to make the JTable.editCellAt(row,col) work

Thread view: 
poorichard@gmail.com - 28 Sep 2006 10:21 GMT
hi,all
I'd to start editing the cell at row and column programatically,
the method JTable.editCellAt(row,col) is for that,
but there is something wrong in my code ,I think,

the whole code is as follows,
public class FrameTest {

    Logger logger = Logger.getLogger(FrameTest.class.getName());
    static int width = 500;
    static int height = 300;
    JFrame frame;
    public FrameTest(){
        logger.setLevel(Level.ALL);

        createUI();
    }

    JTable table;
    public void createUI(){
        logger.fine("start createUI");

        frame = new JFrame("TestFrame");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setPreferredSize(new Dimension(width,height));

        initTable();
        table.requestFocusInWindow();
        boolean d = table.editCellAt(0, 1);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    DefaultTableModel tableModel;
    static final String[] columnNames = {"a","b","c","d","e","f","g","h"};
    private void initTable(){
        tableModel = new DefaultTableModel(columnNames,2);
        table = new JTable(tableModel);
        table.setRowSelectionAllowed(false);

        table.requestFocusInWindow();
        boolean isEditing = table.editCellAt(0, 1);
               logger.info("isEditing?:"+isEditing);
    }

    public static void main(String[] args){
        new FrameTest();
    }
}

it can't work yet in my PC,
the platform I used is Eclipse3.2/3.1 and JDK1.5/1.6,
is there something wrong?

(I had post it half an hour ago,but it not show ,
so I post it again )

best regards,
Richard
poorichard@gmail.com - 29 Sep 2006 10:28 GMT
I'd got it,
the editCellAt(row,col) should invoked after the frame painted,
then it will work(though I'd not know why),

the code is as follows:

frame.setVisible(true);
table.editCellAt(row,col);

the main problem is that I need editCellAt custom cell while the
editing stopped,
the default sequence whould be [ (row,col)]
,(0,0),(0,1),....(1,0),(1,1)...
and I need the sequence likes  (0,0),(1,0),(0,1),(1,1)....

I tried this at the tableModelListener ,the code is as follows:
class ModelListener implements TableModelListener {

        public void tableChanged(TableModelEvent e) {
            tableModel.removeTableModelListener(this);
                       int editingRow = table.getEditingRow();
            int editingColumn = table.getEditingColumn();
                       if(editingRow == 0 && editingColumn == 0){
                table.requestFocusInWindow();
                table.editCellAt(1, 1);
                tableModel.addTableModelListener(this);
            }
               }
}

and this can't work,I think the problem may be the table will be
repainted after the tableChanged, so the editCell will be removed,

all right ,I know the JTable's actionMap can do this for the key
event,but without the mouse pressed,

so who can give me some tips?

best regards,
Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.