Hi Paul,

To catch focus events (also when you are navigating with Tab key and
Ctrl-Tab to leave table), you can use a FocusListener as follows:

ULCTable table = new ULCTable();
        table.addFocusListener(new IFocusListener() {

            public void focusGained(FocusEvent event) {
                System.out.println("Table Focus gained");
            }

            public void focusLost(FocusEvent event) {
                System.out.println("Table Focus lost");
            }

        });

The above works for empty tables too.

Now when a table has rows, a single click on the table selects a row and
sets focus in the table.

However, when a table has no rows, single click on the table does not set
focus on it. For table with rows, a single click on the area where there are
no rows does not result in the table gaining focus. This is because a
table's area is defined by its rows. This behavior is defined by Swing.
(FYI: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4310721).

For behavior such that a single click on empty table or in the area where
there are no rows should make the table gain focus, you will need to extend
ULCTable as shown in the snippet at the end of this mail.

I hope this helps.

Thanks and regards,

Janak



>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] Behalf Of
>[EMAIL PROTECTED]
>Sent: Wednesday, July 19, 2006 4:10 PM
>To: [email protected]
>Subject: [ULC-developer] ULCTable Focus
>
>
>Hello
>
>My application needs to be notified when a Data Table (based on a
>ULCTable) gains focus. Currently the only time the ULCTable gains
>focus is by clicking on a table record. I also need for a table to
>inform me when it gains focus when there are no records, or if a
>user clicks on a part of the table that has no records.
>
>Can you tell me how to make this possible?
>
>Thanks



import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTable;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.FocusEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IFocusListener;
import com.ulcjava.base.application.table.DefaultTableModel;
import com.ulcjava.base.client.UITable;
import com.ulcjava.base.development.DevelopmentRunner;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ULCTableFocusGainedSnippet extends AbstractApplication {

    public void start() {
        ULCFrame frame = new ULCFrame("ULCTableFocusGainedSnippet");
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);

        ULCBoxPane content = new ULCBoxPane(true);

        ULCButton but1 = new ULCButton("Button1");
        content.add(ULCBoxPane.BOX_CENTER_TOP, but1);

        final DefaultTableModel model = new DefaultTableModel(new Object[][]
{ { 0, 0 }, { 1, 1 } }, new String[] {
                "Col1", "Col2" });
        final ULCMyTable table = new ULCMyTable();

        table.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Table action performed");

            }
        });

        table.addFocusListener(new IFocusListener() {

            public void focusGained(FocusEvent event) {
                System.out.println("Table Focus gained");
            }

            public void focusLost(FocusEvent event) {
                System.out.println("Table Focus lost");
            }

        });

        ULCScrollPane sp = new ULCScrollPane(table);

        content.add(ULCBoxPane.BOX_EXPAND_EXPAND, sp);

        ULCButton but2 = new ULCButton("Set Model");
        but2.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                table.setModel(model);
            }
        });

        content.add(ULCBoxPane.BOX_CENTER_BOTTOM, but2);
        frame.add(content);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        DevelopmentRunner.setApplicationClass(ULCTableFocusGainedSnippet.cla
ss);
        DevelopmentRunner.main(args);
    }

    public static class ULCMyTable extends ULCTable {
        public ULCMyTable() {
        }

        protected String typeString() {
            return UIMyTable.class.getName();
        }
    }

    public static class UIMyTable extends UITable {
        protected void postInitializeState() {
            super.postInitializeState();
            getBasicTable().addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    getBasicTable().requestFocus();
                }

                public void mousePressed(MouseEvent e) {
                }

                public void mouseReleased(MouseEvent e) {
                }

                public void mouseEntered(MouseEvent e) {
                }

                public void mouseExited(MouseEvent e) {
                }
            });
        }
    }
}

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to