I'm working on a database management system GUI that creates comboboxes dynamically based on the number of teams competing directly; if there are two teams it creates two comboboxes, three teams it creates three boxes, so on and so forth. Inside of the forloop that initializes and places the comboboxes I add an ActionListener to each of them. Within my nested class that implements ActionListener I have a series of if, else if statements that checks to see the source of the Action. Do I need to create a separate check for each possible instance, or is there a way to make it more dynamic? Basically I want to populate the first combobox with a list of options, once an option has been selected I want it to be removed from the list of options for subsequent comboboxes.
else if(e.getSource()==TournamentMatchupCreatebtn)
{
String[] MatchCreationOptions = {"Create Match","Cancel"};
JPanel MatchCreationpnl = new JPanel();
MatchCreationpnl.setLayout(null);
Object[] NumofTeamsARGS = {TournamentRestbl.getValueAt(TournamentRestbl.getSelectedRow(), 0).toString()};
int NumofTeams = Integer.parseInt(dbUpdater.getData(TournamentMatchupHDR,TeamCountqry, NumofTeamsARGS).getValueAt(0, 0).toString());
System.out.println(NumofTeams);
String LabelFlag = dbUpdater.getData(MatchTypeHDR,MatchTypeqry,NumofTeamsARGS).getValueAt(0, 0).toString();
System.out.println(LabelFlag);
String LabelText;
if(LabelFlag.equals("Teams"))
{
LabelText = "Team:";
}
else
{
LabelText = "Player:";
}
ArrayList<String> PlayersList = new ArrayList<String>();
DefaultComboBoxModel dcmb = new DefaultComboBoxModel();
dcmb = dbUpdater.getSpecificValues(MatchTypeqry, NumofTeamsARGS);
for(int i = 0; i<dcmb.getSize();i++)
{
PlayersList.add(dcmb.getElementAt(i).toString());
}
JLabel[] TeamPlayerlbl = new JLabel[NumofTeams];
TeamPlayercmbx = new JComboBox[NumofTeams];
JLabel[] VSlbl = new JLabel[NumofTeams];
for(int i = 0;i<NumofTeams;i++)
{
TeamPlayerlbl[i] = new JLabel(LabelText);
if(i==0)
{
TeamPlayercmbx[i] = new JComboBox(PlayersList.toArray());
}
else
{
TeamPlayercmbx[i]= new JComboBox();
}
VSlbl[i] = new JLabel("VS");
TeamPlayerlbl[i].setSize(TeamPlayerlbl[i].getPreferredSize());
addComponent(MatchCreationpnl,TeamPlayerlbl[i], 5,i*50+5);
addComponent(MatchCreationpnl,TeamPlayercmbx[i],TeamPlayerlbl[i],i*50+5,150,20,5);
VSlbl[i].setSize(VSlbl[i].getPreferredSize());
if(i<NumofTeams-1)
{
addComponent(MatchCreationpnl,VSlbl[i],100,i*50+30);
}
TeamPlayercmbx[i].setEditable(false);
TeamPlayercmbx[i].addActionListener(action);
}
TeamPlayercmbx[0].setEditable(true);
TeamPlayercmbx[0].setModel(MatchTypes);
MatchCreationpnl.setPreferredSize(new Dimension(200,NumofTeams*50+5));
int x = JOptionPane.showOptionDialog(null,MatchCreationpnl,"Create Tournament Match", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,MatchCreationOptions,null);
}
That code creates the interface for creating a match, I know I shouldn't be setting pixel specific locations for these things, in the next version I make I'll work on making the interface more flexible to fit the screen it's being displayed on.
else if(e.getSource()==TeamPlayercmbx[0])
{
ArrayList <String> Competitors = new ArrayList();
for(int i=0;i<TeamPlayercmbx[0].getItemCount();i++)
{
Competitors.add(TeamPlayercmbx[0].getItemAt(i).toString());
}
DefaultComboBoxModel DCMB = new DefaultComboBoxModel(Competitors.toArray());
TeamPlayercmbx[1].setEditable(true);
TeamPlayercmbx[1].setModel(DCMB);
TeamPlayercmbx[1].removeItem(TeamPlayercmbx[0].getSelectedItem());
}
That code removes the selected item from the model and allows you to select a value for the second combobox. What I need to know is if there's a way to replace
else if(e.getSource()==TeamPlayercmbx[0]
with something along the lines of
else if(e.getSource()==TeamPlayercmbx[some variable]
or if I need to just hard code this out
So I managed to solve this on my own using a simple for loop:
for(int x =0;x<TeamPlayercmbx.length;x++)
{
if(e.getSource()==TeamPlayercmbx[x])
{
try
{
ArrayList <String> Competitors = new ArrayList();
for(int i=0;i<TeamPlayercmbx[x].getItemCount();i++)
{
Competitors.add(TeamPlayercmbx[x].getItemAt(i).toString());
}
DefaultComboBoxModel DCMB = new
DefaultComboBoxModel(Competitors.toArray());
TeamPlayercmbx[x+1].setEditable(true);
TeamPlayercmbx[x+1].setModel(DCMB);
TeamPlayercmbx[x+1].removeItem(TeamPlayercmbx[x].getSelectedItem());
}
catch(Exception ex)
{
}
}
}
It seems pretty obvious after the fact. Hopefully the answer helps someone else though.