Results 1 to 5 of 5
  1. #1
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default [SOLVED] action listener and Jtextfield

    I try to write code for a telephone directory program. I wrote code for main fuctions(search-save-delete).here is code sample

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class Tel_Dir extends JFrame implements ActionListener {
    
        public static void main(String[] args) {
            new Tel_Dir();
        }
    
    
        JPanel mainpanel;
        CardLayout mainLayout;
    
        public Tel_Dir() {
            createGUI();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setAlwaysOnTop(false);
            setVisible(true);
        }
    
        private void createGUI() {
            mainLayout = new CardLayout();
            mainpanel = new JPanel();
            mainpanel.setLayout(mainLayout);
            mainpanel.add(new FirstPanel(null), "part1_layout");
            mainpanel.add(new SecondPanel(), "part2_layout");
            mainpanel.add(new ThirdPanel(), "part3_layout");
    
            JPanel panel = new JPanel();        
            panel.setLayout(new BorderLayout());
            panel.add(new SidePanel(this), BorderLayout.WEST);
            panel.add(mainpanel, BorderLayout.CENTER);
    
            getContentPane().add(panel);
        }
    
        public void actionPerformed(ActionEvent evn) {
            String cmd = evn.getActionCommand();
            if ("button1".equals(cmd)) {
                mainLayout.show(mainpanel, "part1_layout");
               
            } else if ("button2".equals(cmd)) {
                mainLayout.show(mainpanel, "part2_layout");
            } else if ("button3".equals(cmd)) {
                mainLayout.show(mainpanel, "part3_layout");
            }
        }
        public void actionPerformed2(ActionEvent e) {
            String cmd2 = e.getActionCommand();
            if ("button4".equals(cmd2)) {
               System.out.println("pushed");
            }
          
        }
    
        private class SidePanel extends JPanel {
    
            public SidePanel(ActionListener listener) {
                createGUI(listener);
            }
    
            private void createGUI(ActionListener listener) {
    
                setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                add(Box.createVerticalStrut(4));
    
                JButton quickbutton = new JButton("QUİCK SEARCH         ");
                quickbutton.addActionListener(listener);
                quickbutton.setActionCommand("button1");
                add(quickbutton);
                add(Box.createVerticalStrut(8));
    
                JButton savedelbutton = new JButton("SAVE OR DELETE     ");
                savedelbutton.addActionListener(listener);
                savedelbutton.setActionCommand("button2");
                add(savedelbutton);
                add(Box.createVerticalStrut(8));
    
                JButton standardbutton = new JButton("STANDARD SEARCH");
                standardbutton.addActionListener(listener);
                standardbutton.setActionCommand("button3");
                add(standardbutton);
                add(Box.createVerticalStrut(8));
    
            }
        }
    
        private class FirstPanel extends JPanel  {
        	 
    
            public FirstPanel(ActionListener listener2) {
                createGUI(listener2);
              
            }
    
           
    
    		private void createGUI(ActionListener listener2) {
                setLayout(null);
                JButton actionbutton1 = new JButton("action");
                actionbutton1.addActionListener(listener2);
                actionbutton1.setActionCommand("button4");
                actionbutton1.setBounds(30, 30, 100, 30);
                add(actionbutton1);       
            }
        }
      
        private class SecondPanel extends JPanel {
    
            public SecondPanel() {
                createGUI();
            }
    
            private void createGUI() {
                setLayout(new BorderLayout());
                add(new JLabel("Under Construction", JLabel.CENTER));
            }
        }
    
        private class ThirdPanel extends JPanel {
    
            public ThirdPanel() {
                createGUI();
            }
    
            private void createGUI() {
                setLayout(new BorderLayout());
                add(new JLabel("Under Construction", JLabel.CENTER));
            }
        }
    
    }
    I did not add code of functions(save-delete-search). Problem is about action button. It does not work. Also I need some Jtextfield on "FirstPanel" and I would like to give the coordiante for these Jtextfield. I want to take string value on texfield by using action button. Could you please tell me how to do these 3 things. Thanks in advance.

    Kind regards,

    Salih OZan IRBIK.
    A stitch in time saves nine:D

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    What listener do you think this constructor call ends up adding to the button?
    Java Code:
    new FirstPanel(null)
    db

  3. #3
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Before I wrote (null) compiler gives undefine constructor error. So, what is your advice for this error and other problems. Do not angry I just need a little help. Please:)

    Kind regards,

    Salih Ozan IRBIK.
    A stitch in time saves nine:D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So what problem are you having writing the ActionListener for this? There are many ways to stuff on action listener here, so which have you tried?

    i.e.,
    Java Code:
      private void createGUI()
      {
        ActionListener firstPanelActionListener = new ActionListener()
        {
          public void actionPerformed(ActionEvent arg0)
          {
            JOptionPane.showMessageDialog(mainpanel, "Boo!", "Fubars R Us", JOptionPane.ERROR_MESSAGE);
          }
        };
        mainLayout = new CardLayout();
        mainpanel = new JPanel();
        mainpanel.setLayout(mainLayout);
        mainpanel.add(new FirstPanel(firstPanelActionListener), "part1_layout");

  5. #5
    tOpach is offline Member
    Join Date
    Sep 2008
    Location
    Ankara-TURKEY
    Posts
    42
    Rep Power
    0

    Default

    Fubarable,

    I tried your sample but it does not work. Still have same problem. When I push "action" button there is no action. I will add some textfield on part1_layout and I wish to take data from textfield by using that "action" button.
    A stitch in time saves nine:D

Similar Threads

  1. Listener for print job
    By pjmorce in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 09:02 AM
  2. Replies: 7
    Last Post: 09-16-2008, 07:20 PM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-21-2008, 12:07 AM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 03:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM

Posting Permissions

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