Results 1 to 4 of 4
  1. #1
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default [SOLVED] JTabbedPane and Action/Key Listeners

    Hello, I am having some issues with my GUI. I have a UserList GUI (not finished) that, on button click, will check for an open chat window and open a new window if none exists or add a tab if one does (this is working fine). My issue is that the listeners I added to my chat window only work for the last tab added. If I switch back to a previous tab, I can no longer use my input textField and send button to write to my textArea. I add listeners when I initially create the window and first tab. I thought about adding ChangeState listener, but couldn't work out how it would be any different than what I am doing now. Do I not fully understand how listeners are added to a particular object?

    I will post my code for the whole class so you can get the full picture.

    Java Code:
    public class Chat extends JFrame{
        private JPanel tabPanels, mainPanel;
        private JTextArea chatArea;
        private JTextField inputArea;
        private JButton send;
        private JTabbedPane tabPane;
        private JScrollPane scroll;
        private String name;
        
        Chat(String name){
            super("Chat");
            setLocationRelativeTo(null);
            setSize(400,350);
            setResizable(false);
            setVisible(true);
            
            chatArea = new JTextArea();
            chatArea.setEditable(false);
            chatArea.setLineWrap(true);
            inputArea = new JTextField();
            send = new JButton("Send");
            mainPanel = new JPanel(new BorderLayout());
            tabPanels = new JPanel();
            tabPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
            scroll = new JScrollPane(chatArea);
            this.name = name; 
            
            GridBagLayout panelSettings = new GridBagLayout();
            panelSettings.columnWidths = new int[]{320,80};
            panelSettings.rowHeights = new int[]{250,50};
            panelSettings.columnWeights = new double[]{1.0, 1.0};
            panelSettings.rowWeights = new double[]{1.0, 1.0};
            tabPanels.setLayout(panelSettings);
            
            GridBagConstraints chatConstraints = new GridBagConstraints();
            chatConstraints.fill = GridBagConstraints.BOTH;
            chatConstraints.insets = new Insets(5,5,5,5);
            chatConstraints.gridx = 0;
            chatConstraints.gridy = 0;
            chatConstraints.gridwidth = 2;
            tabPanels.add(scroll, chatConstraints);
            
            GridBagConstraints inputConstraints = new GridBagConstraints();
            inputConstraints.fill = GridBagConstraints.HORIZONTAL;
            inputConstraints.insets = new Insets(0,5,5,0);
            inputConstraints.gridx = 0;
            inputConstraints.gridy = 1;
            tabPanels.add(inputArea, inputConstraints);
            
            GridBagConstraints buttonConstraints = new GridBagConstraints();
            buttonConstraints.insets = new Insets(0,0,5,5);
            buttonConstraints.gridx = 1;
            buttonConstraints.gridy = 1;
            tabPanels.add(send, buttonConstraints);
            
            tabPane.add(name, tabPanels);
            mainPanel.add(tabPane);
            add(mainPanel);
            inputArea.requestFocusInWindow();
            
            /*send.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e){
                        send(inputArea.getText());
                    }
                });
            
            inputArea.addKeyListener(
                new KeyAdapter(){
                    @Override
                    public void keyPressed(KeyEvent e){
                        if(e.getKeyCode() == KeyEvent.VK_ENTER){
                            send(inputArea.getText());
                        }
                    }
                });*/
            
            tabPane.addChangeListener(
                new ChangeListener(){
                    @Override
                    public void stateChanged(ChangeEvent e){
                        send.addActionListener(
                            new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent e){
                                    send(inputArea.getText());
                                }
                            });
    
                        inputArea.addKeyListener(
                            new KeyAdapter(){
                                @Override
                                public void keyPressed(KeyEvent e){
                                    if(e.getKeyCode() == KeyEvent.VK_ENTER){
                                        send(inputArea.getText());
                                    }
                                }
                            });
                    }
                });
        }
        
        public void addATab(){
            chatArea = new JTextArea();
            chatArea.setEditable(false);
            chatArea.setLineWrap(true);
            inputArea = new JTextField();
            send = new JButton("Send");
            tabPanels = new JPanel();
            scroll = new JScrollPane(chatArea);
            
            GridBagLayout panelSettings = new GridBagLayout();
            panelSettings.columnWidths = new int[]{320,80};
            panelSettings.rowHeights = new int[]{250,50};
            panelSettings.columnWeights = new double[]{1.0, 1.0};
            panelSettings.rowWeights = new double[]{1.0, 1.0};
            tabPanels.setLayout(panelSettings);
            
            GridBagConstraints chatConstraints = new GridBagConstraints();
            chatConstraints.fill = GridBagConstraints.BOTH;
            chatConstraints.insets = new Insets(5,5,0,5);
            chatConstraints.gridx = 0;
            chatConstraints.gridy = 0;
            chatConstraints.gridwidth = 2;
            tabPanels.add(scroll, chatConstraints);
            
            GridBagConstraints inputConstraints = new GridBagConstraints();
            inputConstraints.fill = GridBagConstraints.HORIZONTAL;
            inputConstraints.insets = new Insets(0,5,5,0);
            inputConstraints.gridx = 0;
            inputConstraints.gridy = 1;
            tabPanels.add(inputArea, inputConstraints);
            
            GridBagConstraints buttonConstraints = new GridBagConstraints();
            buttonConstraints.insets = new Insets(0,0,5,5);
            buttonConstraints.gridx = 1;
            buttonConstraints.gridy = 1;
            tabPanels.add(send, buttonConstraints);
            
            tabPane.add(name, tabPanels);
        }
        
        public void send(String message){
            if(message.equals("")){
                return;
            }else{
                chatArea.append(inputArea.getText() + "\n");
                inputArea.setText(null);
            }
        }
    }
    Thanks for any help.
    Last edited by Carm; 04-27-2014 at 11:08 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: JTabbedPane and Action/Key Listeners

    First, you can get rid of most of your constructor code. Just create the main components where the tabs and buttons and textfields will be.

    Then you can simply add tabs using your addTab method. Within that method, declare you textAreas and fields locally and reference them in your actionListener you assign to your button. Then the button will know which panel to update when you press send. Also, move you send method code inside your action listener.

    The major elements to consider are.
    1. Have a unique action listener for each send button.
    2. Have that action listener reference the appropriate TextField and TextAreas.
    3. You have a nice method to add a new tabbed pane. So why duplicate that in the constructor? Remove the duplicate code from the
    constructor and then use the addTab method to add your tabs.

    And in your addTab method it should be something like this.

    Java Code:
    JTextArea chatArea = ...
    JTextField inputArea = ...
    send = new JButton("Send");
    send.addActionListener(
     /* in here, reference your chatArea and 
         inputArea.
    */
    }

    Regards,
    Jim
    Last edited by jim829; 04-27-2014 at 08:44 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: JTabbedPane and Action/Key Listeners

    Perfect Jim. Thank you very much.

    Java Code:
    public class Chat extends JFrame{
        
        private final JTabbedPane tabPane;
        
        Chat(){
            super("Chat");
            setSize(400,350);
            setLocationRelativeTo(null);
            setResizable(false);
            setVisible(true);
            
            JPanel mainPanel = new JPanel();
            tabPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
            mainPanel.add(tabPane);
            add(mainPanel);
        } // end constructor
        
        public void addATab(String name){
            final JTextArea chatArea = new JTextArea();
            chatArea.setEditable(false);
            chatArea.setLineWrap(true);
            final JTextField inputArea = new JTextField();
            final JButton send = new JButton("Send");
            JPanel tabPanels = new JPanel();
            JScrollPane scroll = new JScrollPane(chatArea);
            
            GridBagLayout panelSettings = new GridBagLayout();
            panelSettings.columnWidths = new int[]{320,80};
            panelSettings.rowHeights = new int[]{250,50};
            panelSettings.columnWeights = new double[]{1.0, 1.0};
            panelSettings.rowWeights = new double[]{1.0, 1.0};
            tabPanels.setLayout(panelSettings);
            
            GridBagConstraints chatConstraints = new GridBagConstraints();
            chatConstraints.fill = GridBagConstraints.BOTH;
            chatConstraints.insets = new Insets(5,5,0,5);
            chatConstraints.gridx = 0;
            chatConstraints.gridy = 0;
            chatConstraints.gridwidth = 2;
            tabPanels.add(scroll, chatConstraints);
            
            GridBagConstraints inputConstraints = new GridBagConstraints();
            inputConstraints.fill = GridBagConstraints.HORIZONTAL;
            inputConstraints.insets = new Insets(0,5,5,0);
            inputConstraints.gridx = 0;
            inputConstraints.gridy = 1;
            tabPanels.add(inputArea, inputConstraints);
            
            GridBagConstraints buttonConstraints = new GridBagConstraints();
            buttonConstraints.insets = new Insets(0,0,5,5);
            buttonConstraints.gridx = 1;
            buttonConstraints.gridy = 1;
            tabPanels.add(send, buttonConstraints);
            
            tabPane.add(name, tabPanels);
            inputArea.requestFocusInWindow();
            
            send.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e){
                        if(inputArea.getText() == null){
    
                        }else{
                            chatArea.append(inputArea.getText() + "\n");
                            inputArea.setText(null);
                        } // end if-else
                    } // end actionPerformed
                }); // end action listener
    
            inputArea.addKeyListener(
                new KeyAdapter(){
                    @Override
                    public void keyPressed(KeyEvent e){
                        if(e.getKeyCode() == KeyEvent.VK_ENTER){
                            if(inputArea.getText() == null){
    
                            }else{
                                chatArea.append(inputArea.getText() + "\n");
                                inputArea.setText(null);
                            } // end if-else
                        } // end if
                    } // end key pressed
                }); // end key adapter
            
            tabPane.addChangeListener(
                new ChangeListener(){
                    @Override
                    public void stateChanged(ChangeEvent e){
                        inputArea.requestFocusInWindow();
                    } // end state changed
                }); // end change listener
        } // end addATab
    } // end class

  4. #4
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: JTabbedPane and Action/Key Listeners

    Hello again. I have run into an issue. The above fix makes my tabs perform how I would like them to, but it has made it very difficult for me to find a way to writeObject() to the chatArea from an outside source. This chat window and tab code is part of a network chat program. When I get input from the server, I would like to display the message to the chatArea of the chat tab I was in.

    Any ideas on how I could go about doing this? The only thing left I haven't actually tried yet is to add the chatArea as a separate object from a different method, but I haven't fully wrapped my head around how to do that ( and then I need to figure out how to send data client-to-server-to-client... but need working IO first)

    Thanks for any help,

    Carm

Similar Threads

  1. ATM Machine - Action Listeners
    By javaStooge in forum New To Java
    Replies: 17
    Last Post: 03-27-2014, 08:45 PM
  2. question about action listeners
    By MW130 in forum New To Java
    Replies: 2
    Last Post: 05-08-2013, 03:45 PM
  3. action listeners
    By Ike in forum New To Java
    Replies: 13
    Last Post: 11-07-2011, 11:31 AM
  4. Adding Action Listeners
    By mamoonrizwan in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2011, 07:25 PM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 05:26 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
  •