Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to select the only item in JComboBox?

Hi guys,

I have a Java project for school that requires me to create a GUI that allows the user to add sport players and the program will keep track of the player. The GUI contains a JComboBox and three JTextFields. The text fields are for the player's first name, last name, and uniform number. One requirement of the project is that the player's name appear in the JComboBox when a player is added. Another requirement is that when the user selects a player from the combo box, the player's first name, last name, and uniform number should show up in the text fields.

The problem I'm having is when I only have one player in the JComboBox. When the JComboBox is empty and I add a player, the JComboBox automatically selects the player. Because of that, I can't get the program to show the player's name and number when I select them. I kind of got around this by using a MouseListener, but it only works when I click the text field part of the JComboBox. The MouseListener doesn't work when I click on the arrow button of JComboBox.

Is there a way to fix this problem? Any help is appreciated. Thanks!

-Falcone

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class TranDavidAssign4
{
public static void main(String[] args)
{
JFrame frame = new TeamRosterFrame();
frame.setVisible(true);
}
}

class TeamRosterFrame extends JFrame
{
public TeamRosterFrame()
{
setTitle("Team Roster");
setBounds(250,250,400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();
JPanel panel = new TeamRosterPanel();
contentPane.add(panel);
}
}

class TeamRosterPanel extends JPanel implements ActionListener, MouseListener
{
private JButton addButton;
private JButton acceptButton;
private JButton deleteButton;
private JButton exitButton;
private JComboBox selectPlayerBox;
private JLabel selectPlayerLabel;
private JLabel firstNameLabel;
private JLabel lastNameLabel;
private JLabel numberLabel;
private JTextField firstNameTextField;
private JTextField lastNameTextField;
private JTextField numberTextField;
private ArrayList<Player> playerList = new ArrayList<Player>(40);

public TeamRosterPanel()
{
// Display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(4,2));

//Select player label
selectPlayerLabel = new JLabel("Select Player:");
displayPanel.add(selectPlayerLabel);

// Select player combo box
selectPlayerBox = new JComboBox();
selectPlayerBox.setMaximumRowCount(5);
selectPlayerBox.addItemListener(this);
selectPlayerBox.addActionListener(this);
selectPlayerBox.addMouseListener(this);
displayPanel.add(selectPlayerBox);

// First name label
firstNameLabel = new JLabel("First Name");
displayPanel.add(firstNameLabel);

// First name text field
firstNameTextField = new JTextField(10);
firstNameTextField.setHorizontalAlignment(JTextField.TRAILING);
firstNameTextField.setEnabled(false);
displayPanel.add(firstNameTextField);

// Last name label
lastNameLabel = new JLabel("Last Name");
displayPanel.add(lastNameLabel);

// Last name text field
lastNameTextField = new JTextField(10);
lastNameTextField.setHorizontalAlignment(JTextField.TRAILING);
lastNameTextField.setEnabled(false);
displayPanel.add(lastNameTextField);

// Number label
numberLabel = new JLabel("Number");
displayPanel.add(numberLabel);

// Number text field
numberTextField = new JTextField(10);
numberTextField.setHorizontalAlignment(JTextField.TRAILING);
numberTextField.setEnabled(false);
displayPanel.add(numberTextField);

// Button panel
JPanel buttonPanel = new JPanel();

// Add button
addButton = new JButton("add");
addButton.addActionListener(this);
buttonPanel.add(addButton);

// Accept button
acceptButton = new JButton("accept");
acceptButton.addActionListener(this);
buttonPanel.add(acceptButton);

// Delete button
deleteButton = new JButton("delete");
deleteButton.addActionListener(this);
buttonPanel.add(deleteButton);

// Exit button
exitButton = new JButton("exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);

this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == selectPlayerBox)
{
// Obtain player selected in combo box
String player = (String) selectPlayerBox.getSelectedItem();


String[] playersData = player.split(" ");

// Put player's data into text fields
firstNameTextField.setText(playersData[0]);
lastNameTextField.setText(playersData[1]);
numberTextField.setText(playersData[3]);
}
else if(e.getSource() == addButton)
{
// Clear the text fields
firstNameTextField.setText("");
lastNameTextField.setText("");
numberTextField.setText("");

// Enable all text fields
firstNameTextField.setEnabled(true);
lastNameTextField.setEnabled(true);
numberTextField.setEnabled(true);
}
else if(e.getSource() == acceptButton)
{
boolean validFirstName = false;
boolean validLastName = false;
boolean validNumber = false;

if(SwingValidator.isPresent(firstNameTextField, "First Name") &&
SwingValidator.isString(firstNameTextField, "First Name"))
{
validFirstName = true;
}

if(SwingValidator.isPresent(lastNameTextField, "Last Name") &&
SwingValidator.isString(lastNameTextField, "Last Name"))
{
validLastName = true;
}

if(SwingValidator.isPresent(numberTextField, "Number") &&
SwingValidator.isInteger(numberTextField, "Number"))
{
validNumber = true;
}

if(validFirstName && validLastName && validNumber)
{
String fName;
String lName;
String number;

fName = firstNameTextField.getText();
lName = lastNameTextField.getText();
number = numberTextField.getText();

Player newPlayer = new Player();

// Assign name and number to player object
newPlayer.setFirstName(firstNameTextField.getText());
newPlayer.setLastName(lastNameTextField.getText());
newPlayer.setNumber(Integer.parseInt(numberTextField.getText()));

playerList.add(newPlayer);

// Add player's data to combo box
selectPlayerBox.addItem(firstNameTextField.getText() + " " +
lastNameTextField.getText() + " - " +
numberTextField.getText());

selectPlayerBox.setSelectedIndex((selectPlayerBox.getItemCount() - 1));

// Clear the text fields
firstNameTextField.setText("");
lastNameTextField.setText("");
numberTextField.setText("");

// Disable all text fields
firstNameTextField.setEnabled(false);
lastNameTextField.setEnabled(false);
numberTextField.setEnabled(false);
}
}
else if(e.getSource() == deleteButton)
{
try
{
if(selectPlayerBox.getItemCount() == 1)
{
selectPlayerBox.removeAllItems();
playerList.clear();
}
else if(selectPlayerBox.getItemCount() > 1)
{
// Obtain index of player in combo box
int index = selectPlayerBox.getSelectedIndex();

// Remove player from combo box
selectPlayerBox.removeItemAt(index);

// Remove player from Player ArrayList
playerList.remove(index);
}
}
catch(NullPointerException npe)
{
// Error occurs because combo box is empty
// after last player is deleted
}
finally
{
// Clear the text fields
firstNameTextField.setText("");
lastNameTextField.setText("");
numberTextField.setText("");

// Disable all text fields
firstNameTextField.setEnabled(false);
lastNameTextField.setEnabled(false);
numberTextField.setEnabled(false);
}
}
else if(e.getSource() == exitButton)
{
// Exit program
System.exit(0);
}
}

public void mouseReleased(MouseEvent e)
{
if(selectPlayerBox.getItemCount() >= 1)
{
// Obtain player selected in combo box
String player = (String) selectPlayerBox.getSelectedItem();


String[] playersData = player.split(" ");

// Put player's data into text fields
firstNameTextField.setText(playersData[0]);
lastNameTextField.setText(playersData[1]);
numberTextField.setText(playersData[3]);
}
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}
[10489 byte] By [Falcone] at [2007-11-11 11:56:49]