0

I have added a jtoolbar to a jframe with a jbutton init for start. However the toolbar appears way too small, the button hardly appears, although the buttons size is bigger. How can I fix this? In the frame I have a jpanel with an image jlabel as background, then I add 2 buttons in the jlabel in the CENTER. Finally I added the jtoolbar in the PAGE_START of the jlabel.

enter image description here

public class bcquery extends JPanel implements ActionListener {


    static JFrame frame = new JFrame("BIOCORE - GAMS Tool");
    static JLabel labelimg;
    public bcquery() throws IOException {


        setLayout(new BorderLayout());

        File file = new File(".jpg");           

        labelimg = new JLabel(new ImageIcon(ImageIO.read(file)));
        labelimg.setPreferredSize(new Dimension(1000, 300));

        add(labelimg);

         labelimg.setLayout(new BorderLayout());


final JButton button1 = new JButton("OK");

final JButton button2 = new JButton("CLEAR");

JPanel btnpanel = new JPanel(new FlowLayout());
        btnpanel.add(button1);
        btnpanel.add(button2);

btnpanel.setOpaque(false);

labelimg.add(btnpanel, BorderLayout.CENTER);


public static void createToolBar() { 


        JToolBar toolbar = new JToolBar("Applications");
        JButton btn = new JButton(new ImageIcon(".jpg"));
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("test");

            }
        });

        toolbar.add(btn);
        labelimg.add(toolbar, BorderLayout.PAGE_START);

    }

    private static void createAndShowGUI() throws IOException {

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
            frame.setSize(500, 500);
            JFrame.setDefaultLookAndFeelDecorated(true);

            frame.add(new bcquery());
            frame.setResizable(false);

            frame.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args) {

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {

                UIManager.put("swing.boldMetal", Boolean.TRUE);


            try {
                createAndShowGUI();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                }
            });

    }
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2598911
  • 379
  • 2
  • 6
  • 22
  • Give some text to your button or use a proper image. The filename you specified is incorrect. – camickr Dec 03 '13 at 16:46
  • I think that I have to make my image be the button, but the way I placed is it like an icon. How do I make the icon be the whole button? @camickr – user2598911 Dec 03 '13 at 16:51
  • The Icon will be the whole button, except for the standard button Border. If you don't want the border then use `button.setBorderPainted(false)`. – camickr Dec 03 '13 at 16:53
  • I added the image that I am using for the button above. It wont appear in its proper size in the toolbar. @camickr – user2598911 Dec 03 '13 at 16:58
  • 1
    you can 1. use `OverlayLayout` put `JButton` over `JLabel`, 2. set `LayoutManager` to `JLabel`, add `JButton` to `JLabel`, 3. change `LayoutManager` for `JToolbar` (your issue `Icon` in `JButton` inside `JTollBar` a few times Q&A here), 4. override `minimum size` for `JButton` in `JToolbar` – mKorbel Dec 03 '13 at 17:05
  • 1
    For [example](http://stackoverflow.com/a/16121288/230513). – trashgod Dec 03 '13 at 17:39
  • my bad. the file was .png, and not .jpg that I wrote. Thanks everyone – user2598911 Dec 03 '13 at 17:55

0 Answers0