JTabbedPane: only first tab is drawn, the second is always empty (newbie Q)
Posted
by
paul
on Stack Overflow
See other posts from Stack Overflow
or by paul
Published on 2011-01-09T05:44:58Z
Indexed on
2011/01/09
5:53 UTC
Read the original article
Hit count: 280
I created a very simple JTabbedPane by first creating an empty JTabbedPane object, then 2 JPanels that I later add. Each JPanel is holding a object that extends JButton and implements MouseListener. Each of these holds a different image loaded from a file; the image is held locally as a buffered image and as an image icon, etc., all of which works great. The point of all that is to allow resizing of the image when the button is resized (using getscaledinstance()), because the panel is resized, because the JTabbedPane is resized, etc., within the JFrame that holds everything. I override paintComponent() to accomplish this in the class that extends JButton. I am using MigLayout Manager, and all is well on that front controlling layout constraints, growing, filling, initial sizes, preferred sizes, etc. The images the buttons hold are of different sizes and proportions, but this caused no trouble before.
Up until 2 days ago everything worked fairly well. I made some changes trying to tweak some resizing issues as I was picking up MigLayout manager. At the time I was playing around with setting various min, max, and preferred sizes using the methods provided for by the components, not the layout manager. I also fooled a bit with pack(), validate(), visible(), opaque() etc., and yes I read the article about Swing and AWT painting here: http://java.sun.com/products/jfc/tsc/articles/painting/ , and I switched to relying more and more on MigLayout. On an unrelated note, it appears JFrame's do not honor maxsize?
Somehow, today, with and without using any of these methods provided by swing, with or without using MigLayout manager to handle some of these matters instead, I now have a JTabbedPane that correctly displays the FIRST JPanel I add, but NOT THE SECOND JPanel--which, while present as a tab--does not show when selected. I have switched the order of which panel is added first, and this still holds true regardless of which JPanel I add first, telling me the JPanels are ok, and the problem is most likely in the JTabbedPane. I click on the second tab, the JTabbedPane switches, but I have what appears to be a blank button in the JPanel. A few console system-out statements reveal the following: a) that the second panel and its button are constructed b) no mouse events are being captured when I click on where the second panel and button should reside, as if it didn't exist at that point; c) when I switch to the second tab, the overrided paintComponent() method of the button within that second JPanel is never called, so it is in fact never being painted despite the tab in which it resides becoming visible; d) the JTabbpedPane getComponentCount() returns a correct value of 2 after adding the 2nd panel; e) MigLayout manager actually rocks, but I digress...
I cannot now revert to my older code, and despite my best efforts to undo whatever changes caused this, I cannot fix my new problem. I've commented out everything but the most essential calls: constructors for each object--with MigLayout; add() for placing the buttons on the panels using string-arguments appropriate for MigLayout; add() for placing the panels on the JTabbedPane, also with MigLayout string arguments; setting the default op on close for the JFrame; and setting the JFrame visible. This means I do not fiddle with optimization settings, double buffering settings, opaque settings, but leave them as default, and still, no fix; the second panel will not show itself. Each panel, I should add, when it is the first to be loaded, works fine, again re-affirming that the panels and buttons are themselves ok.
Here is part of what I am doing:
//Note: BuildaButton is a class that merely constructs my instances
File f = new File("/foo.jpg");
button1 = new BuildaButton().BuildaButton(f).buildfoo1Button();
f = new File("/foo2.jpg");
button2 = new BuildaButton().BuildaButton(f).buildfoo2Button();
MigLayout ml = new MigLayout("wrap 1", "[fill, grow]0[fill, grow]",
"[fill, grow]0[fill, grow]");
MigLayout ml2 = new MigLayout("wrap 2", "[fill, grow]5[fill, grow]",
"[fill, grow]0[fill, grow]");
foo1panel = new JPanel(ml);
foo1panel.add(button1, "w 234:945:, h 200:807:");
foo2panel = new JPanel(ml);
foo2panel.add(button2, "w 186:752:, h 200:807:");
tabs.add("foo1", foo1panel);
tabs.add("foo2", foo2panel);
System.out.println("contents of tabs: " + tabs.getComponentCount() + " elements");
mainframe.setLayout(ml2);
mainframe.setMinimumSize(new Dimension(850,800));
mainframe.add(tabs, "w 600:800:, h 780:780:");
//controlpanel is a still blank jpanel that holds nothing--it is a space holder for now & will be utilized
mainframe.add(controlpanel, "w 200:200:200, h 780:780:");
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setVisible(true);
Thank you in advance for any help you can give.
© Stack Overflow or respective owner