dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Keybinding in JDialog

  1. #1
    Join Date
    Jul 2018
    Posts
    1

    Keybinding in JDialog

    Hi, I want to ask about keybinding.
    Keybinding in f1 (jframe) works fine but somehow keybinding in d1 (jdialog) does not work.
    Here is my code below. Can someone tell me what mistake I've made?

    Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.table.DefaultTableModel;
     
    public class Dialog2 {
        private static JFrame f1;
         
        public static void main(String[] args) {
            f1=new JFrame("Frame");
            f1.setBounds(100, 100, 450, 300);
            f1.setLocationRelativeTo(null);
            f1.getContentPane().setLayout(new BorderLayout());
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JPanel f1cp=new JPanel();
            f1.getContentPane().add(f1cp, BorderLayout.CENTER);
            f1cp.setLayout(null);
             
            String[] columnNames = {"CODE", "NAME"};
             Object[][] data = {{null,null}};
             DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable f1table=new JTable(model);
            JScrollPane f1sp=new JScrollPane(f1table);
            f1sp.setBounds(20, 20, 400, 230);
            f1cp.add(f1sp);
             
            f1.setVisible(true);        
             
            f1table.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "f1enter");
            f1table.getActionMap().put("f1enter", new AbstractAction(){
                @Override
                public void actionPerformed(ActionEvent e){
                    showd1();
                }
            }); 
     
        } //end of main(string...
     
        private static void showd1(){
            JDialog d1=new JDialog(f1, "Dialog", true);
            d1.setBounds(20, 20, 410, 280);
            d1.setLocationRelativeTo(f1);
             
            JPanel d1cp=new JPanel();
            d1.getContentPane().add(d1cp, BorderLayout.CENTER);
            d1cp.setLayout(null);
             
            JTextField d1tf= new JTextField();
            d1tf.setBounds(20, 20, 360, 30);
            d1cp.add(d1tf);
             
            String[] columnNames = {"CODE", "NAME"};
             Object[][] data = {{null,null}};
             DefaultTableModel model = new DefaultTableModel(data, columnNames);
             JTable d1table=new JTable(model);
            JScrollPane d1sp=new JScrollPane(d1table);
            d1sp.setBounds(20, 70, 360, 170);
            d1cp.add(d1sp);
            d1.setVisible(true);        
             
            d1tf.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "d1tfenter");
            d1tf.getActionMap().put("d1tfenter", new AbstractAction(){
                @Override
                public void actionPerformed(ActionEvent e){             
                    d1table.setValueAt(1,0, 0);
                }
            });
        }
    }

    Thank you.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,787

    Re: Keybinding in JDialog

    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)