Changing JTable keyboad handling

Hi
I need to implement a special interface to a JTable. I have JFrame that only contains a JTable, which is loaded with several rows of data. Initially the JTable is in "read-only" mode with single row selection. The table cells cannot be editted. The only keys that should work are up and down arrow, which move the selected row, and the enter key.
When the enter key is pressed the JTable goes into "row edit" mode. The user can edit the contents of the leftmost cell in the selected row. When done he presses enter, the contents are validated and if OK he is moved onto the second sell which he can now edit. This carries on until the rightmost cell is reached when the final enter returns the user to the initial "read only" mode.
While in "line edit" mode the up and down arrow keys must be disabled so the user cannot move off the row until it has been completely processed. For the moment I'm ignoring the use of the mouse and just concentrating on the keyboard.
If anybody has any ideas how I should attempt to do this I would be very grateful.
Thanks
Simon Bisson

You'll probably need to muck with the table's input and/or action maps.
You can find a bit more about these objects [url http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html]here.
The simplest is probably to just bind your own action to the selectPreviousRow
and selectNextRow keys in the action map.
You will likely also need to muck with the focus traversal keys so that
Tab and Shift-Tab do not do anything.
Finally, you will also need to do something for Enter in the input and/or
action maps for the table as well as for the table's editor.
: jay

