Create dynamic JButton with Image and ActionListener - Java Swing
Dynamically create JButton with Image and ActionListener. You can change the height of the button , the width of the horizontal gap and the vertical gap in one place.
I have a dummy database class that will return main menu items and submenu items. You will see the main menu item in your JFrame. If you select the main item (FOOD) from the button bar, it will load Sub Items from the dummy database class (sub-items of FOOD).
you can find more information with images and source code here
private void addMainMenue() { pnl_button.removeAll(); repaint(); Image img, sub; ImageIcon icon; String imagePath, imag = "/com/images/"; ArrayList menue = new ArrayList(); ArrayList itemName = new ArrayList(); for (int size = 0; size < ItemDB.mainMenuCodes.length; size++) { menue.add(ItemDB.mainMenuCodes[size]); itemName.add(ItemDB.mainMenuDesc[size]); } JButton[] buttons = new JButton[menue.size()]; for (int i = 0; i < buttons.length; i++) { imagePath = imag + menue.get(i).toString() + ".jpeg"; URL url = getClass().getResource(imagePath); // System.out.println(imagePath +" Get Res : " // +getClass().getResource(imagePath)); if (url != null) { img = Toolkit.getDefaultToolkit().getImage(url); sub = img.getScaledInstance(button_width - 8, button_height - 30, Image.SCALE_FAST); icon = new ImageIcon(sub); } else icon = new ImageIcon(); buttons[i] = new JButton(itemName.get(i).toString(), icon); buttons[i].setVerticalTextPosition(AbstractButton.BOTTOM); buttons[i].setHorizontalTextPosition(AbstractButton.CENTER); buttons[i] .setBorder(javax.swing.BorderFactory.createEtchedBorder()); buttons[i].setFont(new java.awt.Font("Tahoma", 1, 13)); buttons[i].setForeground(new java.awt.Color(0, 51, 255)); buttons[i].setActionCommand(menue.get(i).toString()); buttons[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String choice = e.getActionCommand(); addSubmenue(choice); } }); } int b = 0; int vGap = verticalGap; int hGap = horizontalGap; int bLength = buttons.length; int bRows = bLength / numberOfColumns + 1; L1: for (int j = 0; j < bRows; j++) { vGap = 10; for (int k = 0; k < numberOfColumns; k++) { pnl_button.add(buttons[b], new org.netbeans.lib.awtextra.AbsoluteConstraints(vGap, hGap, button_width, button_height)); repaint(); vGap += button_width + verticalGap; b++; if (b >= bLength) { break L1; } } hGap += button_height + horizontalGap; } pack(); } private void addSubmenue(String choice) { pnl_button.removeAll(); repaint(); Image img, sub; ImageIcon icon; String imagePath, imag = "/com/images/"; ArrayList menue = new ArrayList(); ArrayList itemName = new ArrayList(); ArrayList list = ItemDB.getSubMenu(choice); String subCode[] = (String[]) list.get(0); String subDesc[] = (String[]) list.get(1); for (int size = 0; size < subCode.length; size++) { menue.add(subCode[size]); itemName.add(subDesc[size]); } JButton[] buttons = new JButton[menue.size()]; for (int i = 0; i < buttons.length; i++) { imagePath = imag + menue.get(i).toString() + ".jpeg"; URL url = getClass().getResource(imagePath); // System.out.println(imagePath +" Get Reso : " // +getClass().getResource(imagePath)); if (url != null) { img = Toolkit.getDefaultToolkit().getImage(url); sub = img.getScaledInstance(button_width - 8, button_height - 30, Image.SCALE_FAST); icon = new ImageIcon(sub); } else icon = new ImageIcon(); buttons[i] = new JButton(itemName.get(i).toString(), icon); buttons[i].setVerticalTextPosition(AbstractButton.BOTTOM); buttons[i].setHorizontalTextPosition(AbstractButton.CENTER); buttons[i] .setBorder(javax.swing.BorderFactory.createEtchedBorder()); buttons[i].setFont(new java.awt.Font("Tahoma", 1, 13)); buttons[i].setForeground(new java.awt.Color(0, 51, 255)); buttons[i].setActionCommand(menue.get(i).toString()); buttons[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String choice = e.getActionCommand(); addItems(choice); } }); } int b = 0; int vGap = verticalGap; int hGap = horizontalGap; int bLength = buttons.length; int bRows = bLength / numberOfColumns + 1; L1: for (int j = 0; j < bRows; j++) { vGap = 10; for (int k = 0; k < numberOfColumns; k++) { pnl_button.add(buttons[b], new org.netbeans.lib.awtextra.AbsoluteConstraints(vGap, hGap, button_width, button_height)); repaint(); vGap += button_width + verticalGap; b++; if (b >= bLength) { break L1; } } hGap += button_height + horizontalGap; } pack(); } private void addItems(String choice) { if (choice.equals("P")) choice = "PIZZA"; else if (choice.equals("B")) choice = "BURGER"; else if (choice.equals("FJ")) choice = "FRUIT JUICE"; else if (choice.equals("HB")) choice = "HOT BEVERAGES"; JOptionPane.showMessageDialog(this, "You have select " + choice); }
source share