How come my JButtons ("search buttons") wont show up on the JPanel?

33 Views Asked by At

Im building a inventory Gui application and I want the items to be searched with the 1st letter of the description. So, I need the 26 small buttons (which will be letters of the alphabet) to show on the JPanel. Then use the search button to correspond to the saved files. But I cant seem to get the buttons to even show??? It just the JPanel. What I have here is just a snippet of the whole program. Just highlighting the JPanel and JButtons that are inside it.

JPanel searchPanel = new JPanel( );
searchPanel.setPreferredSize(new Dimension(240, 160));
searchPanel.setBorder(BorderFactory.createTitledBorder("Item Search")); 
searchPanel.setBounds(149, 295, 205, 94);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 7;
gridConstraints.gridwidth = 3;
gridConstraints.insets = new Insets(10, 0, 10, 0);
gridConstraints.anchor = GridBagConstraints.CENTER;
getContentPane().add(searchPanel, gridConstraints);
layeredPane.setLayer(searchPanel, 2);
layeredPane.add(searchPanel, gridConstraints); 

 
int x = 0, y = 0;
    // Create Button and Position of Search Panel
    for (int i = 0; i < 26; i++)
    {
        JButton[ ] searchBtn = new JButton[26];
        searchBtn[i] = new JButton( );
        searchBtn[i].setText(String.valueOf((char) (65 + i)));
        searchBtn[i].setFont(new Font("Arial", Font.BOLD, 12));
        searchBtn[i].setMargin(new Insets(-10, -10, -10, -10));
        searchBtn[i].setBounds(149, 295, i, i);
        sizeButton(searchBtn[i], new Dimension(37, 27));
        searchBtn[i].setBackground(Color.YELLOW);
        searchBtn[i].setFocusable(false);
        gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = x;
        gridConstraints.gridy = y;
        searchPanel.add(searchBtn[i], gridConstraints);
        //Search Buttons Method
        searchBtn[i].addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e)
            {
                searchBtnActionPerformed(e);
            }
            });
        x++;
        //6 Buttons Per Row
        if (x % 6 == 0)
        {
            x = 0;
            y++;
        }
        }
         
         getContentPane( ).setLayout(getLayout( ));
 }
1

There are 1 best solutions below

1
MadProgrammer On

So, I took your code, corrected for the fact that you're trying add it two different containers simultaneously and it produced...

enter image description here

Now, I'm pretty sure this isn't what you wanted, based on my limited understanding of your code (this is one of those moments where the code is in such a state that it's not even worth trying to salvage), but, it would seem that you clearly don't understand how to use layout managers.

You should make the time to look over Laying Out Components Within a Container

So, I threw out your code and simply did...

enter image description here

public class SearchPane extends JPanel {

    public SearchPane() {
        setBorder(BorderFactory.createTitledBorder("Item Search"));
        setLayout(new GridLayout(-1, 6));

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                JOptionPane.showMessageDialog(SearchPane.this, "You want to search for [" + cmd + "]", "Search", JOptionPane.PLAIN_MESSAGE);
            }
        };

        for (int index = 0; index < 26; index++) {
            JButton btn = new JButton(Character.toString('A' + index));
            btn.addActionListener(listener);
            add(btn);
        }
    }
}