So i'm entering some new territory in gui and I have a gui I can work with that prints a grid of buttons in a panel which centers in a frame. But I really need those buttons to store lots of data (which I think I can handle myself), and have MouseListeners (MouseEntered is important). Since I've never done something like this its hard for me to get a good class going (Eventually i'll have a hierarchy of types of buttons. Default/parent button holds a location, x and y, sub buttons will hold varying stats). I do not know how to start the button class and I do not know how use override Mouse Listener so any help would be appreciated.
Here is what I have already:
public class Board extends JFrame{
private static int lengthy;
private static int lengthx;
public Board(int x, int y){
lengthy = y;
lengthx = x;
}
public void printBoard(){
int x;
int y;
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(lengthy, lengthx));
for (int i = 0; i < lengthx*lengthy; i++) {
if(i<lengthx){
x = i+1;
}else x = i % lengthx+1;
if(i<lengthx){
y=1;
}else y = i/lengthx+1;
String xString = Integer.toString(x);
String yString = Methods.getChar(y);
JButton[][] buttons = new JButton[lengthx][lengthy];
buttons[x-1][y-1] = new JButton(xString+yString);
buttons[x-1][y-1].setPreferredSize(new Dimension(25, 25));
buttons[x-1][y-1].setBackground(Color.WHITE);
buttons[x-1][y-1].setText("");
buttons[x-1][y-1].setToolTipText(xString+yString);
pane.add(buttons[x-1][y-1]);
}
JPanel toolTips = new JPanel();
toolTips.setPreferredSize(new Dimension(200,500));
toolTips.setBackground(Color.WHITE);
JFrame frame = new JFrame("Oval Sample");
SpringLayout layout = new SpringLayout();
frame.setLayout(layout);
layout.putConstraint(SpringLayout.WEST, pane, (1280-lengthx*25)/2, SpringLayout.WEST, frame);
layout.putConstraint(SpringLayout.NORTH, pane, (720-lengthy*30)/3, SpringLayout.NORTH, frame);
layout.putConstraint(SpringLayout.WEST, toolTips, ((1280-lengthx*25)-200)/8, SpringLayout.WEST, frame);
layout.putConstraint(SpringLayout.NORTH, toolTips, (720-lengthy*30)/3, SpringLayout.NORTH, frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.add(toolTips);
frame.setSize(1280, 720);
frame.setVisible(true);
}
public class Methods {
//max 52
public static String getChar(int i) {
return i > 0 && i < 27 ? String.valueOf((char)(i + 96)) : (i > 26 && i < 53 ? String.valueOf((char)(i+64-26)): null );
}
}