I'm creating a simple Minesweeper game in Java. Size 9x9.
I create an array of JPanels and an array of buttons; I add each button to its respective JPanel. then i add the JPanels to the JFrame.
How do i distinguish between each button on the action event?
Here's some of my code:
int gridx = 9;
int gridy = 9;
JButton[] buttons = new JButton[gridx*gridy];
JPanel[] jpanels = new JPanel[gridx*gridy];
public Minesweeper(){
super("Minesweeper");
setLayout(new GridLayout(9,9));
JPanel panel = new JPanel();
int i = 0;
for(i = 0; i<gridx*gridy; i++){
jpanels[i] = new JPanel();
buttons[i] = new JButton();
buttons[i].addActionListener(buttonEvent);
jpanels[i].setLayout(new GridLayout(1,1));
jpanels[i].add(buttons[i]);
add(jpanels[i]);
}
//buttons[67].setEnabled(false);
setSize(300,300);
setVisible(true);
}
The only way i can think about doing this is adding text to the button like so:
buttons[i] = new JButton(i);
Then calling getActionCommand() but i dont want text to show up on the button. Any other ideas?
You can use AbstractButton#setActionCommand.
In your loop:
buttons[i].setActionCommand(i+"");
Then you'll get i back when you use getActionCommand
Note I did mention in a comment on another answer that I would create a new class Mine which extends JButton which I believe to be a better and more complete solution. This however gets the job done rather quickly.
Related
So i'm creating an array of buttons that is supposed to display and 8,8 grid, instead it displays very small buttons spreading across the window (31 buttons in a row for two rows then two more buttons on the third). If I replace:
gamePanel1.add(buttons[a][b]);
with:
frame.add(buttons[a][b]);
... it display correctly but when initialising the array, I have to resize the window to see the buttons as it does not fit to contents.
Here is the code to create the buttons:
contentPane.setLayout(new BorderLayout());
JPanel gamePanel1 = new JPanel();
buttons = new JButton[boardsize][boardsize];
mineBoard = new int[9][9];
for (int a = 0; a < boardsize; a++)
for (int b = 0; b < boardsize; b++) {
buttons[a][b] = new JButton("");
buttons[a][b].setBounds(30+gridsize*a,30+gridsize*b,gridsize,gridsize);
gamePanel1.add(buttons[a][b]);
buttons[a][b].addMouseListener(new MouseListener(a,b));
setx(a);
sety(b);
settried(false);
setmine(false);
}
contentPane.add(gamePanel1, BorderLayout.CENTER);
Can anyone tell me how I might fix this or show me how with this code I may use a different layout - i tried grid layout for the buttons but could not get it working at all.
First create a Panel as:
JPanel panel=new JPanel();
Then set the layout as
panel.setLayout(new GridLayout(8,8));
Then using a for loop create and add the buttons and the buttons will be displayed in eight by eight grid. Thanks.
I'm trying to make a GUI for that simulates elevators in a building (really to test threading/c-scan), but when generating the buttons for the elevator control panel and the buttons for each individual floor, I'm kind of stuck. I thinking about trying to generate a new pair of buttons for each floor, and generating a new control panel per elevator. Also there's the difficulty of having a variable amount of floors. Anyway my question what is this best way to go about doing this? Perhaps it's not necessary to generate new buttons for everything and just use one set and change what the actions do per floor/elevator? I'm asking because I'm not very familiar with GUIs. Thanks for the help
If all the elevators, and the control panel are the same, you can use a singular method and pass in the elevator or the control panel. CustomPanel extends JPanel and has a method foo.
public void createElevatorButtons(final CustomPanel panel) {
ArrayList<JButton> buttons = new ArrayList<>(); //arraylist of buttons we can keep track of
JPanel buttonPanel = new JPanel(); //the visible component
for(int i = 1; i <= numberOfFloors;i++) {
JButton button = new JButton(String.valueOf(i)); //creates buttons for floors 1 to max
buttons.add(button);
buttonPanel.add(button);
}
panel.add(buttonPanel);
//add the action listeners
for(JButton button : buttons) {
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton floor = (JButton) e.getSource();
int floorNumber = Integer.parseInt(floor.getText());
panel.foo(floorNumber); //we tell the elevator/panel/whatever to do something, you will have to extend JPanel to do foo
}
});
}
}
In this case that the number of floors is variable you can create an array of buttons:
JButton[] buttons = new JButton[MAX_NUMBER_OF_FLOORS];
Then when you determine the exact of number of floors at runtime, you can go to instantiate and add the buttons:
for(int i=0; i<numberOfFloors; i++) {
buttons[i] = new JButton();
controlPanel.add(buttons[i]);
}
Something like this should work.
Assign MAX_NUMBER_OF_FLOORS a big number like 100, there should be a possible limit given by the problem.
JButton buttonArray[][] = new JButton [6][7];
JPanel grid;
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));
slot = new ImageIcon ("gameboard.png");
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 7; ++j)
{
b1 = new JButton (slot);
buttonArray[i][j] = b1;
buttonArray[i][j].setContentAreaFilled (false);
buttonArray[i][j].setBorderPainted (false);
grid.add(buttonArray[i][j]);
}
}
I am getting a NullPointerException which points to the grid.setLayout (new GridLayout(6,7,0,0)); part and to the new GameBoard(); which is in the main method at the bottom.
I add grid panel t o another panel as well, together with other panels:
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add("North", panel1);
panel.add("Center",grid);
panel.add("South",panel2);
add(panel);
I did initialize grid and buttonArray[][] already. What am I missing?
The grid variable is null as it has never been assigned an object. You need to either give it a new something or pass in its value via a setter method or constructor parameter.
More important than the actual solution to your current problem, is the knowledge of how to debug most common NullPointerExceptions. When you encounter a NullPointerException, you should carefully check all the variables on the line that throws the exception, find out which one is null, and then track back in your program to find out why it's null when you though otherwise.
You didn't initialize the grid variable:
JPanel grid; // null since it wasn't initialized
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));
You should just create some object there:
JPanel grid = new JPanel();
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));
I am dynamically generating a list of name boxes, sliders and labels, and am trying to figure out how to access the values of the sliders and change the labels. Other posts suggest using an array, but I have no idea where to begin with that.
My code is as such:
public Tailoring(int n) {
/*initComponents();*/
JPanel containerPanel = new JPanel();
containerPanel.setLayout(new BoxLayout(containerPanel, BoxLayout.PAGE_AXIS));
this.add(containerPanel);
JLabel Title = new JLabel("Tailoring:");
containerPanel.add(Title);
for(int i = 0; i < n; i++){
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField NameBox = new JTextField("Guest " + (i+1));
JSlider TipSlider = new JSlider();
JLabel TipCost = new JLabel();
rowPanel.add(NameBox);
rowPanel.add(TipSlider);
rowPanel.add(TipCost);
containerPanel.add(rowPanel);
}
}
You can create a new class YourPanel which extends JPanel.
Instead of the statement
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
you can use
YourPanel rowPanel = new YourPanel(new FlowLayout(FlowLayout.LEFT));
Define textfield, slider and label as the properties of this YourPanel class.
Provide getters/setters for each field. Then use an array or ArrayList of YourPanel objects in your application. You will be able to reach the nth panel's label with a call like:
panels.get(n).getJLabel();
It appears that you'd like to change the value displayed in the JLabel when the associated JSlider is modified, right? The best way to associate pairs of objects in Java is with a Map structure:
Map<Component, JSlider> sliderToLabel = new HashMap<Component, JSlider>();
for (int i = 0; i < n; i++) {
// after your loop code
sliderToLabel.put(TipSlider, TipCost); // map the slider to its label
}
You will be able to get a reference to the JSlider in the code that listens for changes on that component.
JLabel updateLabel = sliderToLabel.get(targetedSlider);
updateLabel.setText("updated text");
Notes
As a matter of convention, variable names should begin with lower case letters
The event listener I alluded to should also be attached in the loop. See Writing Event ListenersOracle
I got a JScrollPane in which I want to place a list of radio buttons and labels. My problem is the panel doesn't scroll, I suppose it's because i didn't set a viewport, but how can I set it when I have to many components?
My code looks something like this:
JScrollPane panel = new JScrollPane();
JRadioButton myRadio;
JLabel myLabel;
for(int i = 0; i<100; i++){
myRadio = new JRadioButton();
myLabel = new JLabel("text");
panel.add(myRadio);
panel.add(myLabel);
}
Thanks.
It is better to put your buttons and labels in a wrapper JPanel and then drop that into a JScrollPane.
try this:
JPanel panel = new JPanel(new GridLayout(0,1));
JRadioButton myRadio;
for(int i = 0; i<100; i++){
myRadio = new JRadioButton("text" + i);
panel.add(myRadio);
}
JScrollPane scrollPane = new JScrollPane(panel);
be sure to look into ButtonGroup as well. ButtonGroups allow you to enforce the single selection constraint common to radio buttons.