Results 1 to 9 of 9
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default Help with Listeners

    I'm making a GUI with jButtons, jRadioButtons, and a jList. I've got the ActionListeners working for the jButtons and jRadioButtons, but can't seem to get the MouseListener working for the jList.

    I previously built this GUI in NetBeans form editor and it worked fine, although I didn't do much of the coding. I'm trying to learn Swing, so I started over from scratch and it was going well until I hit this little snag.

    I'm trying to get the JList to return the selected value. I want some of the labels (which I didn't include) to change based on which list item is selected. I've been trying to get a MouseListener working with a MouseClicked event for the last 4 hours and can't figure it out.

    QUESTION #1: I've got the ActionListeners working for my jButtons and jRadioButtons. How do I get the MouseListener working with the JList in the following code?

    In the code below, the jButtons and jRadioButtons are working without any issues or errors... so far. BUT, most of the tutorials and examples I've seen are seem to be using a subclass(?). I'm a little confused about how I should be building this.

    QUESTION #2: Am I building the jButton and jRadioButton ActionListeners correctly in the code below? They work fine, but I'm not sure if I did it the right way.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Lobby {
    
        private static void createAndShowLobbyGUI() {
            JFrame lobbyFrame = new JFrame("Welcome to Lobby");
            lobbyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lobbyFrame.setLayout(null);
            lobbyFrame.setSize(1280, 768);
            lobbyFrame.setVisible(true);
    
        // TOP PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelTop = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelTop);
            lobbyPlayersInLobbyPanelTop.setLayout(null);
            lobbyPlayersInLobbyPanelTop.setOpaque(true);
            lobbyPlayersInLobbyPanelTop.setVisible(true);
            lobbyPlayersInLobbyPanelTop.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelTop.setPreferredSize(new Dimension(1280, 120));
            Dimension size = lobbyPlayersInLobbyPanelTop.getPreferredSize();
            lobbyPlayersInLobbyPanelTop.setBounds(0, 0, size.width, size.height);
    
        // LEFT PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelLeft = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelLeft);
            lobbyPlayersInLobbyPanelLeft.setLayout(null);
            lobbyPlayersInLobbyPanelLeft.setOpaque(true);
            lobbyPlayersInLobbyPanelLeft.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelLeft.setPreferredSize(new Dimension(160, 648));
            size = lobbyPlayersInLobbyPanelLeft.getPreferredSize();
            lobbyPlayersInLobbyPanelLeft.setBounds(0, 120, size.width, size.height);
    
            String[] data = {"Player1", "Player2", "Player3", "Player4", "Player5", "Player6"};
            final JList lobbyPlayersInLobbyList = new JList(data);
            lobbyPlayersInLobbyList.setLayout(null);
            lobbyPlayersInLobbyList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            lobbyPlayersInLobbyList.setBackground(new Color(221, 215, 173));
            lobbyPlayersInLobbyList.setPreferredSize(new Dimension(140, 438));
            size = lobbyPlayersInLobbyList.getPreferredSize();
            lobbyPlayersInLobbyList.setBounds(10,30, size.width, size.height);
            lobbyPlayersInLobbyList.setFont(new Font("Tahoma", Font.PLAIN, 16));
            lobbyPlayersInLobbyList.setForeground(Color.black);
            lobbyPlayersInLobbyPanelLeft.add(lobbyPlayersInLobbyList);
    
            JButton lobbyChallengeButton = new JButton("Challenge");
            lobbyChallengeButton.setLayout(null);
            lobbyChallengeButton.setOpaque(true);
            lobbyChallengeButton.setPreferredSize(new Dimension(130, 30));
            size = lobbyChallengeButton.getPreferredSize();
            lobbyChallengeButton.setBounds(15, 573, size.width, size.height);
            lobbyChallengeButton.setFont(new Font("Tahoma", Font.BOLD, 18));
            lobbyChallengeButton.setForeground(Color.RED);
            lobbyPlayersInLobbyPanelLeft.add(lobbyChallengeButton);
    
            lobbyChallengeButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Challegne Button");
                }
            });
    
        // CENTER PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelCenter = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelCenter);
            lobbyPlayersInLobbyPanelCenter.setLayout(null);
            lobbyPlayersInLobbyPanelCenter.setOpaque(true);
            lobbyPlayersInLobbyPanelCenter.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelCenter.setPreferredSize(new Dimension(960, 468));
            size = lobbyPlayersInLobbyPanelCenter.getPreferredSize();
            lobbyPlayersInLobbyPanelCenter.setBounds(160, 120, size.width, size.height);
    
        // RIGHT PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelRight = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelRight);
            lobbyPlayersInLobbyPanelRight.setLayout(null);
            lobbyPlayersInLobbyPanelRight.setOpaque(true);
            lobbyPlayersInLobbyPanelRight.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelRight.setPreferredSize(new Dimension(160, 648));
            size = lobbyPlayersInLobbyPanelRight.getPreferredSize();
            lobbyPlayersInLobbyPanelRight.setBounds(1120, 120, size.width, size.height);
    
            JButton lobbyEditDecksButton = new JButton("Edit Decks");
            lobbyEditDecksButton.setLayout(null);
            lobbyEditDecksButton.setOpaque(true);
            lobbyEditDecksButton.setPreferredSize(new Dimension(130, 30));
            size = lobbyEditDecksButton.getPreferredSize();
            lobbyEditDecksButton.setBounds(15, 573, size.width, size.height);
            lobbyEditDecksButton.setMargin(new Insets(0, 0, 0, 0));
            lobbyEditDecksButton.setFont(new Font("Tahoma", Font.BOLD, 16));
            lobbyEditDecksButton.setForeground(new Color(0, 180, 0));
            lobbyPlayersInLobbyPanelRight.add(lobbyEditDecksButton);
    
            lobbyEditDecksButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Edit Decks Button");
                }
            });
    
        // BOTTOM PANEL (Lobby)
            JPanel lobbyPanelBottom = new JPanel();
            lobbyPanelBottom.setBackground(new Color(0, 0, 0));
            lobbyPanelBottom.setPreferredSize(new Dimension(960, 180));
            size = lobbyPanelBottom.getPreferredSize();
            lobbyPanelBottom.setBounds(160, 588, size.width, size.height);
            lobbyFrame.add(lobbyPanelBottom);
    
            JRadioButton lobbyRadioButton1 = new JRadioButton("RadioButton1");
            lobbyRadioButton1.setBackground(new Color(0, 0, 0));
            lobbyRadioButton1.setPreferredSize(new Dimension(135, 27));
            lobbyRadioButton1.setForeground(Color.RED);
            lobbyPanelBottom.add(lobbyRadioButton1);
    
            lobbyRadioButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Radio Button 1");
                }
            });
    
            JRadioButton lobbyRadioButton2 = new JRadioButton("RadioButton2");
            lobbyRadioButton2.setBackground(new Color(0, 0, 0));
            lobbyRadioButton2.setPreferredSize(new Dimension(135, 27));
            lobbyRadioButton2.setForeground(Color.RED);
            lobbyPanelBottom.add(lobbyRadioButton2);
    
            lobbyRadioButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Radio Button 2");
                }
            });
    
        //Group the radio buttons.
            ButtonGroup lobbyButtonGroup = new ButtonGroup();
            lobbyButtonGroup.add(lobbyRadioButton1);
            lobbyButtonGroup.add(lobbyRadioButton2);
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowLobbyGUI();
                }
            });
    
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Psyclone View Post
    I'm making a GUI with jButtons, jRadioButtons, and a jList. I've got the ActionListeners working for the jButtons and jRadioButtons, but can't seem to get the MouseListener working for the jList.
    Usually you don't use mouse listeners on a jlist. Usually you'd use a ListSelectionListener.

    I previously built this GUI in NetBeans form editor and it worked fine, although I didn't do much of the coding. I'm trying to learn Swing, so I started over from scratch and it was going well until I hit this little snag.
    Good for you! Keep at it!

    I'm trying to get the JList to return the selected value. I want some of the labels (which I didn't include) to change based on which list item is selected. I've been trying to get a MouseListener working with a MouseClicked event for the last 4 hours and can't figure it out.
    Again, I'd first try with a ListSelectionListener (as per the tutorial) coupled with a ListCellRenderer.

    In the code below, the jButtons and jRadioButtons are working without any issues or errors... so far. BUT, most of the tutorials and examples I've seen are seem to be using a subclass(?). I'm a little confused about how I should be building this.
    In the tutorials, they have the overall class implement ActionListener which is OK for small "toy" programs, but your current way of handling this with anonymous inner classes is better. Don't stop doing this.

  3. #3
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    OK, I got this working two different ways, but I know this first one isn't right. I couldn't figure out how to get the ListCellRenderer to work and keep getting errors.

    SOLUTION #1:

    I happened across this solution, but it returns the value twice. Once when the mouse clicks on the item in the list, and once when it is released. This works, but it's not ideal since the list I'm using is SINGLE_SELECTION.

    Java Code:
            ListSelectionListener listSelectionListener = new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent listSelectionEvent) {
                  JList list = (JList) listSelectionEvent.getSource();
                  int selections = list.getSelectedIndex();
                  System.out.println(selections);
                }
            };
            lobbyPlayersInLobbyList.addListSelectionListener(listSelectionListener);
    SOLUTION #2:

    The 2nd solution I came up with works exactly the way I need it to, plus it has the ability to recognize double mouse clicks, which I'm going to probably use in the next day or so for another list (i'm going to use one list to populate another and would like to do it without using a button).

    Java Code:
     final JList list = new JList(data);
     MouseListener mouseListener = new MouseAdapter() {
                @Override
         public void mouseClicked(MouseEvent e) {
                    int index1 = list.locationToIndex(e.getPoint());
                    System.out.println("Single clicked on Item " + index1);
             if (e.getClickCount() == 2) {
                 int index2 = list.locationToIndex(e.getPoint());
                 System.out.println("Double clicked on Item " + index2);
              }
         }
     };
     lobbyPlayersInLobbyList.addMouseListener(mouseListener);
    I know neither of these use the ListCellRenderer you were talking about. I'd actually like to learn how to use it as well, but couldn't figure it out and came across a solution that seems to work for now.

    Is there any advantages/disadvantages in using Solution #2 in lieu of using ListCellRenderer?

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    UGH! Ok, I just realized that the 2nd solution has an issue. It sometimes returns the wrong index for some reason. LOL

  5. #5
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    I'm so close to getting this but can't figure out why I can't pass the value selected from the list to a label. I want the value selected from the list to change the label to that value. I have it printing the value and index number out correctly using System.out.println(), but can't get the label to change to the value. I even have it converted to a String and it still won't work.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Lobby {
    
        private static void createAndShowLobbyGUI() {
            JFrame lobbyFrame = new JFrame("Welcome to Lobby");
            lobbyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lobbyFrame.setLayout(null);
            lobbyFrame.setSize(1280, 768);
            lobbyFrame.setVisible(true);
    
        // TOP PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelTop = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelTop);
            lobbyPlayersInLobbyPanelTop.setLayout(null);
            lobbyPlayersInLobbyPanelTop.setOpaque(true);
            lobbyPlayersInLobbyPanelTop.setVisible(true);
            lobbyPlayersInLobbyPanelTop.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelTop.setPreferredSize(new Dimension(1280, 120));
            Dimension size = lobbyPlayersInLobbyPanelTop.getPreferredSize();
            lobbyPlayersInLobbyPanelTop.setBounds(0, 0, size.width, size.height);
    
        // LEFT PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelLeft = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelLeft);
            lobbyPlayersInLobbyPanelLeft.setLayout(null);
            lobbyPlayersInLobbyPanelLeft.setOpaque(true);
            lobbyPlayersInLobbyPanelLeft.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelLeft.setPreferredSize(new Dimension(160, 648));
            size = lobbyPlayersInLobbyPanelLeft.getPreferredSize();
            lobbyPlayersInLobbyPanelLeft.setBounds(0, 120, size.width, size.height);
    
            String[] data = {"Player1", "Player2", "Player3", "Player4", "Player5", "Player6"};
            final JList lobbyPlayersInLobbyList = new JList(data);
            lobbyPlayersInLobbyList.setLayout(null);
            lobbyPlayersInLobbyList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            lobbyPlayersInLobbyList.setBackground(new Color(221, 215, 173));
            lobbyPlayersInLobbyList.setPreferredSize(new Dimension(140, 438));
            size = lobbyPlayersInLobbyList.getPreferredSize();
            lobbyPlayersInLobbyList.setBounds(10,30, size.width, size.height);
            lobbyPlayersInLobbyList.setFont(new Font("Tahoma", Font.PLAIN, 16));
            lobbyPlayersInLobbyList.setForeground(Color.black);
            lobbyPlayersInLobbyPanelLeft.add(lobbyPlayersInLobbyList);
    
    [B][COLOR="Blue"]        ListSelectionListener listSelectionListener = new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent listSelectionEvent) {
                    boolean adjust = listSelectionEvent.getValueIsAdjusting();
                    if (!adjust)
                        {
                        JList list = (JList) listSelectionEvent.getSource();
                        int selection = list.getSelectedIndex();
                        Object selectionValue = list.getSelectedValue();
                        System.out.println(selection + "/" + selectionValue + " ");
                        String name = (String) list.getSelectedValue();
                        System.out.println(name);
                        [COLOR="Red"]lobbyOpponentNameLabel.setText(name);[/COLOR]
                        }
                    }
                };
            lobbyPlayersInLobbyList.addListSelectionListener(listSelectionListener);[/COLOR][/B]
    
            JButton lobbyChallengeButton = new JButton("Challenge");
            lobbyChallengeButton.setLayout(null);
            lobbyChallengeButton.setOpaque(true);
            lobbyChallengeButton.setPreferredSize(new Dimension(130, 30));
            size = lobbyChallengeButton.getPreferredSize();
            lobbyChallengeButton.setBounds(15, 573, size.width, size.height);
            lobbyChallengeButton.setFont(new Font("Tahoma", Font.BOLD, 18));
            lobbyChallengeButton.setForeground(Color.RED);
            lobbyPlayersInLobbyPanelLeft.add(lobbyChallengeButton);
    
            lobbyChallengeButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Challegne Button");
                }
            });
    
        // CENTER PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelCenter = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelCenter);
            lobbyPlayersInLobbyPanelCenter.setLayout(null);
            lobbyPlayersInLobbyPanelCenter.setOpaque(true);
            lobbyPlayersInLobbyPanelCenter.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelCenter.setPreferredSize(new Dimension(960, 468));
            size = lobbyPlayersInLobbyPanelCenter.getPreferredSize();
            lobbyPlayersInLobbyPanelCenter.setBounds(160, 120, size.width, size.height);
    
        // RIGHT PANEL (Lobby)
            JPanel lobbyPlayersInLobbyPanelRight = new JPanel();
            lobbyFrame.add(lobbyPlayersInLobbyPanelRight);
            lobbyPlayersInLobbyPanelRight.setLayout(null);
            lobbyPlayersInLobbyPanelRight.setOpaque(true);
            lobbyPlayersInLobbyPanelRight.setBackground(new Color(0, 0, 0));
            lobbyPlayersInLobbyPanelRight.setPreferredSize(new Dimension(160, 648));
            size = lobbyPlayersInLobbyPanelRight.getPreferredSize();
            lobbyPlayersInLobbyPanelRight.setBounds(1120, 120, size.width, size.height);
    
            JButton lobbyEditDecksButton = new JButton("Edit Decks");
            lobbyEditDecksButton.setLayout(null);
            lobbyEditDecksButton.setOpaque(true);
            lobbyEditDecksButton.setPreferredSize(new Dimension(130, 30));
            size = lobbyEditDecksButton.getPreferredSize();
            lobbyEditDecksButton.setBounds(15, 573, size.width, size.height);
            lobbyEditDecksButton.setMargin(new Insets(0, 0, 0, 0));
            lobbyEditDecksButton.setFont(new Font("Tahoma", Font.BOLD, 16));
            lobbyEditDecksButton.setForeground(new Color(0, 180, 0));
            lobbyPlayersInLobbyPanelRight.add(lobbyEditDecksButton);
    
            lobbyEditDecksButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Edit Decks Button");
                }
            });
    
        // BOTTOM PANEL (Lobby)
            JPanel lobbyPanelBottom = new JPanel();
            lobbyPanelBottom.setBackground(new Color(0, 0, 0));
            lobbyPanelBottom.setPreferredSize(new Dimension(960, 180));
            size = lobbyPanelBottom.getPreferredSize();
            lobbyPanelBottom.setBounds(160, 588, size.width, size.height);
            lobbyFrame.add(lobbyPanelBottom);
    
            JRadioButton lobbyRadioButton1 = new JRadioButton("RadioButton1");
            lobbyRadioButton1.setBackground(new Color(0, 0, 0));
            lobbyRadioButton1.setPreferredSize(new Dimension(135, 27));
            lobbyRadioButton1.setForeground(Color.RED);
            lobbyPanelBottom.add(lobbyRadioButton1);
    
            lobbyRadioButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Radio Button 1");
                }
            });
    
            JRadioButton lobbyRadioButton2 = new JRadioButton("RadioButton2");
            lobbyRadioButton2.setBackground(new Color(0, 0, 0));
            lobbyRadioButton2.setPreferredSize(new Dimension(135, 27));
            lobbyRadioButton2.setForeground(Color.RED);
            lobbyPanelBottom.add(lobbyRadioButton2);
    
            lobbyRadioButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Radio Button 2");
                }
            });
    
        //Group the radio buttons.
            ButtonGroup lobbyButtonGroup = new ButtonGroup();
            lobbyButtonGroup.add(lobbyRadioButton1);
            lobbyButtonGroup.add(lobbyRadioButton2);
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowLobbyGUI();
                }
            });
    
        }
    }

    The line highlighted in Red is giving me the following error.

    Java Code:
    cannot find symbol
       symbol: variable lobbyOpponentNameLabel
    Isn't this an object though? How can I change the text value of lobbyOpponentNameLabel?

    I was able to do it in the past when I used NetBeans, but it built most of the code. It seems there is something simple I am missing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Psyclone View Post
    Java Code:
    cannot find symbol
       symbol: variable lobbyOpponentNameLabel
    Isn't this an object though? How can I change the text value of lobbyOpponentNameLabel?
    You should be able to answer this for us -- where is this variable declared? initialized?

  7. #7
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    LOL... I don't know the answer to this one sadly. I thought it was an object.

    I'm gonna do a lot of reading tonight again and maybe I'll have an answer tomorrow. :D

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Psyclone View Post
    LOL... I don't know the answer to this one sadly. I thought it was an object.
    It's not an object that you're looking for, but rather a variable, and a variable doesn't magically appear by itself but rather only exists if you've declared it and only holds an object if you assign one to it. So do a simple text search of your code and see if you've created this variable, because for the life of me, I can't find it anywhere in your code except where you try to use it.

    edit:
    in other words, where in your code is this line:
    Java Code:
    JLabel lobbyOpponentNameLabel = new JLabel("....");
    or even simply
    Java Code:
    JLabel lobbyOpponentNameLabel;
    Because again, I don't see anywhere where you've declared the variable or initialized it. And without doing this first before using it, the compiler has every right to complain.
    Last edited by Fubarable; 02-09-2010 at 04:42 AM.

  9. #9
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Sorry, I left out a small part of the code. This was actually immediately after the original code I had highlighted in blue.

    Java Code:
            String[] [B][COLOR="Red"]playersInLobby[/COLOR][/B] = {"Player1", "Player2", "Player3", "Player4", "Player5"};
            JList lobbyPlayersInLobbyList = new JList([COLOR="Red"][B]playersInLobby[/B][/COLOR]);
            lobbyPlayersInLobbyList.setLayout(null);
            lobbyPlayersInLobbyList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            lobbyPlayersInLobbyList.setBackground(new Color(221, 215, 173));
            lobbyPlayersInLobbyList.setPreferredSize(new Dimension(140, 438));
            size = lobbyPlayersInLobbyList.getPreferredSize();
            lobbyPlayersInLobbyList.setBounds(10,30, size.width, size.height);
            lobbyPlayersInLobbyList.setFont(new Font("Tahoma", Font.PLAIN, 16));
            lobbyPlayersInLobbyList.setForeground(Color.black);
            lobbyPlayersInLobbyPanelLeft.add(lobbyPlayersInLobbyList);
    I accidentally omitted it when I was copying and pasting the code. I have a lot of commented code that includes failed attempts and/or non-relevant coding. If I pasted the full code, it would have taken up about 3-4 times as much space and was just trying to help out.

    You were correct, of course. The variable I highlighted in Red was the issue. I had a typo in the variable. It should have been lobbyPlayersInLobby instead of playersInLobby.

    I also had to move the line...

    Java Code:
    JList lobbyPlayersInLobbyList = new JList(playersInLobby);
    ...to the beginning of the method to declare it. I assume there was still a problem with it farther down in the method because it was trying to refer to a variable which was never declared/instantiated?

    It now sits at the top of the method and the code works fine.

    Java Code:
        private static void createAndShowLobbyGUI() {
                    //Create and set up the window.
            JFrame lobbyFrame = new JFrame("Welcome to ...");
            lobbyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lobbyFrame.setLayout(null);
            lobbyFrame.setSize(1280, 768);
            lobbyFrame.setVisible(true);
            final JLabel lobbyOpponentNameLabel = new JLabel("Opponent Name", JLabel.CENTER);

Similar Threads

  1. jTextField Listeners
    By user6123456 in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2009, 05:58 PM
  2. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM
  3. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 10:06 AM
  4. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04: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
  •