Search code examples
javaswingburp

How to control size of JTextField in Burp Extension?


I'm developing Burp extension and add additional tab. I have to return java.awt.component, so i decided javax.swing.JPanel would be nice. It must be a JLabel and JTextField on my Tab, code here:

            JPanel panel = new JPanel();
            panel.setLayout(new BoxLayout(panel,Y_AXIS));

            JLabel label = new JLabel("hostname : ");
            panel.add(label);
            JTextField tf = new JTextField("text");
            panel.add(tfHost);

I wanted little text and textfield on top left, but my TextField stretched on all my screen. What do i have to do to fix it? Maybe i have to change layout manager?


Solution

  • The problem is a BoxLayout will allow components to grow to fill the available space to the panel.

    So the easiest solution is to add your panel to another panel that will respect the size of the BoxLayout panel.

    Something like:

    JPanel wrapper = new JPanel(); // uses FlowLayout by default.
    wrapper.add( panel );
    frame.add( wrapper );
    

    Now when you add the wrapper panel to the frame, the wrapper panel will grow in size, but it will not affect the components added to the wrapper panel.