Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 05:58 PM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
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.

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();
            }
        });

    }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-08-2010, 06:33 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,388
Rep Power: 11
Fubarable is on a distinguished road
Default
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.

Quote:
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!

Quote:
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2010, 09:21 PM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
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.

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).

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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-08-2010, 09:41 PM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
Default
UGH! Ok, I just realized that the 2nd solution has an issue. It sometimes returns the wrong index for some reason. LOL
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-08-2010, 10:45 PM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
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.


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);

        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);
                    lobbyOpponentNameLabel.setText(name);
                    }
                }
            };
        lobbyPlayersInLobbyList.addListSelectionListener(listSelectionListener);

        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.

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-09-2010, 04:13 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,388
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by Psyclone View Post
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-09-2010, 04:18 AM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-09-2010, 04:24 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,388
Rep Power: 11
Fubarable is on a distinguished road
Default
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:
Code:
JLabel lobbyOpponentNameLabel = new JLabel("....");
or even simply
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-09-2010, 07:21 PM
Member
 
Join Date: Jan 2010
Posts: 81
Rep Power: 0
Psyclone is on a distinguished road
Default
Sorry, I left out a small part of the code. This was actually immediately after the original code I had highlighted in blue.

Code:
        String[] playersInLobby = {"Player1", "Player2", "Player3", "Player4", "Player5"};
        JList lobbyPlayersInLobbyList = new JList(playersInLobby);
        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...

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.

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);
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
jTextField Listeners user6123456 AWT / Swing 6 10-15-2009 05:58 PM
Seriously need help on my listeners!! themburu Java Applets 4 05-26-2008 10:41 AM
Multiple listeners per event Java Tip Java Tips 1 01-03-2008 10:06 AM
Action Event and Listeners lost1 New To Java 3 11-14-2007 04:26 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:01 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org