Search code examples
javaswingjxbrowser

Problem with changing the size of a JButton within a JxBrowser JFrame


So i am using JxBrowser and have it added to the JFrame. Now I want to add like multiple buttons to the right side of the browser but it doensn't matter what I try (test1.setSize, or test1.setPreferredSize etc it doesn't change the size).

Picture of problem

The red rectangles in this pictures are examples of the size of the JButtons i want at the right side of the JFrame. Why does the JButton remain that big?

here's the code:

    public test() {
    test1 = new JButton("test");
    test1.addActionListener(this);
    browser = new Browser();
    view = new BrowserView(browser);

    JFrame frame = new JFrame("FOEBot - Gemaakt door Gerrit");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(view, BorderLayout.CENTER);
    frame.add(test1, BorderLayout.AFTER_LINE_ENDS);
    frame.setSize(1500, 1000);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    browser.loadURL("https://www.google.nl/");
}

Please help me out.


Solution

  • You should really have a look at the Layout documentation.

    Here you have an example how I solved your problem. I put the JButtons into another container which is used as a BoxLayout:

    public static void main(String[] args) {
        JPanel mainContainer = new JPanel();
        mainContainer.setLayout(new BorderLayout());
    
        JFrame jf = new JFrame();  
        jf.add(mainContainer);
    
        JPanel browser = new JPanel();
        JLabel browserDummy = new JLabel("Browser");
        browser.add(browserDummy);
        browserDummy.setFont(new Font("Arial", Font.BOLD, 200));
    
        JPanel buttonContainer = new JPanel();
        buttonContainer.setLayout(new BoxLayout(buttonContainer, BoxLayout.Y_AXIS));
        buttonContainer.add(new JButton("Button one"));
        buttonContainer.add(new JButton("Button two"));
    
        mainContainer.add(browser, BorderLayout.WEST);
        mainContainer.add(buttonContainer, BorderLayout.EAST);
    
        jf.setVisible(true);
        jf.pack();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    I never did anything with browser programming in Swing so I just used some dummies, but the principle should be the same.

    Result: enter image description here