Jmenu in Swing: Set The Window Minimum Size in Java
Jmenu in Swing: Set The Window Minimum Size in Java
Jmenu in Swing: Set The Window Minimum Size in Java
In Java 1.6, Sun added the ability to set the minimum size of a Window, Frame, or JFrame. To set the minimum size of a window, call the setMinimumSize() method. This prevents the user from resizing the window smaller than the size specified. The following example shows how to set the minimum size of a JFrame:
view plaincopy to clipboardprint?
1. 2.
import javax.swing.JFrame;
4. 5. //Create the window 6. JFrame frame = new JFrame("Window"); 7. frame.setSize(800, 800); 8. 9. //Set the minimum size 10. Dimension minimumSize = new Dimension(400, 400);
11. frame.setMinimumSize(minimumSize);
JMenu in Swing
Windows users are used to a certain interface, and it is best if you keep your program has similar to the interface as possible so the user doesn't have to learn an entirely new interface (when possible). A menu is something that appears at the top of almost every program you will use. It contains short cuts to commonly use features of the program. In this tutorial we are going to create a simple menu that has two menus menu items and other each menu item, we will have 2 menu items. They will be separated by a separator. When you click on each menu item, a popup message will be displayed. We are going to create our class as an extension of the JMenuBar so that we can create an extension of the JFrame that adds the menu bar to every form. First the class skeleton: Code:
}
The action listener is required for us to listen for click events on each menu item. Each item in the menu is a JMenu object. We are going to add two of them.
Code:
Add the above lines just below the class declaration line. Now in the constructor, add these lines: Code:
add(menuFile); add(menuEdit);
We also need to add four menu items. itmNew itmClose itmUndo itmCut itmNew and itmClose will be displayed under the file menu. Add these four variables to the class variables section: Code:
JMenuItem JMenuItem JMenuItem JMenuItem itmNew = new JMenuItem("New"); itmClose = new JMenuItem("Close"); itmUndo = new JMenuItem("Undo"); itmCut = new JMenuItem("Cut");
The first two are for the file menu and the other two are for the edit menu. Now in our constructor, we will add them to the appropriate menu. Code:
menuFile.add(itmNew); menuFile.addSeperator(); menuFile.add(itmClose); menuEdit.add(itmUndo); menuEdit.addSeperator(); menuEdit.add(itmCut);
Now, all that is left is to set up the action listeners and action commands. Code:
itmNew.setActionCommand("new");
The action command is what we will use in the action performed method to determine which menu item was clicked. Add this method: Code:
public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("new")) { JOptionPane.showMessageDialog(null,"New"); } else if (e.getActionCommand().equals("close")) { JOptionPane.showMessageDialog(null,"Close"); } else if (e.getActionCommand().equals("undo")) { JOptionPane.showMessageDialog(null,"Undo"); } else if (e.getActionCommand().equals("cut")) { JOptionPane.showMessageDialog(null,"Cut"); } }
The getActionCommand will return the action command associated with the menu item that was clicked. This we can use to determine what action to perform. Now, the last thing we are going to do is create a JFrame class that will have the menubar added to it as soon as we create it declared. Code:
public class frame extends JFrame { public frame() { this.setJMenuBar(new MenuBar()); } }
Now instead of creating an instance of JFrame you will create an instance of frame and it will always have the menu bar on the frame.
In order to display tooltips for individual items in a list box, we need to override the getToolTipText() method in JList.
view plaincopy to clipboardprint?
1.
In this method, the first thing we need to determine is where the mouse pointer is located. Based on the mouse location, we can determine which item in the list box the mouse is over.
view plaincopy to clipboardprint?
1. 3. 4. 5.
//Get the mouse location 2. Point point = event.getPoint(); //Get the item in the list box at the mouse location int index = this.locationToIndex(point);
Once we have determined the index of the list box item the mouse is over, we can get and return the value of the item from the JList model.
view plaincopy to clipboardprint?
1. 2.
//Get the value of the item in the list return (String) this.getModel().getElementAt(index);
Below is the complete code for the list box. NOTE: The FontCellRender source code can be found here.
view plaincopy to clipboardprint?
1. 2. 3. 4. 5.
8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27.
28. }
{ //Construct a JList with the font names super(GraphicsEnvironment.getLocalGraphicsEnvironment(). getAvailableFontFamilyNames()); //Set the cell render this.setCellRenderer(new FontCellRenderer()); } public String getToolTipText(MouseEvent event) { //Get the mouse location Point point = event.getPoint(); //Get the item in the list box at the mouse location int index = this.locationToIndex(point); /Get the value of the item in the list return (String) this.getModel().getElementAt(index); }
This Java tutorial shows how to use a custom cell renderer to create a font list box. Below is a screen shot of the font list box that we will create in this tutorial:
In a previous tutorial, we showed how to get the names of the available fonts on your computer. This list of font names will be used to populate the items in the list box:
view plaincopy to clipboardprint?
1. 3. 4. 6. 7. 8.
//Get the local graphics environment 2. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); //Get the font names from the graphics environment 5. String[] fontNames = env.getAvailableFontFamilyNames(); //Create the JList with the font names JList listBox = new JList(fontNames);
Next, the DefaultListCellRenderer will be extended to render the font names in that font. In the new class, we will override getListCellRendererComponent() method.
view plaincopy to clipboardprint?
1. 2. 3. 4. 5. 1. 2. 3. 4. 5. 6. 1. 2.
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
In this method, we will get an instance of the default cell renderer for this item:
view plaincopy to clipboardprint?
//Get the default cell renderer JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Next, we will create a new font for the based on the value in the list box item:
view plaincopy to clipboardprint?
//Create a font based on the item value Font itemFont = new Font((String) value, Font.PLAIN, 24);
Since not all characters are displayable with every font, we will need to check to see if the font is capable of displaying the font name. If the font name is displayable with the font, we will use that font for displaying the the font name. However, if the font is not capable of displaying the font name, we will use the standard, but larger font.
view plaincopy to clipboardprint?
1. 4.
if (itemFont.canDisplayUpTo((String) value) == -1) 2. { 3. //Set the font of the label label.setFont(itemFont); 5. } 6. else 7. {
//Create a font based on the item value String fontName = label.getFont().getFontName(); Font largerFont = new Font(fontName, Font.PLAIN, 24); //Set the font of the label label.setFont(largerFont);
The final step for this method is to return the modified renderer:
view plaincopy to clipboardprint?
1. 1. 2. 3. 4.
return label;
7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.
Object value, int index, boolean isSelected, boolean cellHasFocus) { //Get the default cell renderer JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //Create a font based on the item value Font itemFont = new Font((String) value, Font.PLAIN, 24); if (itemFont.canDisplayUpTo((String) value) == -1) { //Set the font of the label label.setFont(itemFont); } else { //Create a font based on the item value String fontName = label.getFont().getFontName(); Font largerFont = new Font(fontName, Font.PLAIN, 24);
1.
listBox.setCellRenderer(new FontCellRenderer());
This Java tutorial shows how to go beyond the predefined cursors in Java and create your own cursors using a GIF or PNG that are dsiplayed when the user moves the mouse over a Java component (AWT or Swing). Creating the Cursor Image For the custom cursor, a transparent GIF or PNG will be needed for the cursor. For this tutorial, a sample transparent GIF image of a pencil is provided below (right click to save):
Loading the Cursor Image The image for the custom cursor needs to be loaded into an Image object.
view plaincopy to clipboardprint?
1. 3. 4. 5.
//Get the default toolkit 2. Toolkit toolkit = Toolkit.getDefaultToolkit(); //Load an image for the cursor Image image = toolkit.getImage("pencil.gif");
Defining the Cursor Hot Spot The cursor hot spot is used for the point location in mouse events. For a cursor such as a cross-hair cursor, the hot spot would be in the center of the cursor. In our example, the cursor hot spot will be at the pencil point or the point 0,0.
view plaincopy to clipboardprint?
1. 2.
//Create the hotspot for the cursor Point hotSpot = new Point(0,0);
Creating the Custom Cursor To create the custom image, we put together the cursor image and the hot spot:
view plaincopy to clipboardprint?
1. 2.
Displaying the Custom Cursor The final step is to notify the component to display the cursor.
view plaincopy to clipboardprint?
1.
This Java tutorial shows how to change the default close action to exit the application when the window is closed. In additon, this tutorial discusses the different close actions. When a JFrame is closed, the default behavior is to hide the window when the user clicks on the close box. There are four different actions that can be performed when the window is closed. These actions are defined in
DISPOSE_ON_CLOSE - Dispose the window when closed. DO_NOTHING_ON_CLOSE - Do nothing when the window is closed. EXIT_ON_CLOSE - Exit the application when the window is closed. HIDE_ON_CLOSE - Hide the window when the window is closed. To change the default close action on a JFrame, it is done by simply calling the setDefaultCloseAction() method on a JFrame. The following example shows how to change the default close action:
view plaincopy to clipboardprint?
1.
In the Java Swing classes JFrame and JApplet, the glass pane provides a unique and powerful feature. The glass pane is a layer above all other controls within the JFrame and JApplet. It allows you to paint above all other controls. In addition to drawing graphics over the contents of a JFrame or JApplet, the glass pane can also be used to handle mouse events. Be aware that adding a mouse listener to the glass pane will prevent any mouse events from being sent to the controls below it. The first example will demonstrate drawing graphics over all controls in the JFrame.
view plaincopy to clipboardprint?
1. 2.
5. 6. 7. 8. 9. 10.
{ g.setColor(Color.red); //Draw an oval in the panel g.drawOval(10, 10, getWidth() - 20, getHeight() - 20); }
11. };
12. 13. //Turn off the opaque attribute of the panel 14. //This allows the controls to show through 15. panel.setOpaque(false); 16. 17. //Set the glass pane in the JFrame
18. setGlassPane(panel);
By setting the opaque attribute on the JPanel to false, it turns of the background of the JPanel. When the panel is painted, only the graphics in the paintComponent will be drawn without the background. By simply modifying the paintComponent you can create a completely different effect. In the next example, we
set the alpha level for the color. This allows a color to be painted while allowing the controls below it to be shown.
view plaincopy to clipboardprint?
1. 2.
5. 6. 7. 8. 9. 10. 11.
{ //Set the color to with red with a 50% alpha g.setColor(new Color(1, 0, 0, 0.5f)); //Fill a rectangle with the 50% red color g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20); }
12. };
This tutorial only covers the basics of using the glass pane. There are a number of interesting possible uses of the glass pane. Below are some ideas of things to try with the glass pane: Create a transition effects between screens. Display a progress screen within the glass pane. Display debug information within the glass pane