0

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 );
}

}

  • "buttons to store a lot of data"? I'm confused. Why/how do you want to store data in buttons? – isnot2bad May 02 '15 at 17:13
  • Well the first button in the hierarchy will have x and y cords. One branch would store another string and say 4 ints. Then those ints interact with the ints of other buttons in equations. I want to print all this info into the toolTips panel when mouseEntered is executed. Also when I click on one button the info is in the toolTip panel and when I hover over another button the equations work and the toolTip panel will print the results of the two buttons being put together. Then clicking the second button will actually execute the equations. (toolTip panel is a preview of what will happen) – toxicswagger1 May 02 '15 at 17:23
  • [use put/getClientProperty](http://stackoverflow.com/a/10416286/714968) – mKorbel May 03 '15 at 08:15

1 Answers1

0

Not sure exactly what you want to do, but here's an example of a custom button class.

public class MyButton extends JButton{

  public final int x, int y;

  public MyButton(String text, int x, int y){
    super(text);

    this.x = x;
    this.y = y;

    addMouseListener(new MouseAdapter(){
      @Override
      public void mousePressed(MouseEvent e){
        System.out.println("The mouse was pressed!");
      }
    });
  }

}
satnam
  • 10,719
  • 5
  • 32
  • 42