JMenuBar not showing when it should - java

Everything seems right, but the menubar doesn't show up, I might just be missing something.
This frame gets called from the main class with a simple new AffmView();. Could that cause this problem?
public class AffmView extends JFrame {
public AffmView() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(AffmView.class.getName()).log(Level.SEVERE, null, ex);
}
this.setSize(700, 500);
this.setTitle("'s Factorio Mod Manager");
this.setLocationRelativeTo(null);
JMenuBar menubar = new JMenuBar();
JMenu packsMenu = new JMenu("Modpacks");
JMenuItem newPackMI = new JMenuItem("New Pack");
packsMenu.add(newPackMI);
menubar.add(packsMenu);
this.setJMenuBar(menubar);
this.rootPane.setLayout(new GridBagLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//snipped so that StackOverflow wont make me write an essay, but its
//just some basic swing
this.setVisible(true);
}
}
I have confirmed that none of the snipped parts cause the issue.

As mentioned by #FastSnail and #camickr in the comments, the issue lies with the usage of this.rootPane.
getContentPane() should have been used instead.

Related

Tabbedpane change tabs position Java Swing

I have i situation when my Tabbedpane have 3 tabs and then I choose someone of them, it sets in first position and move whole tabs.
I couldn't find in Internet anything like that, everyone has fixed positions of their tabs. How to fix tabs?
//EDITED
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
JFrame frame = new JFrame("Somest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
frame.add(UpperPanel);
}
}
Find out that try {...} "enable" this problem. Used try {...} to change JChooser from standard to more-or-less Windows chooser view. What I have to do?
The reason is that this is implemented differently in various Look and Feel implementations (See here, section 6.2.1):
[...] selecting a tab that is not in the frontmost row or column moves that row or column to the front. This does not occur in a JTabbedPane using the default Metal L&F as can be seen in the TabbedPaneDemo example above. However, this does occur when using the Windows, Motif, and Basic L&Fs. This feature was purposefully disabled in the Metal L&F [...]
The responsible method is shouldRotateTabRuns(...) and returns true or false in various TabbedUI implementations (see Metal L&F vs. Basic L&F).
To prevent tab rotation, you could overwrite like this:
public class TabExample extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
TabExample app = new TabExample();
app.setVisible(true);
}
});
}
private TabExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
setLocationRelativeTo(null);
setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
// Check if TabbedPaneUI is instance of WindowsTabbedPaneUI
// You may add checks for other L&F
if (tabbedPane.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI) {
tabbedPane.setUI(new MyWindowsTabbedPaneUI());
}
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
add(tabbedPane, BorderLayout.NORTH);
}
// Class that overwrites WindowsTabbedPaneUI and returns false
class MyWindowsTabbedPaneUI extends com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI{
#Override
protected boolean shouldRotateTabRuns(int tabPlacement) {
return false;
}
}
}

How to Set Vertical Separators for JMenuItems

