javaswinguser-interfacejbuttonjtextarea

A problem of visibility in my custom User Interface


To create my GUI I created a number of subclasses of JPanel, each of those solving a specific layout problem.

The first is the AltBoxLayout_JPanel, inspired by the answer to a question I posed time ago (link: Setting the height of a JTextFrame): it simply is a couple of JPanels nestled one within the other, the inner one with a BoxLayout to distribute the components in the preferred way, the outer one with a BorderLayout to squeeze the components to their minimal dimension.

The second is the Fieldset, inspired by the HTML tag with the same name: again it is made by two nestled JPanels, the outer one with a TitleBorder, the inner one that is given in the constructor and whose purpose is to set up the layout inside the Fieldset zone.

I tested those two specialized components separately, with good results, and was satisfied by them. Recently I needed to use them both: I wanted a JButton and a JTextArea to be displayed one over the other without being artificially expanded (i.e. inside a AltBoxLayout_JPanel), and surrounded by a border (i.e. the AltBoxLayout_JPanel inside a Fieldset). Since all of my components were already tested, I was sure it would go well.

It didn't.

Somehow my JButton is not visible anymore. I know it is there (I checked) and I assume it is covered by the JTextArea, but I have no further information. Below is my code.

Any help in rescuing my JButton would be really appreciated!



public class StackOverflowQuestion extends JFrame {

    private static final long serialVersionUID = -5644282466098799568L;

    private JPanel contentPanel;
        

    // Display area
        
    private Fieldset fieldset;
    private JButton button;
    private JTextArea jTextArea;
        
    /**
     * Create the frame.
     */
    public StackOverflowQuestion() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 140);
        contentPanel = new JPanel();
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPanel.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPanel);
        
    // Testing new components
        
    //  JPanel fieldsetLayoutPanel = new JPanel();
    //  BoxLayout fieldsetLayout = new BoxLayout(fieldsetLayoutPanel, BoxLayout.PAGE_AXIS);
    //  fieldsetLayoutPanel.setLayout(fieldsetLayout);
        
        JPanel fieldsetLayoutPanel_2 = new AltBoxLayout_JPanel(BoxLayout.PAGE_AXIS);
        
        this.fieldset               = new Fieldset("Fieldset title", fieldsetLayoutPanel_2, null);
        this.button                 = new JButton("Button text");
        this.jTextArea              = new JTextArea("JTextArea here!");
        this.jTextArea.setRows(5);
        this.jTextArea.setColumns(20);
        this.button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Action executed: " + arg0.toString());
            }});
            
        fieldset.add(button);
        fieldset.add(jTextArea);
        contentPanel.add(fieldset);

        System.out.println("Analysis of the components of the contentPanel:");
        analyzeLayoutsOfComponentAndSubComponents(contentPanel, 0);
            
        pack();
            
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StackOverflowQuestion frame = new StackOverflowQuestion();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** A method that studies the {@link JPanel} and its sub-{@link JPanel}s, iterating through all the component tree.
     * 
     * @param c
     * @param tab
     */
    public static void analyzeLayoutsOfComponentAndSubComponents(JPanel c, int tab) {
        System.out.println(repeatChar('\t', tab) + c.toString());
        for(int i=0 ; i<c.getComponentCount() ; i++)
            if( c.getComponent(i) instanceof JPanel )
                analyzeLayoutsOfComponentAndSubComponents( (JPanel)c.getComponent(i), tab+1 );
    }
    
    public static String repeatChar(char c, int tab) {
        StringBuffer support = new StringBuffer();
        for(int i=0 ; i<tab ; i++)      support.append(c);
        return support.toString();
    }
    
        
        
    
    
    public class Fieldset extends JPanel {
        
        private static final long serialVersionUID = -1464052286624448783L;
        
        /** The {@link Border} of {@code this} {@link Fieldset}.                */
        private Border border;
        /** The {@link JPanel} responsible for the layout management.           */
        public JPanel layoutPanel;
            
        /** Creates a new {@link Fieldset}. 
         * 
         * @param colorBorder The {@link Color} of the border. If {@code null}, {@code Color.BLACK} is chosen.
         * 
         */
        public Fieldset(String titleFieldset, JPanel panel, Color colorBorder) {
            super();
            
            this.border = new TitledBorder(new LineBorder(colorBorder == null ? Color.BLACK : colorBorder), titleFieldset);
            this.setBorder(border);
            this.layoutPanel = panel;
            
            super.add(layoutPanel);
        }
        
        public Component add(Component component) {
            this.layoutPanel.add(component);
            return component;
        }
            
    }


    public class AltBoxLayout_JPanel extends JPanel {
        
        private static final long serialVersionUID = -8204033141468207723L;
        
        private JPanel layoutPanel;
        
        /** Creates a new {@link AltBoxLayout_JPanel}.
         * 
         * @param axisOrientation A constant from the {@link BoxLayout} class representing the chosen insertion axis.
         */
        public AltBoxLayout_JPanel(int axisOrientation) { 
            // ===== INNER PANEL =====
            this.layoutPanel = new JPanel(); //This is the nested panel
            layoutPanel.setLayout(new BoxLayout(layoutPanel, axisOrientation));
            
            // ===== OUTER PANEL =====
            this.setLayout(new BorderLayout());
            this.add(layoutPanel, BorderLayout.PAGE_START);
        
            System.out.println("Analysis of the components of the AltBoxLayout_JPanel:");
            analyzeLayoutsOfComponentAndSubComponents(this, 0);
        }
        public void add(JComponent component) {
            layoutPanel.add(component);
        }
    }

}


Solution

  • The layoutPanel in your Fieldset class seems to stack the components on top of each other because it's default layout tells it to. Just to get started, try setting a simple BoxLayout to it in the constructor of Fieldset:

    this.layoutPanel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    

    Your button will be visible. So, the full constructor should look like this:

    public Fieldset(String titleFieldset, JPanel panel, Color colorBorder) {
        super();    
        this.border = new TitledBorder(new LineBorder(colorBorder == null ? Color.BLACK : colorBorder), titleFieldset);
        this.setBorder(border);
        this.layoutPanel = panel;
        this.layoutPanel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        super.add(layoutPanel);
    }