Reputation: 103
I recently re-wrote a lot of my application, and before I did so what I'm trying to accomplished worked just fine. I'm trying to hide a JTabbedPane
on demand, via a JCheckBoxMenuItem
in a JMenuBar
.
What I did before seemed to be exactly the same, although this time it's not working. I've tried using invokeLater(...)
but to no avail.
Here's a basic example of what I'm working with:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class ToolkitFrame extends JFrame {
private static final long serialVersionUID = -6757985823719418526L;
public static void main(String[] args) {
new ToolkitFrame().construct();
}
private UtilityTabs utilityTabs;
public UtilityTabs getUtilityTabs() {
if (utilityTabs == null) {
utilityTabs = new UtilityTabs();
}
return new UtilityTabs();
}
public ToolkitFrame() {
super();
}
public void construct() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(getGameScreen(), BorderLayout.CENTER);
add(getUtilityTabs().getTabs(), BorderLayout.EAST);
setJMenuBar(getJMenuBar());
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public JMenuBar getJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu viewMenu = new JMenu("View");
final JCheckBoxMenuItem checkbox = new JCheckBoxMenuItem("Show Utilities");
checkbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
getUtilityTabs().getTabs().setVisible(checkbox.isSelected());
System.out.println("Tabs should be: " + (checkbox.isSelected() ? "visible" : "hidden"));
}
});
checkbox.setSelected(true);
viewMenu.add(checkbox);
menuBar.add(viewMenu);
return menuBar;
}
public JPanel getGameScreen() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(765, 503));
panel.setBackground(Color.BLACK);
return panel;
}
class UtilityTabs {
private JTabbedPane tabs;
public JTabbedPane getTabs() {
return tabs;
}
private Tab tab;
public Tab getTab() {
if (tab == null) {
tab = new Tab();
}
return tab;
}
public UtilityTabs() {
tabs = new JTabbedPane(JTabbedPane.TOP);
tabs.setPreferredSize(new Dimension(275, 503));
tabs.add("FooBar", getTab());
tabs.setVisible(true);
}
class Tab extends JPanel {
private static final long serialVersionUID = 677183279827434278L;
private DefaultListModel<String> listModel;
private JList<String> list;
public Tab() {
construct();
}
public void construct() {
listModel = new DefaultListModel<String>();
for (String name : new String[] { "foo", "bar" }) {
listModel.addElement(name);
}
list = new JList<String>(listModel);
add(new JScrollPane(list));
}
}
}
}
Could anyone tell me what I'm doing wrong? I basically want the utilities for the game to be shown or hidden depending on the users settings (without restarting).
Using setVisible(false)
will not show them, but that doesn't help if I want it to be done without restarting the application.
Upvotes: 2
Views: 621
Reputation: 717
Looking at your getUtilityTabs function, regardless of what utilityTabs is, you will always return a new UtilityTabs. So, setting the visibility of the new utilityTabs does nothing to the one you already created.
This should fix it:
public UtilityTabs getUtilityTabs() {
if (utilityTabs == null) {
utilityTabs = new UtilityTabs();
}
return utilityTabs;
}
Upvotes: 3