How do i create a JFrame window in eclipse on a Mac that has an icon that makes the window full screen like the double arrow icon on most windows at the top right??
Take a look at
Fullscreen feature for Java Apps on OSX Lion
And Java Runtime System Properties, which may be of interested
or How can I do full screen in Java on OSX if those were the wrong feature you wanted
UPDATE
Lucky for you JFrame extends Window via Frame...
public class TestMacFullScreen {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
JLabel label = new JLabel("Look ma, no hands");
frame.add(label);
enableOSXFullscreen(frame);
frame.setVisible(true);
}
});
}
public static void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
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;
}
}
}
I'm trying to use the DJ Native Swing API to open a WebBrowser on my code:
public class YoutubeViewer {
public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("YouTube Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(getBrowserPanel(null), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
// don't forget to properly close native components
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
#Override
public void run() {
NativeInterface.close();
}
}));
}
public static JPanel getBrowserPanel(String trailer) {
trailer = "https://www.youtube.com/watch?feature=player_detailpage&v=6kw1UVovByw";
JPanel webBrowserPanel = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser();
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
webBrowser.navigate(trailer);
return webBrowserPanel;
}
}
But I keep getting this error:
Could anyone help me?
This kind of exception is caused when the class loader (module of the interpreter) do not locate the respective class (SWTNativeInterface, in your case) in the class path. You should place the respective library in the project's classpath.
You set this classpath in the project properties.
I have created JMenuItems with Key Accelerators, then i have added them to the menubar directly without the need to be added to the JMenu so they look like JButtons, every thing work just fine, but i noticed that JMenuItem never get focus when clicked or key pressed, that make some problems to me for example:
One of the JMenuItems is for save, also i have one JTextField which do some validation when losing focus, but that not working since when pressing the Save, the focus kept there on the JTextField.
Any Ideas ?!
I'd suggest using a JToolBar and taking advantage of the Action API
Here's a really short example of how you might be able to achieve accelerator support for toolbar (or any) button.
public class MenuTest {
public static void main(String[] args) {
new MenuTest();
}
public MenuTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JToolBar tb = new JToolBar();
tb.add(new FastButton(new OpenAction()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(tb, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FastButton extends JButton {
private FastButton(Action action) {
super(action);
setHideActionText(true);
}
#Override
protected void configurePropertiesFromAction(Action a) {
super.configurePropertiesFromAction(a);
if (a != null) {
KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(ks, "Action.accelerator");
actionMap.put("Action.accelerator", a);
}
}
}
public class OpenAction extends AbstractAction {
public OpenAction() {
putValue(NAME, "Open");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("/folder_document.png")));
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Open");
}
}
}
I have a class developed with windowbuilderpro that i want to close also from a JButton further than with the standard X button on the window, so here the example of the class :
public class MainWindow {
public JFrame frame;
public MainWindow() {
initialize();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Show the main Frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
When i close the window from the X button the window close correctly and the process terminate.
When i close instead from a JButton that have this listener :
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
the frame window close correctly but the process remain alive ... Why ?
As you can see there is an AWT-Shutdown thread that start and terminate continuously, How can i achieve the same behaviour of the X button that close also the application process ?
Notes :
System.exit(0); is not suitable because it terminate the application also if there are another background running thread and i don't want that . The MainWindow class should close and release it's resource, the same behaviour that have closing the application with the X button that close the MainWindow instance but if there are background thread running it doesn't kill they but wait until they finished their work...
Enviroment :
JDK 7
Eclipse 3.7.1
not sure what you really needed, that looks like that you create new JFrame again an again, don't do that, create JFrame once and re-use this Container
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // do nothing
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // same as setVisible(false)
then for visibily you can only to call frame.setVisible(true);
for more Confortable is override WindowListener, then you can control some Events
All threads in this code stop when either the x button or the Exit button are activated. Are you getting different behavior?
import java.awt.event.*;
import javax.swing.*;
public class MainWindow {
public JFrame frame;
JButton mntmExit = new JButton("Exit");
public MainWindow() {
frame = new JFrame("Close Me!");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
frame.add(mntmExit);
frame.pack();
show();
}
public void show() {
//Show the main Frame
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainWindow mw = new MainWindow();
mw.show();
}
});
}
}
Just add one line:
System.exit(0);
i'm kinda new to manipulating UI in java, so pls forgive me for this question i cant find the answer anywhere.
Description
| i'm trying to do a card game and i have an engine class that manipulates all the cards and plays made, and i want the engine to tell the UI to update the score, card position or card image.
this is an example of how i start the UI, problem here is i don't have any instance to manipulate the JLabels using the instance methods i made inside Board class, and i can't create a instance outside of the EventQueue because i would be violating "never manipulate/create UI outside UI thread"
public class Engine {
public StartUp(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (UnsupportedLookAndFeelException e) {
}
new Board().setVisible(true);
}
});
}
}
the Board class extends JPanel and adds some JLabels to the ui in the constructor , and has several methods to change text and imgs.
My question is how to properly call these methods(the ones i created to alter text and img), i'm also open o any other suggestions on how to approach this problem.
*edited:
here's simple example of my Board Class:
public class Board extends JFrame{
public JLabel img1;
public Board(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 265);
JPanel body = new JPanel(new GridBagLayout());
getContentPane().add(body);
img1 = new JLabel();
body.add(img1);
}
public void setImg1(String s){
img1.setIcon(new ImageIcon(s));
}
}
i want to be able from Engine to access setImg1(String s) method that is inside the Board to be able to change the current image during runtime
sorry if i expressed my question wrong
final edit:
solved it merging the Engine into the Board.
ty to everyone that helped and for your time
public class MainFrame extends JFrame {
public MainFrame() {
super("Demo frame");
// set layout
// add any components
add(new Board()); // adding your board component class
frameOptions();
}
private void frameOptions() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); // or setSize()
setVisible(true);
}
public static void main(String[] a) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
new MainFrame();
} catch (Exception exp) {
exp.printStackTrace();
}
}
});
}
}
The basic idiom for getting a GUI up is:
SwingUtilities.invokeLater(new Runnable() {
JFrame frame = new JFrame("My Window Title");
frame.setSize(...);
frame.add(new Board()); // BorderLayout.CENTER by default
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // center on main screen
frame.setVisible(true);
});