Currently displays a GUI with an 8x8 grid of randomized colored buttons. The newButton is to reset the score display to 0 and reset the grid with a fresh like it does on startup after being clicked. I haven't been able to find many solutions other than that it's to do with the way Java displays it's buttons and the way layering works. Here are the 2 classes I'm using.
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Color;
public class ShinyButtonsApp extends JFrame implements ActionListener {
private static byte ROWS = 8;
public int useThis;
ShinyButtons shiny = new ShinyButtons();
public static ImageIcon[] icons = {new ImageIcon("RedButton.png"),
new ImageIcon("OrangeButton.png"),
new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"),
new ImageIcon("BlueButton.png"),
new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png")};
public ShinyButtonsApp(String title) {
super(title); // Set title of window
setDefaultCloseOperation(EXIT_ON_CLOSE); // allow window to close
setSize(578, 634); // Set size of window
setResizable(false);
getContentPane().setLayout(null);
JLabel aLabel = new JLabel("Score: ");
aLabel.setLocation(10, 570);
aLabel.setSize(80,30);
getContentPane().add(aLabel);
JTextField scoreField = new JTextField();
scoreField.setText(Integer.toString(shiny.score));
scoreField.setEditable(false);
scoreField.setHorizontalAlignment(JTextField.RIGHT);
scoreField.setLocation(60, 570);
scoreField.setSize(120,30);
scoreField.setBackground(Color.WHITE);
getContentPane().add(scoreField);
JButton newButton = new JButton("New Game");
newButton.addActionListener(this);
newButton.setLocation(348,570);
newButton.setSize(110,30);
getContentPane().add(newButton);
JButton quitButton = new JButton("Quit");
quitButton.setLocation(468,570);
quitButton.setSize(80,30);
getContentPane().add(quitButton);
resetButtons2();
}
public void actionPerformed(ActionEvent e) {
shiny.score = 0;
shiny.resetButtons();
resetButtons2();
}
public void resetButtons2() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
ImageIcon image1 = icons[(int)shiny.getButton(r,c)];
JButton button = new JButton(image1);
button.setLocation(10+(69*r),10+(69*c));
button.setSize(69,69);
button.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
getContentPane().add(button);
}
}
}
public static void main(String[] args) {
ShinyButtonsApp frame;
frame = new ShinyButtonsApp("Shiny Buttons"); // Create window
frame.setVisible(true); // Show window
}
}
and
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.Color;
public class ShinyButtons extends JPanel{
public static byte RED = 0;
public static byte ORANGE = 1;
public static byte YELLOW = 2;
public static byte GREEN = 3;
public static byte BLUE = 4;
public static byte LIGHT_GRAY = 5;
public static byte DARK_GRAY = 6;
public static byte ROWS = 8;
public byte[][] buttonTable;
public int score = 0;
public ShinyButtons() {
buttonTable = new byte[ROWS][ROWS];
resetButtons();
}
public void resetButtons() {
for (int r=0; r<ROWS; r++)
for (int c=0; c<ROWS; c++)
buttonTable[r][c] = (byte)(Math.random()*7);
}
public byte getButton(int r, int c) { return buttonTable[r][c]; }
public int getScore() { return score; }
}
Building on to what this answer points out (using layout managers instead of setting size, as you should be doing) you could reset the the images just by looping through the components (JLabels) of the JPanel and changing their icons.
This particular example uses JLabels with MouseListeners but it could easily be switched to JButtons with ActionListeners
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
reset(iconPanel, icons); <--- call reset method from below
score = 0;
scoreField.setText(String.valueOf(score));
}
});
....
private void reset(JPanel panel, ImageIcon[] icons) {
Component[] comps = panel.getComponents();
Random random = new Random();
for(Component c : comps) {
if (c instanceof JLabel) {
JLabel button = (JLabel)c;
int index = random.nextInt(icons.length);
button.setIcon(icons[index]);
}
}
}
Here's the complete running code. You just need to replace the image paths.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class CircleImages {
private int score = 0;
private JTextField scoreField = new JTextField(10);
public CircleImages() {
scoreField.setEditable(false);
final ImageIcon[] icons = createImageIcons();
final JPanel iconPanel = createPanel(icons, 8);
JPanel bottomLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
bottomLeftPanel.add(new JLabel("Score: "));
bottomLeftPanel.add(scoreField);
JPanel bottomRightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JButton newGame = new JButton("New Game");
bottomRightPanel.add(newGame);
JButton quit = new JButton("Quit");
bottomRightPanel.add(quit);
JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
bottomPanel.add(bottomLeftPanel);
bottomPanel.add(bottomRightPanel);
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
reset(iconPanel, icons);
score = 0;
scoreField.setText(String.valueOf(score));
}
});
JFrame frame = new JFrame();
frame.add(iconPanel);
frame.add(bottomPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void reset(JPanel panel, ImageIcon[] icons) {
Component[] comps = panel.getComponents();
Random random = new Random();
for(Component c : comps) {
if (c instanceof JLabel) {
JLabel button = (JLabel)c;
int index = random.nextInt(icons.length);
button.setIcon(icons[index]);
}
}
}
private JPanel createPanel(ImageIcon[] icons, int gridSize) {
Random random = new Random();
JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));
for (int i = 0; i < gridSize * gridSize; i++) {
int index = random.nextInt(icons.length);
JLabel label = new JLabel(icons[index]);
label.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
score += 1;
scoreField.setText(String.valueOf(score));
}
});
label.setBorder(new LineBorder(Color.GRAY, 2));
panel.add(label);
}
return panel;
}
private ImageIcon[] createImageIcons() {
String[] files = {"blackcircle.png",
"bluecircle.png",
"greencircle.png",
"greycircle.png",
"orangecircle.png",
"redcircle.png",
"yellowcircle.png"
};
ImageIcon[] icons = new ImageIcon[files.length];
for (int i = 0; i < files.length; i++) {
icons[i] = new ImageIcon(getClass().getResource("/circles/" + files[i]));
}
return icons;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CircleImages();
}
});
}
}
You are creating a new set of buttons in resetButtons2() and placing them on top (or rather under) of the already existing set of buttons in the same locations. You should create the set of buttons once and only update their icons upon reset.
You should do a number of things:
Use a proper layout manager, e.g., GridLayout
Create the 8x8 grid of buttons only once and replace their icons when needed
Call invalidate/repaint to refresh the content pane
And for a JFrame you don't need getContentPane().add(...), you can directly do add(...)
Two things.
First, make sure you remove the existing buttons first. Alternatively, you could simply update the state of the buttons. This would require you to create an array or List of buttons first, which your reset method would then iterate over and update their properties as required.
Second, make use of an appropriate layout manager. A null layout ias a very bad idea. You do not control the font metrics of the underlying platform and this will change how your buttons look on different systems, making your UI less dynamic then it should be
Related
I am trying to make minesweeper. When I click on the JButton I want the button to hide. I have accomplished this but the button does not cover the entire JPanel. It always leaves some of the panel on the top visible.
I have tried setting the preferred size, vertical and horizontal alignment.
Why is it doing this? How can I fix it?
class canvas extends JFrame {
public static JPanel[] panels;
public static Tile[] tiles;
canvas(int size){
JFrame theGUI = new JFrame();
panels = new JPanel[size*size]; //creates panels and tiles, the tiles contain color, if it is
a bomb ect.
tiles = new Tile[size*size];
Container con = theGUI.getContentPane();
Graphics g;
for(int i = 0; i < panels.length; i++) {
JPanel temp = new JPanel();
temp.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));//adds borders
temp.setBorder(BorderFactory.createLineBorder(Color.black,1));
JButton button = new JButton();
button.setBackground(Color.GRAY);
button.setAlignmentY(0.0f);//tried to see if vertical align would fix it, it didnt
button.setPreferredSize(new Dimension(70,70));
button.addActionListener(new Action() {
public void actionPerformed(ActionEvent event) {
button.setVisible(false);
}
});
temp.add(button);
Tile tempTile = new Tile();
panels[i] = temp;
tiles[i] = tempTile;
tiles[i].color = Color.green;
panels[i].setBackground(tiles[i].color);
con.add(panels[i]);
}
con.setLayout(new GridLayout(size,size));
theGUI.setTitle("mine sweeper");
theGUI.setSize(size*40, size*40);
theGUI.setVisible(true);
}
}
The problem I am trying to fix:
I think you can achieve your goal with simpler code. You just need to add the JButtons directly to a JPanel that uses GridLayout as its layout manager. Consider the following code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class MineSweeper implements ActionListener, Runnable {
private JFrame frame;
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj instanceof JButton) {
JButton button = (JButton) obj;
button.setVisible(false);
}
}
public void run() {
showGui();
}
private JButton createButton() {
JButton button = new JButton();
button.setPreferredSize(new Dimension(40, 40));
button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
button.addActionListener(this);
return button;
}
private JPanel createMineField() {
JPanel mineField = new JPanel(new GridLayout(10, 10));
mineField.setBackground(Color.GREEN);
for (int i = 0; i < 100; i++) {
mineField.add(createButton());
}
return mineField;
}
private void showGui() {
frame = new JFrame("Mine Sweeper");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createMineField(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
/**
* Start here.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new MineSweeper());
}
}
I'm trying to create a jigsaw puzzle of sorts, where I have loaded an image onto a 5x5 grid, cropped the image and assigned each cropped part as an icon to 25 buttons arranged in the same pattern. I want to be able to drag my mouse pointer from a button to another and swap the icons on those 2 buttons.
I've tried using multiple MouseListener methods and MouseMotionListener methods, but nothing has worked so far.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.Component;
import javax.swing.*;
//import java.awt.image.*;
public class ImageMove {
JFrame jf;
JButton[][] grid;
JLabel test;
public static void main(String[] args) {
ImageMove ob = new ImageMove();
ob.start();
}
public void start() {
jf = new JFrame();
JPanel gridPanel = new JPanel();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("download.jpg");
Image temp1= img.getImage();
img=new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
Image img1 = img.getImage();
gridPanel.setLayout(new GridLayout (5,5));
grid = new JButton[5][5];
for(int y=0;y<5;y++) {
for(int x=0; x<5; x++) {
Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
ImageIcon icon = new ImageIcon(image);
JButton temp = new JButton(icon);
temp.setTransferHandler(new TransferHandler("icon"));
temp.addMouseMotionListener(new DragMouseAdapter());
grid[x][y]=temp;
gridPanel.add(grid[x][y]);
}
}
test= new JLabel();
jf.getContentPane().add(BorderLayout.NORTH, test);
jf.add(gridPanel);
jf.pack();
jf.setSize(500, 500);
jf.setVisible(true);
}
class DragMouseAdapter extends MouseAdapter{
private int x, y;
public void mouseDragged(MouseEvent e) {
final int x0=MouseInfo.getPointerInfo().getLocation().x;
final int y0=MouseInfo.getPointerInfo().getLocation().y;
x=x0;
y=y0;
JButton c = (JButton) e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
public void mouseReleased(MouseEvent e) {
JButton c = (JButton) e.getSource();
Icon icon = c.getIcon();
grid[((int)(x/100))][((int)(y/100))].setIcon(icon);
}
}
}
Currently, the program copies the icon from the first button to the second button, i.e. it replaces the second button's icon with the first, but the first button remains the same. I expect to swap those two icons entirely. The MouseDragged method towards the end is performing the described behavior, but the MouseReleased doesn't seem to do anything at all.
Any help is much appreciated.
It's very easy. You simply need to save somewhere the drag source button, and on drop replace its icon with the old icon of drop target button.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
//import java.awt.image.*;
public class ImageMove {
JFrame jf;
JButton[][] grid;
JLabel test;
JButton dragSource; // here we save drag source component
public static void main(String[] args) {
ImageMove ob = new ImageMove();
ob.start();
}
public void start() {
jf = new JFrame();
JPanel gridPanel = new JPanel();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("download.jpg");
Image temp1 = img.getImage();
img = new ImageIcon(temp1.getScaledInstance(500, 500, Image.SCALE_SMOOTH));
Image img1 = img.getImage();
gridPanel.setLayout(new GridLayout(5, 5));
grid = new JButton[5][5];
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
Image image = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(img1.getSource(), new CropImageFilter(x * 500 / 5, y * 500 / 5, 100, 100)));
ImageIcon icon = new ImageIcon(image);
JButton temp = new JButton(icon);
// use own extension of TransferHandler
temp.setTransferHandler(new MyTransferHandler("icon"));
// start drag on mouse pressed event.
temp.addMouseListener(new DragMouseAdapter());
grid[x][y] = temp;
gridPanel.add(grid[x][y]);
}
}
test = new JLabel();
jf.getContentPane().add(BorderLayout.NORTH, test);
jf.add(gridPanel);
jf.pack();
jf.setSize(500, 500);
jf.setVisible(true);
}
class DragMouseAdapter extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JButton c = (JButton) e.getSource();
dragSource = c;
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
private class MyTransferHandler extends TransferHandler {
public MyTransferHandler(String property) {
super(property);
}
#Override
public boolean importData(JComponent comp, Transferable t) {
Icon targetIcon = ((JButton) comp).getIcon();
boolean result = super.importData(comp, t);
if (dragSource != null) {
dragSource.setIcon(targetIcon);
dragSource = null;
}
return result;
}
}
}
I have a matrix of JToggleButton as components of a JPanel.
I want to call setSelected() for a group of those buttons
but I want the changes to be visually simultaneous.
How can I achieve this?
Consider the following code:
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class Main {
private static final JToggleButton buttons[][] = new JToggleButton[8][8];
public static void main(String args[]){
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JPanel panel = new JPanel();
panel.setLayout(new java.awt.GridLayout(8, 8, 0, 0));
//Initialize buttons and add them to panel
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j){
buttons[i][j] = new JToggleButton(){
public JToggleButton setRowAndColumn(int row, int column){
addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent evt){
onClick(row, column);
}
});
return this;
}
}.setRowAndColumn(i, j);
buttons[i][j].setPreferredSize(new java.awt.Dimension(40, 40));
panel.add(buttons[i][j]);
}
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
});
}
//This method is called when the JToggleButton at [row][column] is clicked
private static void onClick(int row, int column){
//In this example all other buttons at the same row or column
//as the clicked one, are also selected
for(int i = 0; i < 8; ++i){
buttons[i][column].setSelected(true);
buttons[row][i].setSelected(true);
//I don't want to see visual changes yet
}
//All changes should be visualized now
}
}
Basically I want onClick to be executed as a transaction (visually).
Thanks in advance.
This is my button click event. i want to create combo boxes each time i click the button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
List<JComboBox> listOfComboBoxes = new ArrayList<JComboBox>();
for(int i=1;i<4;i++){
int x = 28;
int y = 100;
int a = 145;
int b = 28;
listOfComboBoxes.get(i);
listOfComboBoxes.get(i).addItem("--Select the Teacher--");
listOfComboBoxes.get(i).setLayout(null);
listOfComboBoxes.get(i).setLocation(x,y);
add(listOfComboBoxes.get(i)).setSize(a,b);
x=x+30;
a=a+40;
i++;
}
}
});
You are coping with two issues: one is adding combos (say to a JPanel), and the second is laying out those combos.
Let's break it into two, and understand the adding mechanism first :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MakeCombos extends JFrame {
private static final int Number_OF_COMBOS = 4;
private JButton jButton1;
List<JComboBox> listOfComboBoxes;
private JPanel panel;
int counter = 0;
MakeCombos(){
super("Test frame");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400,300));
setLocationRelativeTo(null);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
//create a panel
panel = new JPanel();
getContentPane().add(panel);
//add button to it
jButton1 = new JButton("Click Me");
//add action listener to the panel
jButton1.addActionListener((ActionEvent e)-> {
jButton1ActionPerformed();
});
panel.add(jButton1, BorderLayout.CENTER);
validate();
pack();
setVisible(true);
listOfComboBoxes = makeCombos();
}
/**
*#return
*/
private List<JComboBox> makeCombos() {
List combos = new ArrayList<JComboBox>();
for(int i = 0; i < Number_OF_COMBOS; i++) {
JComboBox<String> combo = new JComboBox<>(new String[] {});
combos.add(combo);
}
return combos;
}
private void jButton1ActionPerformed() {
if(counter >= listOfComboBoxes.size()) {
return;
}
listOfComboBoxes.get(counter);
listOfComboBoxes.get(counter).addItem("--Select the Teacher--");
getContentPane().add(listOfComboBoxes.get(counter)) ;//.setSize(a,b);
counter++;
pack();
}
public static void main(String[] args) {
new MakeCombos();
}
}
Run it, and if it is not clear, don't hesitate to ask.
Now let's take it one step further and do the layout as well.
Please see comments:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MakeCombos extends JFrame {
private static final int Number_OF_COMBOS = 4;
private List<JComboBox<String>> listOfComboBoxes;
private JPanel combosPanel;
private int xPosition = 28;
private int yPosition = 100;
private int width = 145;
private int height = 28;
private int counter = 0;
MakeCombos(){
//make width frame for testing
super("Test frame");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500,400));
setLocationRelativeTo(null);
//set layout manager to the frame
getContentPane().setLayout(new BorderLayout());
//add panel + button to the frame
JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.NORTH); //add the panel to the frame
//add button to the button panel
JButton jButton1 = new JButton("Click Me");
//add action listener to the button
jButton1.addActionListener((ActionEvent e)-> {
jButton1ActionPerformed();
});
buttonPanel.add(jButton1, BorderLayout.CENTER);
//--add width panel for the combos
combosPanel = new JPanel();
//set layout manager to null so you can layout each combo
//consider using a layout manager instead
combosPanel.setLayout(null);
getContentPane().add(combosPanel, BorderLayout.CENTER); //add the panel to the frame
validate();
pack();
setVisible(true);
//make the combos and add them to width list
listOfComboBoxes = makeCombos();
}
/**
*
*/
private List<JComboBox<String>> makeCombos() {
List<JComboBox<String>> combos = new ArrayList<JComboBox<String>> ();
for(int i = 0; i < Number_OF_COMBOS; i++) {
JComboBox<String> combo = new JComboBox<>(new String[] {});
combos.add(combo);
}
return combos;
}
private void jButton1ActionPerformed() {
if(counter >= listOfComboBoxes.size()) {
return;
}
//////////////////////////////////////////////////////////////////////////////
//////////////// note : all this block could be moved to MakeCombos() ////////
// (adding content and setting bounds could have been done when creating ////
//the combo
//add content to the combo
listOfComboBoxes.get(counter).addItem("--Select the Teacher--");
//set layout bounds to each combo
listOfComboBoxes.get(counter).setBounds(xPosition, yPosition, width, height);
//increment position
xPosition=xPosition+30;
yPosition=yPosition+40;
////////////////////////////////////////////////////////////////////////////
/////////////////////// end of move-to-makeCombos() block //////////////////
////////////////////////////////////////////////////////////////////////////
//add the combo to the combos panel
combosPanel.add(listOfComboBoxes.get(counter)) ;
//increment position and counter
counter++;
repaint();
pack();
}
public static void main(String[] args) {
new MakeCombos();
}
}
Im using a JPanel to make a GUI for Game Of Life, my class extends JFrame and implements MouseListener, but when im clicking on a JLabel, the MouseListener works only once.
Any idea what to do?its not a problem in the code in my GameOfLife class because im also trying to print some string and it only works once.
Thanks!
here is the class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.Border;
public class GameOfLifeWindow extends JFrame implements ActionListener, MouseListener
{
private static final int DEF_ROWS = 16;
private static final int DEF_COLS = 16;
private JLabel[][] cells;
GameOfLife gol;
private JPanel panClear;
private JPanel panCenter;
private JPanel panNextGen;
private JButton nextGen;
private JButton clear;
private JMenuBar menuBar;
private JMenu file;
private JMenuItem newGame;
private JMenuItem loadFile;
public GameOfLifeWindow()
{
super("Game Of Life");
gol = new GameOfLife(DEF_ROWS, DEF_COLS);
//sets layout and dimension
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().setPreferredSize(new Dimension(300,400));
//Sets the panels
panClear = new JPanel(new FlowLayout());
panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
panNextGen = new JPanel(new FlowLayout());
//Adds the panel to the JFrame
this.getContentPane().add(panClear, BorderLayout.NORTH);
this.getContentPane().add(panCenter, BorderLayout.CENTER);
this.getContentPane().add(panNextGen, BorderLayout.SOUTH);
//Sets the next generation button
this.nextGen = new JButton("Next Generation");
panNextGen.add(this.nextGen);
this.nextGen.addActionListener(this);
//Sets the clear button
ImageIcon btnIcon = new ImageIcon(getClass().getResource("images/clear.png"));
Image tmpImg = btnIcon.getImage();
BufferedImage bi = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(tmpImg, 0, 0, 30, 30, null);
this.clear = new JButton(new ImageIcon(bi));
this.clear.addActionListener(this);
panClear.add(clear);
//Set the GridLayout
this.updateGeneration();
//Set the mouse listener to each cell
for(int i = 0 ; i < this.cells.length ; i++)
for(int j = 0 ; j < this.cells[i].length ; j++)
this.cells[i][j].addMouseListener(this);
//Sets MenuBar
this.menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
//Sets the File menu
this.file = new JMenu("File");
menuBar.add(file);
//Sets the New Game in the file
this.newGame = new JMenuItem("New Game");
this.newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, InputEvent.CTRL_MASK));
this.newGame.addActionListener(this);
file.add(this.newGame);
//Sets the Load File in the file
this.loadFile = new JMenuItem("Load File");
this.loadFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, InputEvent.CTRL_MASK));
this.loadFile.addActionListener(this);
file.add(this.loadFile);
}
public void updateGeneration()
//updates the JLabels according to the Game Of Life Screen
{
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
this.getContentPane().remove(this.panCenter);
this.panCenter = new JPanel(new GridLayout(gol.getRows(), gol.getCols()));
this.cells = new JLabel[gol.getRows()][gol.getCols()];
this.panCenter.removeAll();
for(int i = 0 ; i < cells.length ; i++)
for(int j = 0 ; j < cells[i].length ; j++)
{
this.cells[i][j] = new JLabel();
this.cells[i][j] .setOpaque(true); // make the color visible
this.cells[i][j].setBorder(border);// sets borders
if(gol.isAlive(i, j))
this.cells[i][j].setBackground(Color.BLACK);
else
this.cells[i][j].setBackground(Color.white);
panCenter.add(this.cells[i][j]);
}
this.getContentPane().add(this.panCenter);
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.nextGen)
{
this.gol.nexGeneration();
this.updateGeneration();
}
else if(e.getSource() == this.clear)
{
this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
this.updateGeneration();
}
else if(e.getSource() == this.newGame)
{
this.gol = new GameOfLife(DEF_ROWS, DEF_COLS);
this.updateGeneration();
}
else if(e.getSource() == this.loadFile)
{
String fileName = JOptionPane.showInputDialog("Please Enter A File Name");
this.gol = new GameOfLife(fileName);
System.out.println(gol.toString());
this.updateGeneration();
}
}
#Override
public void mouseClicked(MouseEvent e)
{
boolean found = false;
for(int i = 0 ; i < cells.length && !found ; i++)
for(int j = 0 ; j < cells[i].length && !found; j++)
if(e.getSource() == this.cells[i][j])
{
this.gol.setForGUI(i, j);
found = true;
}
this.updateGeneration();
System.out.println("asdsad");
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
}
In your Constructor you assign a mouse listener to all of your cells which you have in a multidimensional array of JLabel. However, then in your updateGeneration() you create another JLabel[][] array but don't assign your mouse listener to cells again.
To fix you need to add a mouse listener to all the cells again in updateGeneration(). Or instead of creating a new JLabel[][] just update the state of the JLabel in the existing array.