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));
Related
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;
}
}
}
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.
I want to replace the current panel and call another panel in JPanel forms.
I try to do using setContentPane() and getContentPane() method but it gives error.
how can I do that....
I also try this but clear all the componens but do not add anything
private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
if (new ConnectionFactory().userLoginCheck(usernameText.getText(), new String(passwordText.getPassword()))) {
removeAll();
add(new ChangeUsernamePassword());
revalidate();
repaint();
//new Welcomeboard();
} else {
warningLabel.setText("Invalid Username Or Password!!!");
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(DashboardPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DashboardPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
"I want to replace the current panel and call another panel in JPanel forms"
Instead of trying to add an remove panels, use a CardLayout. Seeing how you're using Netbeans GUI Builder, see How to Use CardLayout with Netbeans GUI Builder. What the CardLayout does is allow you to change between different views without having to add and drop panels, which can be troublesome.
Also you may want to debug your if statement. Hard to tell with only the little but of code you're showing.
I would like to apply my own close and minimize buttons. Is there any way to change the JFrame design?
The trick lies in the PLAF and setDefaultLookAndFeelDecorated(true) (Specifying Window Decorations).
E.G.
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameCloseButtonsByLookAndFeel {
FrameCloseButtonsByLookAndFeel() {
String[] names = {
UIManager.getSystemLookAndFeelClassName(),
UIManager.getCrossPlatformLookAndFeelClassName()
};
for (String name : names) {
try {
UIManager.setLookAndFeel(name);
} catch (Exception e) {
e.printStackTrace();
}
// very important to get the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel gui = new JPanel(new BorderLayout());
f.setContentPane(gui);
JTree tree = new JTree();
tree.setVisibleRowCount(4);
gui.add(tree, BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(3,15)));
JToolBar toolbar = new JToolBar();
gui.add(toolbar, BorderLayout.PAGE_START);
for (int ii=1; ii<5; ii++) {
toolbar.add(new JButton("Button " + ii));
if (ii%2==0) {
toolbar.addSeparator();
}
}
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new FrameCloseButtonsByLookAndFeel();
}
});
}
}
think you are after a JWindow
http://docs.oracle.com/javase/7/docs/api/javax/swing/JWindow.html
You can then create your own buttons which actions can minimize/close your window
The only thing I'm aware that can be done is to add a WindowListener to the JFrame and handle closing events in that listener. You can make virtually anything, like displaying dialogs or even cancelling the closing of the JFrame.
See this tutorial for more details about how to write such listeners.
As for minimizing: as far as I know, there is no way to control or modify such behaviour, it's completely controlled by the operating system.
The only way to change the aspect of the minimize/close/maximize buttons is to use a custom LookAndFeel and setting JFrame.setDefaultLookAndFeelDecorated (true);.
Set jframe undecorated.
Place a jlabel for each button.
Put own icon for each Btn.
Put mouseListeners for each jlabel and
specify code eg, System.exit(0);/set ICONIFIED option
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);
}
}