Similar Messages

  • Changing jtable header when moving down

    Hi,
    I would need changing Jtable header, when the cursor switches between lines
    How to do this ???
    Best Regards
    S.Ancelot

    1) You could create a second table header and replace the old one using scrollPane.setColumnHeaderView(...)
    2) Or, you could simply change the values in each TableColumn and then repaint the header.

  • Changing JTable Row Color OnMouseOver

    Hello everybody,
    is it possible to change JTable Row Color when Mouse passes over it ??
    THanks!

    Yes...
    You have to override the table cell renderer and set the color of the rows based on flag.
    The flag can be turned on/off in a mouse motion listener of the table.

  • Couldn't change my name (handle)

    Hi,
    I went to "Your Control Panel" in order to change my name (handle) but the change doesn't take effect.
    For now I remain a number
    Please, advise.
    Carlos.

    Re: Uh-oh...changed my emailaddress/username
    Srini

  • Multiple JButtons in a JTable cell: handling events

    Hello!
    I'm trying to develop a Swing application which makes use of tables in several panels.
    Each row of each table should present the user two buttons, one for editing the row, one for viewing details of that row. Both editing and viewing are done in another panel.
    I thought I could add the buttons as the last column of each row, so I made a panel which holds the two buttons and the id of the row, so that I know what I have to edit or view.
    I managed to insert the panel as the last column, but I can't have the buttons click.
    I studied cell renderers and editors from several tutorials and examples, but evidently I can't understand either of them: whatever I try doesn't change the outcome... :(
    Below is the code I use, except for imports and packages:
    ActionPanel.java - The panel which holds the buttons and the row id
    public class ActionPanel extends JPanel{
      private static final long serialVersionUID = 1L;
      public static String ACTION_VIEW="view";
      public static String ACTION_EDIT="edit";
      private String id;
      private JButton editButton;
      private JButton viewButton;
      public String getId() {
        return id;
      public void setId(String id) {
        this.id = id;
      public JButton getEditButton() {
        return editButton;
      public void setEditButton(JButton editButton) {
        this.editButton = editButton;
      public JButton getViewButton() {
        return viewButton;
      public void setViewButton(JButton viewButton) {
        this.viewButton = viewButton;
      public ActionPanel() {
        super();
        init();
      public ActionPanel(String id) {
        super();
        this.id = id;
        init();
      private void init(){
        setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0))
        editButton=new JButton(new ImageIcon("./images/icons/editButtonIcon.png"));
        editButton.setBorderPainted(false);
        editButton.setOpaque(false);
        editButton.setAlignmentX(TOP_ALIGNMENT);
        editButton.setMargin(new Insets(0,0,0,0));
        editButton.setSize(new Dimension(16,16));
        editButton.setMaximumSize(new Dimension(16, 16));
        editButton.setActionCommand(ACTION_EDIT);
        editButton.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, id, "Editing", JOptionPane.INFORMATION_MESSAGE);
        viewButton=new JButton(new ImageIcon("./images/icons/viewButtonIcon.png"));
        viewButton.setMaximumSize(new Dimension(16, 16));
        viewButton.setActionCommand(ACTION_VIEW);
        viewButton.setBorderPainted(false);
        viewButton.setOpaque(false);
        viewButton.setMargin(new Insets(0,0,0,0));
        viewButton.setSize(new Dimension(16,16));
        viewButton.setMaximumSize(new Dimension(16, 16));
        viewButton.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, id, "Viewing", JOptionPane.INFORMATION_MESSAGE);
        add(viewButton);
        add(editButton);
    ActionPanelRenerer.java - the renderer for the above panel
    public class ActionPanelRenderer implements TableCellRenderer{
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component ret=(Component)value;
        if (isSelected) {
          ret.setForeground(table.getSelectionForeground());
          ret.setBackground(table.getSelectionBackground());
        } else {
          ret.setForeground(table.getForeground());
          ret.setBackground(UIManager.getColor("Button.background"));
        return ret;
    ActionPanelEditor.java - this is the editor, I can't figure out how to implement it!!!!
    public class ActionPanelEditor extends AbstractCellEditor implements TableCellEditor{
      public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {
        return (ActionPanel)value;
      public Object getCellEditorValue() {
        return null;
    ServicesModel.java - The way I fill the table is through a model:
    public class ServicesModel extends AbstractTableModel  {
      private Object[][] data;
      private String[] headers;
      public ServicesModel(Object[][] services, String[] headers) {
        this.data=services;
        this.headers=headers;
      public int getColumnCount() {
        return headers.length;
      public int getRowCount() {
        return data.length;
      public Object getValueAt(int row, int col) {
        if(col==data.length-1)
          return new ActionPanel(""+col);
        else
          return data[row][col];
      public boolean isCellEditable(int row, int col) {
        return false;
      public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    ServicesList.java - The panel which holds the table (BasePanel is a custom class, not related to the table)
    public class ServicesList extends BasePanel {
      private JLabel label;
      private JTable grid;
      public ServicesList(SessionManager sessionManager){
        super(sessionManager);
        grid=new JTable() ;
        add(new JScrollPane(grid), BorderLayout.CENTER);
        layoutComponents();
      public void layoutComponents(){
        ConfigAccessor dao=new ConfigAccessor(connectionUrl, connectionUser, connectionPass);
        String[] headers=I18N.get(dao.getServiceLabels());
        grid.setModel(new ServicesModel(dao.getServices(), headers));
        grid.setRowHeight(20);
        grid.getColumnModel().getColumn(headers.length-1).setCellRenderer(new ActionPanelRenderer());
        grid.setDefaultEditor(ActionPanel.class, new ActionPanelEditor());
        grid.removeColumn(grid.getColumnModel().getColumn(0));
        dao.close();
    }Please can anyone at least address me to what I'm doing wrong? Code would be better, but examples or hints will do... ;)
    Thank you very much in advance!!!

    Hello!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class MultipleButtonsInCellTest {
      public JComponent makeUI() {
        String[] columnNames = {"String", "Button"};
        Object[][] data = {{"AAA", null}, {"BBB", null}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
          @Override public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        JTable table = new JTable(model);
        table.setRowHeight(36);
        ActionPanelEditorRenderer er = new ActionPanelEditorRenderer();
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellRenderer(er);
        column.setCellEditor(er);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(table));
        p.setPreferredSize(new Dimension(320, 200));
        return p;
      public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() { createAndShowGUI(); }
      public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new MultipleButtonsInCellTest().makeUI());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    class ActionPanelEditorRenderer extends AbstractCellEditor
                       implements TableCellRenderer, TableCellEditor {
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      public ActionPanelEditorRenderer() {
        super();
        JButton viewButton2 = new JButton(new AbstractAction("view2") {;
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Viewing");
        JButton editButton2 = new JButton(new AbstractAction("edit2") {;
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Editing");
        panel1.setOpaque(true);
        panel1.add(new JButton("view1"));
        panel1.add(new JButton("edit1"));
        panel2.setOpaque(true);
        panel2.add(viewButton2);
        panel2.add(editButton2);
      @Override
      public Component getTableCellRendererComponent(JTable table, Object value,
                   boolean isSelected, boolean hasFocus, int row, int column) {
        panel1.setBackground(isSelected?table.getSelectionBackground()
                                       :table.getBackground());
        return panel1;
      @Override
      public Component getTableCellEditorComponent(JTable table, Object value,
                                    boolean isSelected, int row, int column) {
        panel2.setBackground(table.getSelectionBackground());
        return panel2;
      @Override
      public Object getCellEditorValue() {
        return null;
    }

  • How to change JTable column header text

    How do you set the text in the JTable column headers? I know you can create a JTable specifying the text in an array:
    <li>JTable(Object[][] rowData, Object[] columnNames)
    But if you create the JTable specifying a TableModel,
    <li>JTable(TableModel dm)
    the header text defaults to "A", "B", "C", etc. I cannot figure out how to access the text in the header names so it can be changed to something useful. I know how to get the JTableHeader for the table, but it does not seem to have methods for actually setting header values.

    I'm sure that model allows you to specify header values so you don't have to do so manually. I would be very surprised if it didn't override the default getColumnName() method to provide a reasonable names.She wasn't writing the class, but [url http://forums.oracle.com/forums/thread.jspa?messageID=9200751#9200751]outlining a design for me to implement. And, based on a previous comment I had made, I think she assumed I wanted the new design to look as much like the old as possible. There were no headers in the original design, which wasn't even a table.
    Anyway, this works:
        final static String statisticsColumnNames[] = {
         "Type", "Count",
         "Red QE", "Green QE", "Blue QE", "Average QE",
         "Distance"
         qErrors = new QEBeanTableModel();
         JTable errorTable = new JTable(qErrors);
         TableColumnModel tcm = errorTable.getColumnModel();
         for (int col = 0; col < statisticsColumnNames.length; col++)
             tcm.getColumn(col).setHeaderValue(statisticsColumnNames[col]);
    It looks like setHeaderValue() on the TableColumn is what I was looking for.Again, only used if you are dynamically changing the values at run time or you don't like the defaults provided by the Bean-aware model.I coded the above before I read your last post. The QEBeanTableModel is extremely specific to my program. I.e. I cannot imagine it being used anywhere else. Would it still be better to implement a getColumnName() within the table model? Looking at your [url http://www.camick.com/java/source/RowTableModel.java]RowTableModel.java source, I can see that it would not be difficult to do so.
    Just decided to add the getColumnName() method. This whole sub-project is based on implementing a clean modern design (and learning about Java Beans). You've clearly stated twice that the method I have implemented is for dynamic header values only, which has already answered what I asked last paragraph.

  • Changing JTable column names

    I have a JTable where the user can right click on the table header to change what data is displayed. When the user selects different data to display, I would like to change the column name to reflect this. What is the best way to do this?
    TIA
    ZeroFK

    table.getColumnModel().getColumn(???).setHeaderValue(???).

  • Change default PDF handler

    I downloaded  a trial of Adobe Acrobat...didn't buy it.  Have Adobe Reader, but the default is now Adobe Acrobat.  How do I change it so I can download to/with Adobe Reader?

    In either app, choose Edit > Preferences > General > Select default PDF handler.

  • Change the Error handling in DTP

    Hi Gurus,
    In the update tab it was 'No Update,No Reporting' and the Version was Active and green. I Chnaged it to 'valid RecirdsUpdate, No reporting (request Red)' and saved it . It is now green and active.I clicked on create Error DTPs. I am done with my issue and so want to change it back.I deleted the Error DTP.But now I can't change it back to 'No Update, No Reporting'
    It is now green and active but the error Handling now shows 'Valid Records Update, No Reporting (Request Red)'. How can I change it back to 'No Update, No Reporting'? I tried to change it and save it but it is then showing Version Revised and is yellow. Please suggest.
    Thanks
    Prasad

    Hi Prasad,
    When it says "Version Revised" and is yellow it means its not in active version . Please activate the DTP again by changing it to No update no reporting . It will be fine then.
    Regards
    Venky

  • How do I change pop-up handling

    On several shopping sites, when I click on a particular item to order it, the window never comes up. I get it ok using explorer. I'm thinking it has something to do with how firefox is handling popups, but I don't know how to chANGE IT.

    TenFourFox -- It's a port of the latest FireFox to run on older hardware and software.
    "World's most advanced web browser. Finely tuned for the Power PC."
        --  works for me on 10.4.  Supports 10.5
    http://www.floodgap.com/software/tenfourfox/
    alternative download site:
    http://www.macupdate.com/app/mac/37761/tenfourfox
    Turn on pipelining.  This will allow Firefox to make simaltaneous requests to the server.  Chrome has pipeling turned on. Some sites could fail to load with pipeling set on. The site will be old. See "Increase pipelining" in:
    http://www.hongkiat.com/blog/firefox-optimization-tips/
    OmniWeb uses the lastest Safari framework.  The open source WebKit. Other browsers like Safari and iCab use the OS version of WebKit.  The OmniWeb downloaded dmg includes it's own copy of the latest WebKit.
    http://www.omnigroup.com/products/omniweb/
    Safari 4.1.3 for Tiger
    http://support.apple.com/kb/DL1069
    iCab - The Taxi for the Internet
    http://www.icab.de/

  • Help! How do i change jTable Font Size

    Hi,
    I want to change the all table column text font size except first column.
    Please see the code which i wrote but it is giving errors.
    //set font size
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    Please any solution.
    Thanks in Advance
    sangeeta
    Here are the errors given below.
    C:\JSRC\MIGD>javac Table2.java
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable family
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable Dialog
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: name has private access in java.awt.Component
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable Dialog
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable style
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable plain
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    Table_celera2.java:145: cannot resolve symbol
    symbol : variable size
    location: class Table2
    Table.font(family=Dialog,name=Dialog,style=plain,size=12);
    ^
    7 errors
    C:\JSRC\MIGD>

    hi ma1evolance
    Thank you very much for your prompt reply.
    But when i run i got some errors please check. and give some good suggessions.
    I compile this program please see below
    public class FontRenderer extends DefaultTableCellRenderer implements TableCellRenderer
    //change them to whatever you like
    java.awt.Font font1 = new java.awt.Font("Dialog",1,12);
    java.awt.Font font2 = new java.awt.Font("Dialog",1,10);
    public FontRenderer()
    setFont(jTable1.getFont());
    public java.awt.Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    java.awt.Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus, row, column);
    //check for First Column
    if(table.convertIndexToModel(column) == 0)
    this.setFont(font1); //or whatever font you want
    else
    this.setFont(font2); //or this.setFont(table.getFont());
    return c;
    When I compile the above program then I got these errors. Please check this.
    C:\JSRC>javac FontRenderer.java
    FontRenderer.java:1: cannot resolve symbol
    symbol : class DefaultTableCellRenderer
    location: class FontRenderer
    public class FontRenderer extends DefaultTableCellRenderer implements TableCellR
    enderer
    ^
    FontRenderer.java:1: cannot resolve symbol
    symbol : class TableCellRenderer
    location: class FontRenderer
    public class FontRenderer extends DefaultTableCellRenderer implements TableCellR
    enderer
    ^
    FontRenderer.java:12: cannot resolve symbol
    symbol : class JTable
    location: class FontRenderer
    public java.awt.Component getTableCellRendererComponent(JTable table, Ob
    ject value, boolean isSelected, boolean hasFocus, int row, int column)
    ^
    FontRenderer.java:9: cannot resolve symbol
    symbol : variable jTable1
    location: class FontRenderer
    setFont(jTable1.getFont());
    ^
    FontRenderer.java:14: cannot resolve symbol
    symbol : variable super
    location: class FontRenderer
    java.awt.Component c = super.getTableCellRendererComponent(table,v
    alue,isSelected,hasFocus, row, column);
    ^
    FontRenderer.java:18: cannot resolve symbol
    symbol : method setFont (java.awt.Font)
    location: class FontRenderer
    this.setFont(font1); //or whatever font you want
    ^
    FontRenderer.java:20: cannot resolve symbol
    symbol : method setFont (java.awt.Font)
    location: class FontRenderer
    this.setFont(font2); //or this.setFont(table.getFont());
    ^
    7 errors
    C:\JSRC>

  • Changing the Receive Handler

    Is it possible to change the the FILE Receive Handler from 32 bit to 64 bit of the running application? Because I receive a File which is extremely large in size, it is taking so many days to receive the File from the receive location. I wanted to change
    the FILE adapter with 64 bit receive handler, since it is production I am concerned to stop the application and change the handler. Does it affect the process.

    Hello,
    All components of BizTalk works on streaming concept. As per your requirement you are not using any internal feature of BizTalk for message processing. Then you have to think for other solution as you are just using custom pipeline for file transfer. If
    you are planning to implement any pipeline for handling large file, it is always good to go with streaming concept  rather than reading the data.
    Yes you will get some performance boost by using 64 bit host, which does have a slight performance overhead. As you are using 64-bit OS and BizTalk it is advisable to use 64-bit host instances for better performance. But there can be limitations with some
    adapters and components (like FTP, BAM) and only run on 32-bit, in this case you can go for 32-bit host instance.
    You can test the transferring of file using passthourgh pipeline. I think so you will be able to transfer file in hr rather than few day. As you are only transferring the file BizTalk will read only chunk of data at a time. But the only drawback to
    it you are going to publish message in BizTalk. And BizTalk is not built for file transfer.
    http://social.technet.microsoft.com/wiki/contents/articles/30763.streaming-concept-in-biztalk-part1.aspx

  • JTable Event Handling for Boolean Columns

    I have a JTable (tableOne) with two columns. ColumnA contains simple text and ColumnB contains a boolean value which is rendered (via getColumnClass in the table model) as a check box.
    When the user selects/deselects a check box, an action must take place in another jtable (tableTwo) in the same frame (a column in tableTwo must be hidden/displayed).
    What is the correct way to do this type of event handling? Do I register the tableTwo as a listener with the table model for tableOne and then simply execute fireTableCellUpdated()? Is it appropriate for tableTwo to have access to tableOne's table model?
    Thanks for any help.
    NT

    Better create a table model listener that gets tableTwo in the constructor and keep it as a private member.
    Then regsiter this listener to tableOne model.

  • Change email and handle name

    Hi.
    I changed my email and my Handle name of my account but in my user profile and my old posts the name and the email not change.
    Thanks in advance

    Hi Siri
    I already changed my email address in https://myprofile.oracle.com and there is fine
    but if login in fourm, User Profile, the email did not change.
    I also want to change my visible name
    I changed everything today, maybe I have to give a time

  • NEW TEMPLATES-CHANGE TIME?  HANDLES?

    In FCP 6 there's the new templates which work with Motion, they're great but how do you change the time and handles? When I pick one, it defaults to a certain time because it's essentially a pre-made QT, some are 20 sec, some are 10 sec, etc.....
    When I use one and put in my information the in and out points are clear on the ends, therefore, no handles! If you "force" the handles, you lose part of template.
    Help please.

    David,
    Thank you, I knew that part, but I'm unfamiliar on how to change the time with in Motion. Since it's a template, it seems "locked" on that time. It's probably something simple I'm overlooking. This time problem has always alluded me.

Maybe you are looking for

  • Images were not displaying in the B2B web shop for the products

    Hi, We have an issue with Product catalog images.we maintained images for the products and replicated(Initial) to TREX server,replication was successfull,but the images were not displaying in the b2b webshop for the products. Anyone suggest what coul

  • Photoshop starter program 3.0

    I want to transfer photos in several collections to a flash drive and they are not in a file. How do I make a file to transfer?

  • Floating Screen Issue

    I have a MacBook. I have inadvertently done something which has enlarged the screen a bit (the window doesn't fit on the screen) and when I use my trackpad, the whole screen moves. Trackpad left, screen goes right. Trackpad up, screen goes down. I tr

  • Change LOV of column in view, based on values of another LOV of column in the same view

    Hi, I am using Jdeveloper 11.1.1.5. I have a scenario in which I have two columns in the view. column names are: 1- Column A 2- Column B                     both the columns have LOV's LOV in column A contains values as: 1, 2. Now, when I select the

  • Adhoc query designer

    Hi All, Is adhoc query designer (Portal) supports 7.3 version? we have 3.x version Thanks in advance Regards, Ravi