Results 1 to 8 of 8
  1. #1
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Actionlister problem

    The following code is part of my GUI.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import static javax.swing.GroupLayout.Alignment.*;
    
    public class GUI extends JFrame
    {
        private Container container;
        private JTabbedPane tabbedPane;
        private JPanel mainPanel;
        public JComponent projectPanel;
        private JButton addProject;
        private GroupLayout layout; 
     
        public static void main(String[] args)
        {
            JFrame frame = new GUI();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        
        public GUI()
        {     
            setTitle("RMS");
            mainPanel = new JPanel();
            container = getContentPane();     
            tabbedPane = new JTabbedPane();
            tabbedPane.setPreferredSize(new Dimension(400,400));
            tabbedPane.setBackground(Color.white);
            projectPanel = makeProjectPanel();
            projectPanel.setBorder(BorderFactory.createTitledBorder("Options"));        
            tabbedPane.addTab("Project", projectPanel);
            container.add(mainPanel);
            mainPanel.add(tabbedPane);
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            pack();
        }
        
        protected JComponent makeProjectPanel()
        {
            JPanel panel = new JPanel();
            tabbedPane.setPreferredSize(new Dimension(400,400));
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            
            addProject = new JButton("Add Project");
            addProject.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                    createNewProject();
               }
            });
                
            addProject.setMaximumSize(new Dimension(210,30));
            addProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(addProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton editProject = new JButton("Edit Project");
            editProject.setMaximumSize(new Dimension(210,30));
            editProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(editProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton removeProject = new JButton("Remove Project");
            removeProject.setMaximumSize(new Dimension(210,30));
            removeProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(removeProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton listProject = new JButton("List Projects");
            listProject.setMaximumSize(new Dimension(210,30));
            listProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(listProject);   
                    panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton assignReference = new JButton("Assigned Reference to Project");
            assignReference.setMaximumSize(new Dimension(210,30));
            assignReference.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(assignReference);
                    panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton addNote = new JButton("Add Note to Project");
            addNote.setMaximumSize(new Dimension(210,30));
            addNote.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(addNote);
          
          return panel;
    
        }
        
        public void createNewProject()
        {
            projectPanel.removeAll();
            projectPanel.setBorder(BorderFactory.createTitledBorder(""));
            tabbedPane.setPreferredSize(new Dimension(560, 430));
            JPanel panel = new JPanel(); 
            panel.setBorder(BorderFactory.createTitledBorder("Create New Project"));
            layout = new GroupLayout(panel);
            panel.setLayout(layout);        
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            JLabel projectName = new JLabel("Name: ");
            JLabel projectDescription = new JLabel("Description: ");
            JLabel projectNote = new JLabel("Note: ");
            TextField nameTextfield = new TextField(10);
            TextArea descriptionTextarea = new TextArea(5,60);
            TextArea noteTextarea = new TextArea();
            JButton save = new JButton("Save");
            JButton cancel = new JButton("Cancel");
    [COLOR="Yellow"]        cancel.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                   makeProjectPanel();
               }
            }); [/COLOR]       
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(projectName)
                    .addComponent(projectDescription)
                    .addComponent(projectNote))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)       
                    .addComponent(nameTextfield)
                    .addComponent(descriptionTextarea)
                    .addComponent(noteTextarea)
                .addGroup(layout.createSequentialGroup()
                    .addGap(150).addComponent(cancel)
                    .addComponent(save))));
            
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectName)
                    .addComponent(nameTextfield))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectDescription)
                    .addComponent(descriptionTextarea))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectNote)
                    .addComponent(noteTextarea))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(cancel)
                    .addComponent(save)));
             
             projectPanel.add(panel);  
             pack();
        }
    }
    My problem is when I'm on the create new project panel and that I click on cancel, nothing happen. The actionlistener is no returning the makeProjectPanel method. Can anyone help me please. I'm new with actionlistener.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Please take away those yellow colour tags; I can't read it.

    kind regards,

    Jos

  3. #3
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import static javax.swing.GroupLayout.Alignment.*;
    
    public class GUI extends JFrame
    {
        private Container container;
        private JTabbedPane tabbedPane;
        private JPanel mainPanel;
        private JComponent projectPanel;
        private JButton addProject;
        private GroupLayout layout; 
     
        public static void main(String[] args)
        {
            JFrame frame = new GUI();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        
        public GUI()
        {     
            setTitle("RMS");
            mainPanel = new JPanel();
            container = getContentPane();     
            tabbedPane = new JTabbedPane();
            tabbedPane.setPreferredSize(new Dimension(400,400));
            tabbedPane.setBackground(Color.white);
            projectPanel = makeProjectPanel();
            projectPanel.setBorder(BorderFactory.createTitledBorder("Options"));        
            tabbedPane.addTab("Project", projectPanel);
            container.add(mainPanel);
            mainPanel.add(tabbedPane);
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            pack();
        }
        
        protected JComponent makeProjectPanel()
        {
            JPanel panel = new JPanel();
            tabbedPane.setPreferredSize(new Dimension(400,400));
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            
            addProject = new JButton("Add Project");
            addProject.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                    createNewProject();
               }
            });
                
            addProject.setMaximumSize(new Dimension(210,30));
            addProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(addProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton editProject = new JButton("Edit Project");
            editProject.setMaximumSize(new Dimension(210,30));
            editProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(editProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton removeProject = new JButton("Remove Project");
            removeProject.setMaximumSize(new Dimension(210,30));
            removeProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(removeProject);
            panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton listProject = new JButton("List Projects");
            listProject.setMaximumSize(new Dimension(210,30));
            listProject.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(listProject);   
                    panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton assignReference = new JButton("Assigned Reference to Project");
            assignReference.setMaximumSize(new Dimension(210,30));
            assignReference.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(assignReference);
                    panel.add(Box.createRigidArea(new Dimension(0,30)));
            JButton addNote = new JButton("Add Note to Project");
            addNote.setMaximumSize(new Dimension(210,30));
            addNote.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(addNote);
          
          return panel;
    
        }
        
        public void createNewProject()
        {
            projectPanel.removeAll();
            projectPanel.setBorder(BorderFactory.createTitledBorder(""));
            tabbedPane.setPreferredSize(new Dimension(560, 430));
            JPanel panel = new JPanel(); 
            panel.setBorder(BorderFactory.createTitledBorder("Create New Project"));
            layout = new GroupLayout(panel);
            panel.setLayout(layout);        
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            JLabel projectName = new JLabel("Name: ");
            JLabel projectDescription = new JLabel("Description: ");
            JLabel projectNote = new JLabel("Note: ");
            TextField nameTextfield = new TextField(10);
            TextArea descriptionTextarea = new TextArea(5,60);
            TextArea noteTextarea = new TextArea();
            JButton save = new JButton("Save");
            JButton cancel = new JButton("Cancel");
            cancel.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                   makeProjectPanel();
               }
            });        
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(projectName)
                    .addComponent(projectDescription)
                    .addComponent(projectNote))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)       
                    .addComponent(nameTextfield)
                    .addComponent(descriptionTextarea)
                    .addComponent(noteTextarea)
                .addGroup(layout.createSequentialGroup()
                    .addGap(150).addComponent(cancel)
                    .addComponent(save))));
            
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectName)
                    .addComponent(nameTextfield))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectDescription)
                    .addComponent(descriptionTextarea))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(projectNote)
                    .addComponent(noteTextarea))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(cancel)
                    .addComponent(save)));
             
             projectPanel.add(panel);  
             pack();
        }
    }
    Can u help me please?

  4. #4
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    I've removed the colour. can you help me please?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jmoutia View Post
    I've removed the colour. can you help me please?
    In your action listener of the cancel button, shouldn't that be:

    Java Code:
    projectPanel = makeProjectPanel();
    i.e. you don't want your projectPanel to refer to the old project panel.

    kind regards,

    Jos

  6. #6
    jmoutia is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    I made the changes but nothing happen

    Ex:
    Java Code:
            cancel.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                   projectPanel = makeProjectPanel();
    
                   makeProjectPanel();
               }
            });

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I made the changes but nothing happen

    You say nothing happens both here and in your original post.

    First check that the action listener is actually called. You can doing this by adding System.out.println("some message") at the very start of the action listener's handler method. That way at least something will happen that you can observe and confirm that the method is being called.

    Secondly you could describe what you expected to happen. Was something supposed to happen visually to the gui? If so, what? And which lines of your code were supposed to make that change to the gui?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jmoutia View Post
    I made the changes but nothing happen

    Ex:
    Java Code:
            cancel.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                   projectPanel = makeProjectPanel();
    
                   makeProjectPanel();
               }
            });
    One thing is you should remove that second makeProjectPanel() call and that panel should be added to the tabbed pane again (and the old one should be removed). Now the new panel just floats in the void.

    kind regards,

    Jos

Posting Permissions

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