I'm trying to find an easier way to populate alot of ComboBoxes.
Author |
Message |
TokenHerbz
|
Posted: Sun Sep 30, 2012 12:18 am Post subject: I'm trying to find an easier way to populate alot of ComboBoxes. |
|
|
I have this working when i write them all out and giving them all different names, However i wanted to try to use an array so that it saves alot of time.
However i'm unable to figure out how to get this to work. Heres some code.
the commented out huge blocks work, but as you can see its alot of repeatingness, so i wanted to use an array (up at the top) to eliminate that issue. thanks for helping
Java: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel {
protected JPanel topLeftPanel, topMidPanel, topRightPanel,
botLeftPanel, botMidPanel, botRightPanel,
midLeftPanel, midMidPanel, midRightPanel;
protected JPanel panel [];
protected JComboBox TLChoice, TMChoice, TRChoice,
MLChoice, MMChoice, MRChoice,
BLChoice, BMChoice, BRChoice;
protected JComboBox choice [];
protected String choiceOptions [] = {"", "X", "O"};
public GamePanel () {
setLayout (new GridLayout(3, 3)); //game panels gid layout
panel [9] = new JPanel();
choice [9] = new JComboBox(choiceOptions );
for (int i = 0; i < 9; i++ ) {
panel [i ]. setLayout(new FlowLayout());
panel [i ]. add(choice [i ]);
add (panel [i ]);
}
/** //need a ComboBox for each panel
TLChoice = new JComboBox(choiceOptions);
TMChoice = new JComboBox(choiceOptions);
TRChoice = new JComboBox(choiceOptions);
MLChoice = new JComboBox(choiceOptions);
MMChoice = new JComboBox(choiceOptions);
MRChoice = new JComboBox(choiceOptions);
BLChoice = new JComboBox(choiceOptions);
BMChoice = new JComboBox(choiceOptions);
BRChoice = new JComboBox(choiceOptions);
*/
/** TOP ROW panels for gamePanel*/
/** topLeftPanel = new JPanel();
topMidPanel = new JPanel();
topRightPanel = new JPanel();
//flow layouts
topLeftPanel.setLayout(new FlowLayout());
topMidPanel.setLayout(new FlowLayout());
topRightPanel.setLayout(new FlowLayout());
//add the choice to each top row panels
topLeftPanel.add(TLChoice);
topMidPanel.add(TMChoice);
topRightPanel.add(TRChoice);
*/
/** MID ROW panels for gamePanel*/
/** midLeftPanel = new JPanel();
midMidPanel = new JPanel();
midRightPanel = new JPanel();
//flow layouts
midLeftPanel.setLayout(new FlowLayout());
midMidPanel.setLayout(new FlowLayout());
midRightPanel.setLayout(new FlowLayout());
//add the choice to each top row panels
midLeftPanel.add(MLChoice);
midMidPanel.add(MMChoice);
midRightPanel.add(MRChoice);
*/
/** BOT ROW panels for gamePanel*/
/** botLeftPanel = new JPanel();
botMidPanel = new JPanel();
botRightPanel = new JPanel();
//flow layouts
botLeftPanel.setLayout(new FlowLayout());
botMidPanel.setLayout(new FlowLayout());
botRightPanel.setLayout(new FlowLayout());
//add the choice to each top row panels
botLeftPanel.add(BLChoice);
botMidPanel.add(BMChoice);
botRightPanel.add(BRChoice);
//Have to add all the panels to the gamePanel now in order
add(topLeftPanel); add(topMidPanel); add(topRightPanel); //TOP
add(midLeftPanel); add(midMidPanel); add(midRightPanel); //MID
add(botLeftPanel); add(botMidPanel); add(botRightPanel); //BOT
*/
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
2goto1
|
Posted: Sun Sep 30, 2012 12:33 pm Post subject: RE:I\'m trying to find an easier way to populate alot of ComboBoxes. |
|
|
One issue looks like you're not assigning each panel[] and choice[] array element, and they arrays are not initially declared properly.
The Java tutorial covers arrays enough to be able to do what you're trying to do, see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html to get started. |
|
|
|
|
|
TokenHerbz
|
Posted: Sun Sep 30, 2012 4:48 pm Post subject: RE:I\'m trying to find an easier way to populate alot of ComboBoxes. |
|
|
I still can't get this working,
These don't error out but it doesn't look right
code: |
panel = new JPanel[9];
|
it doesn't use the () which i thought was required.
However even with that working it doesn't for the ComboBoxes, you cant do the following
code: |
choice = new JComboBox(choiceOptions)[9];
//but you it doesn't error using
choice = new JComboBox[9];
//but then how do you add choiceOptions to it later?
|
|
|
|
|
|
|
DemonWasp
|
Posted: Sun Sep 30, 2012 6:19 pm Post subject: RE:I\'m trying to find an easier way to populate alot of ComboBoxes. |
|
|
You're creating 10 objects, not 1.
Java: |
choice = new JComboBox[9]; // creates one object, the array choice[]
for ( int i = 0; i < choice.length; ++i ) {
choice[i] = new JComboBox ( choiceOptions ); // creates 9 separate JComboBox objects, one at a time, and assigns choice[i] elements to refer to them.
}
|
|
|
|
|
|
|
Beastinonyou
|
Posted: Sun Sep 30, 2012 6:38 pm Post subject: Re: I'm trying to find an easier way to populate alot of ComboBoxes. |
|
|
I think you're confused as to how to go about creating an array of objects.
Java: | // This declares a variable 'panels' that references an array for JPanel. Note: Declaring an array reference variable does not create an array
JPanel[] panels;
// This creates an array and assigns the address to the panels variable
panels = new JPanel[6];
// You can both declare and create the array in one statement, like so:
JPanel[] panels = new JPanel[6];
// This creates an instance of the JPanel object for each element in the panels array (therefore, 6 JPanel object references in the panels array).
for (int i = 0; i < panels. length; i++ ) {
panels [i ] = new JPanel();
}
|
|
|
|
|
|
|
TokenHerbz
|
Posted: Mon Oct 01, 2012 7:44 pm Post subject: RE:I\'m trying to find an easier way to populate alot of ComboBoxes. |
|
|
Haha awesome thank you.
I can't believe i got that all confused up...
...embarrassing |
|
|
|
|
|
|
|