Pregunta

As part of my Diploma in Software Dev we have to create a Java GUI to manage a Soccer League. My group has produced numerous JPanels that in the click-through prototype we put into JTabbedPane. This worked fine up until now where I'm moving them into separate files and into a MVC layout.

I'm using a window class to hold the top level JFrame and menubar and adding in the tabbed panes to that. This works fine in the constructor but when I remove them from the constructor and try to add them from the Bootstrap via the Window.attachTabbedPanel(String name, JPanel panel) it doesn't display but querying tabbedPane.getTabCount() display an incrementing number of tabs.

Here's a stripped back set of code: The Bootstrap file:

public class Bootstrap {

    private Window mainWindow;

    public Bootstrap() {

        //Window class is our containing JFrame and JMenuBar
        mainWindow = new Window();

        //Load up our view classes
        TestTab tab1 = new TestTab();
        TestTab tab2 = new TestTab();

        //Attach them
        mainWindow.attachTabbedPanel("Tab1", tab1.getScreen());
        mainWindow.attachTabbedPanel("Tab2", tab2.getScreen());

    } // Bootstrap()

    public gui.Window getWindow(){
        return mainWindow;
    }

} // Bootstrap

This is called by the Main file:

public class Main {

    public static void main(String[] args) {

        Bootstrap RunMVC = new Bootstrap();
        gui.Window mainWindow = RunMVC.getWindow();
        mainWindow.run();
    } // main()

} // Main

The problem starts here at the Window class, I've added in a In Constructor tab to check I haven't stuffed up the tabbedPane but it works fine at that point.

public class Window {

    private JFrame frame;
    private JMenuBar menuBarMain;
    private JMenu mnFile;
    private JTabbedPane tabbedPane;
    private int count;
    /**
     * Create the application.
     */
    public Window() {

        //Build the frame
        frame = new JFrame();
        frame.setBounds(100, 100, 1280, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));
        //End Build frame

        TestTab conTab = new TestTab();

        //Add the tabbed pane to hold the top level screens
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);    
        frame.getContentPane().add(tabbedPane, "name_1");
        tabbedPane.addTab("In Consructor", conTab.getScreen());
        count = 1;
    }

    public void attachTabbedPanel(String name, JPanel panel){
        System.out.println("Window: adding Jpanel name: "+name);
        System.out.println("panel is a: "+panel);
        tabbedPane.addTab(name, panel);
        tabbedPane.updateUI();
        System.out.println("Number of tabs: "+tabbedPane.getTabCount());
        System.out.println("Last Tab .isEnabledAt() "+tabbedPane.isEnabledAt(count++));
        tabbedPane.updateUI();
    }
    /**
     * Launch the window.
     */
    public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

And lastly the Panel:

public class TestTab {

    private JPanel screen;
    private JLabel lblSeason;
    private JButton btnEdit;
    private JLabel lblRounds;

    public JPanel getScreen() {
        return screen;
    }
    /**
     * Initialize the contents of the frame.
     */
    public TestTab() {

        screen = new JPanel();
        screen.setLayout(new MigLayout("", "[8%,right][10%,left][8%,right][10%,left][grow][50%]", "[][][grow]"));

        lblSeason = new JLabel("Test");
        screen.add(lblSeason, "flowx,cell 0 0");

        btnEdit = new JButton("Edit Test");
        screen.add(btnEdit, "cell 5 0,alignx right");

        lblRounds = new JLabel("More Testing");
        screen.add(lblRounds, "cell 0 1,alignx left");

    }

}
¿Fue útil?

Solución

Your error is here:

public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

You are using new instance of window instead of using created earlier, try to use this code

 public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Otros consejos

updateUI() does not do what you think it does. It should never be called directly - it is called from the superclass constructor to allow the Swing look and feel delegate to initialize itself.

Eliminating the call to updateUI() may solve your problem; or if the tabbed pane is already on screen, you may need to force a repaint/revalidate - the incantation for that is invalidate(); revalidate(); repaint();.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top