Search code examples
javaswingjframejpaneljtabbedpane

Panel inside tab not displayed


I'm trying to have an UI using IntelliJ GUI Form. I've created a class that extends the JFrame and contains only the JTabbedPane with 2 Tabs each one having a JPanel. Then 2 other classes that extends JPanel. But, no panel displays when launching.

I've tried to invalidate, repaint and revalidate the panels

//main class
import javax.swing.*;

public class jumpManMain {
    public static void main (String[] args) {
        SwingUtilities.invokeLater(() -> {
            final JFrame frame =  new frmTabs();
        });
    }
}

// tabbedpanel class
public class frmTabs extends JFrame implements constants{
    private JTabbedPane tabbedPane;
    private JPanel panelTab1;
    private JPanel panelTab2;

    public frmTabs()
    {
        setContentPane(tabbedPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
        panelTab1 = new panel1();
        panelTab2 = new panel2();
        pack();
        setVisible(true);
        setSize(FORM_WIDTH,FORM_HEIGHT);


        ChangeListener changeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent changeEvent) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                int index = sourceTabbedPane.getSelectedIndex();
                System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
                panelTab1.revalidate();
                panelTab1.repaint();
                panelTab2.revalidate();
                panelTab2.repaint();
            }
        };
        tabbedPane.addChangeListener(changeListener);
    }

}
//tabs form generated by IntelliJ
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="frmTabs">
  <tabbedpane id="78766" binding="tabbedPane">
    <constraints>
      <xy x="20" y="20" width="900" height="560"/>
    </constraints>
    <properties>
      <minimumSize width="900" height="560"/>
      <preferredSize width="900" height="560"/>
    </properties>
    <border type="none"/>
    <children>
      <grid id="35c5e" binding="panelTab1" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <tabbedpane title="Jumps arrived"/>
        </constraints>
        <properties>
          <maximumSize width="1000" height="1000"/>
          <minimumSize width="900" height="560"/>
          <preferredSize width="900" height="560"/>
        </properties>
        <border type="none"/>
        <children/>
      </grid>
      <grid id="4323" binding="panelTab2" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
        <margin top="0" left="0" bottom="0" right="0"/>
        <constraints>
          <tabbedpane title="Jumps created"/>
        </constraints>
        <properties>
          <maximumSize width="1000" height="1000"/>
          <minimumSize width="900" height="560"/>
          <preferredSize width="900" height="560"/>
        </properties>
        <border type="none"/>
        <children/>
      </grid>
    </children>
  </tabbedpane>
</form>

//panel1 XML
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="panel1">
  <grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="5" left="5" bottom="5" right="5"/>
    <constraints>
      <xy x="21" y="20" width="900" height="560"/>
    </constraints>
    <properties>
      <minimumSize width="900" height="560"/>
      <preferredSize width="900" height="560"/>
      <requestFocusEnabled value="true"/>
    </properties>
    <border type="bevel-raised" title="Jump Ticket Manager">
      <font name="Arial Black" style="1"/>
      <title-color color="-4451918"/>
    </border>
    <children>
....
    </children>
  </grid>
</form>
...

Solution

  • Effectively using IntelliJ GUI the solution is this one:

    1) Create main class

    public class jManagerMainUI extends JFrame {
    private JPanel panelTab1;
    private JPanel panelTab2;
    private JTabbedPane tabbedPane;
    
    public jManagerMainUI(String param) {
        setContentPane(tabbedPane);
        tabbedPane.setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
        setDefaultCloseOperation(3);
        setBounds(FORM_LEFT, FORM_TOP, FORM_WIDTH, FORM_HEIGHT);
        tabbedPane.setComponentAt(0,new jA(param).getPanel());
        tabbedPane.setComponentAt(1,new jC(param).getPanel());
        pack();
        setVisible(true);
        setSize(FORM_WIDTH, FORM_HEIGHT);
    }
    }
    

    2) Create one class for each tab that will return the reference to the panel

    public class jA extends JFrame {
        private JPanel panelA;
        //Tab A
        private JButton btAddA;
    
        public JPanel getPanel() {
            return panelA;
        }
    ...