I can only set horizontal separator to my code , how to set vertical one ? Similar to this
http://jade-cheng.com/hpu/2012-spring/csci-2912/assignment-5/blueprint-2.png
file.add(newMenuItem);
file.add(openMenuItem);
file.add(saveMenuItem);
file.add(subMenu);
file.addSeparator();
file.add(exitMenuItem);
Vertical separator in JMenuItem? The only thing which comes to my mind and which you can treat as a JSeparator is something like below:
But this left "JSeparator" is not an extra added JSeparator, but depends on LookAndFeel.
Below you see the same JFrame with the same JMenuBar but with different lookandfeel:
The code for both screens is exactly the same, but executed with different look and feels:
public class NewClass extends JFrame {
public NewClass() throws HeadlessException {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
menu.add(new JMenuItem("Open..."));
menu.add(new JMenuItem("Save"));
menu.add(new JMenuItem("Save as..."));
menu.addSeparator();
menu.add(new JMenuItem("Delete"));
setJMenuBar(menuBar);
setSize(new Dimension(500,500));
setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new NewClass();
} catch (ClassNotFoundException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Note, that
menu.add(new JSeparator(JSeparator.VERTICAL));
will not generate any separator at all (you can try)
As already pointed out by #guitar_freak, some LayoutManagers give you this effect for free, whereas others do not. If you wanted this effect for any LM, you'll have to roll up your sleeves a bit.
A JMenu is an AbstractButton that has no layout by default. When you add something to a JMenu, you're actually adding it to the menu's internal JPopupMenu, which has a DefaultMenuLayout (subclass of BoxLayout) as it's layout by default.
Things to try: write your own MenuItemUI to install on the JMenu, or subclass the JMenu to use a JPopupMenu with a different LayoutManager. I haven't tried either so I'm not sure which is correct.
Personally, I'd just leave it up to the L&F as #guitar_freak suggested. It seems to much work for too little gain, but ultimately, only you can decide that.
I think this is what you're looking for:
file.add(new JSeparator(SwingConstants.VERTICAL));

Content of JInternalFrame packed too tightly

When I call pack() on a JInternalFrame it is not packed correctly, mostly too tight.
Edit:
Here is a minimal example that shows the behavior described above.
It also seems to depend on the used lookandfeel (here: Nimbus).
import javax.swing.*;
public class JInternalFrameTester {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
System.err.println("Failed setting NimbusLookAndFeel");
}
JFrame frame = new JFrame();
JDesktopPane desktop = new JDesktopPane();
desktop.setOpaque(true);
frame.setContentPane(desktop);
frame.setSize(250, 250);
frame.setVisible(true);
JInternalFrame iframe = new JInternalFrame("Internal Frame");
JTextField textfield = new JTextField("Any text here");
iframe.add(textfield);
iframe.setVisible(true);
/* XXX If placed here, it crashes the layouts */
iframe.pack();
desktop.add(iframe);
/* XXX If placed here, the layout is right */
//iframe.pack();
}
}
You have to call pack() after adding the JInternalFrame to the respective JDesktopPane.

how can i trigger a JPanel to show up in a different application?

I'm trying to make a little application in console. The whole thing is yes or no if statements.
Everything is working somewhat perfectly, but the last thing that happens is a JPanel pops up with a picture in it.
The rest of the app works and the JPanel with the picture in it works but i don't know how to make the JPanel to show up, can someone tell me what I should do, plz? ty
Start by taking a look at Creating an UI with Swing
I order for any component to be showing on the screen, it needs to be attached to some kind of Window, for example...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new YourAwesomePanelHere());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
when a JPanel draw a image you must repain it ;
test such as:
panel.revalidate();
panel.repain;

How to call setUndecorated() while application is running?

Hi I'm not such a professional at programming so i come here to ask how i can make this possible.
Issue: Client game is running once fullscreen mode clicked i want it to call setUndecorated() but cant while the frame is already visible.
I realized that i would need to create a new frame but im unsure how to transfer everything over, i have tried myself and all i get is a blank white screen of the new frame.
Here is my code:
public Jframe(String args[]) {
super();
try {
sign.signlink.startpriv(InetAddress.getByName(server));
initUI();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void initUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.BLACK);
gamePanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
gamePanel.add(this);
JMenu fileMenu = new JMenu("Menu");
String[] mainButtons = new String[] { "-", "Donate", "-", "Vote", "-", "Hiscores", "-", "Forums", "-", "Exit"};
for (String name : mainButtons) {
JMenuItem menuItem = new JMenuItem(name);
if (name.equalsIgnoreCase("-")) {
fileMenu.addSeparator();
} else {
menuItem.addActionListener(this);
fileMenu.add(menuItem);
}
}
JMenuBar menuBar = new JMenuBar();
JMenuBar jmenubar = new JMenuBar();
Jframe.frame.setMinimumSize(new Dimension(773, 556));
frame.add(jmenubar);
menuBar.add(fileMenu);
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
init();
} catch (Exception e) {
e.printStackTrace();
}
I hope any of you can help i really appreciate thanks!
If this is for a game then most likely you don't want this. You should take a look at
http://download.oracle.com/javase/tutorial/extra/fullscreen/exclusivemode.html
I may be pointing out the obvious, as well as facilitating the wrong approach, but can't you just make it not visible then make it visible again?
i.e.
myFrame.setVisible(false);
myFrame.setUndecorated(true);
myFrame.setVisible(true);
However, there is a better approach, as "ghostbust555" pointed out.
This is the setFullScreenWindow() that answer was referring to:
public void goFullScreen() {
GraphicsDevice myDevice =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// wrap in try because we don't want to lock the app in fullscreen mode
try {
myDevice.setFullScreenWindow(myFrame);
// do your stuff
...
} finally {
myDevice.setFullScreenWindow(null);
}
}

Categories