[Swing] How to display an arbitrary name on the menu bar on Mac

I didn't come out at once, so make a note.

problem

MainWindow.java


public class MainPanel extends JPanel{
	public MainPanel(){
		JButton button1 = new JButton("button");
		add(button1);
	}
	public static void main(String[] args){
		JFrame frame = new JFrame();
		MainPanel mainPanel = new MainPanel();
		frame.getContentPane().add(mainPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);    
	}
}

solution

If nothing is done, MainWindow will appear in the menu bar, so do as follows. Confirmed on macOS 10.12.6, JRE 9.0.1.

MainWindow.java


public class MainPanel extends JPanel{
	public MainPanel(){
		JButton button1 = new JButton("button");
		add(button1);
	}
}

//Make another class. Any name
class Loader{
	public static void main(String[] args){
		//Use the system menu bar
	 	System.setProperty("apple.laf.useScreenMenuBar", "true");
	 	//Specifying the application name
    	System.setProperty("apple.awt.application.name", "test");
		JFrame frame = new JFrame();
		MainPanel mainPanel = new MainPanel();
		frame.getContentPane().add(mainPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);    
	}
}

Supplementary explanation

Specify properties for the JRE with System.setProperty. At this time, the point is to specify ** before Swing is loaded. If you write JPanel in an extension class, Swing will be loaded first. Therefore, it is necessary to create a separate class that is not related to Swing and write it at the beginning of the main method there.

Recommended Posts

[Swing] How to display an arbitrary name on the menu bar on Mac
[Ruby on Rails] How to change the column name
[Rails] How to display an image in the view
How to switch Java in the OpenJDK era on Mac
How to display 0 on the left side of the standard input value
How to display products by category on the same list screen
How to check Java installed on Mac
How to install Eclipse (Photon) on Mac
How to publish an application on Heroku
How to switch Java versions on Mac
How to change the timezone on Ubuntu
Pre-processing to display on the browser (compiler)
How to switch the display of the header menu for each transition page
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby on Rails] How to display error messages
Use the Mac menu bar with Processing 3 apps
How to display the result of form input
[Java] Memo on how to write the source
[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to install Ruby on an EC2 instance on AWS
How to set the display time to Japan time in Rails
[Java] How to extract the file name from the path
I want to display the name of the poster of the comment
[Rails] How to change the column name of the table
As of April 2018 How to get Java 8 on Mac
Install MySQL 5.6 on CentOS6 [How to specify the version]
I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
How to get the class name / method name running in Java
How to display the select field of time_select every 30 minutes
How to dynamically change the column name acquired by MyBatis
[IOS] How to get the table name from AWS DynamoDB
How to retrieve the hash value in an array in Ruby
How to disable Set-Cookie from API on the front side
How to display a graph in Ruby on Rails (LazyHighChart)
Display an error screen during the download process to response
I want to put the JDK on my Mac PC
How to install JDK 8 on Windows without using the installer
[Rails] How to display the list of posts by category
How to run React and Rails on the same server
Learn how to customize the Navigation Bar from the sample app
[Rails] How to display information stored in the database in view
How to change the file name with Xcode (Refactor Rename)
How to check the database of apps deployed on Heroku
How to make an application with ruby on rails (assuming that the environment has been built)
How to create an application
How to uninstall Java 8 (Mac)
How to deploy on heroku
[Java] How to display Wingdings
How to handle an instance
How to install JDK8-10 (Mac)
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to install and use Composer on an ECS instance on Ubuntu 16.04
How to uninstall if you have updated to Java 9 on your Mac
[Swift] How to display when the quiz app answers correctly [Beginner]
[Swift5] How to get an array and the complement of arrays
I opened the menu bar (option menu) on Android and saw it.
How to output the value when there is an array in the array
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to switch Java version with direnv in terminal on Mac