Java Swing and Mac view - java

I'm training with Java and Swing, in Windows the GUI is shown correctly, instead on MAC there are several problems.
The JMenuBar is shown in the window, while should be shown in the MAC bar [finder].
Code of JMenuBar:
JMenuBar menuBar = new JMenuBar();
preferenceItem.addMouseListener(this);
printItem.addMouseListener(this);
menuBar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.add(printItem);
fileMenu.add(exportItem);
menuBar.add(optionMenu);
optionMenu.add(preferenceItem);
setJMenuBar(menuBar);
Than another problem is how two panels are shown. As you can see in the previous image backup button, which is in the second panel is shown above the first panel.
public OptionDialog(){
super();
setTitle(NOME_APPLICAZIONE);
Dimension dimensioni = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)(dimensioni.getWidth()/2-getWidth()/2), (int)(dimensioni.getHeight()/2-getHeight()/2));
this.setVisible(true);
this.add(pnlDialog, BorderLayout.NORTH);
this.add(pnlDialogBackup, BorderLayout.SOUTH);
pnlDialog.setBorder(general);
GridLayout grid = new GridLayout(7,2,20,5);
pnlDialog.setLayout(grid);
pnlDialog.add(lblLingua);
pnlDialog.add(Language);
pnlDialog.add(lblCarattere);
pnlDialog.add(Char);
pnlDialog.add(lblOrdinamento);
pnlDialog.add(Order);
pnlDialog.add(lblScadenza);
pnlDialog.add(Scadenza);
pnlDialog.add(lblNotifica);
pnlDialog.add(Notifica);
pnlDialog.add(lblTempo);
pnlDialog.add(Time);
pnlDialog.add(Applica,BorderLayout.PAGE_END);
pnlDialogBackup.setBorder(backup);
pnlDialogBackup.add(Backup);
Notifica.addItemListener(this);
lblTempo.setVisible(false);
Time.setVisible(false);
setSize(new Dimension(300, 300));
setResizable(false);
}

Related

How to make an extra JMenuBar and JLabel to a game made by a gameengine?

I have a game made with my game engine Java class.
The game engine uses paintComponent to draw the elements.
Here is a snippet from the game:
gamepic
I want to add this game an extra JLabel and JMenu to the top or the button of the game.
Here is my code:
public class LabyrinthGUI {
private JFrame frame;
private GameEngine gameArea;
private String PlayerName;
public LabyrinthGUI(String PlayerName) {
frame = new JFrame("GAME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//making new manu bar
JMenuBar mb = new JMenuBar();
JMenu sg = new JMenu("Settings");
JMenuItem ng = new JMenuItem("New game");
mb.add(ng);
mb.add(sg);
frame.getContentPane().add(mb);
gameArea = new GameEngine(PlayerName);//This is a game engine
JLabel l1 = new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
frame.add(l1);
frame.getContentPane().add(gameArea);
frame.setPreferredSize(new Dimension(800, 600));
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
The problem with my code is it does not seem to add the JLabel and JMenuBar to the game.
To add a JMenuBar to your JFrame you have to use the setJMenuBar() method:
public void setJMenuBar(JMenuBar menubar)
Sets the menubar for this frame.
Parameters:
menubar - the menubar being placed in the frame
See Also:
getJMenuBar()
So you use it like:
frame.setJMenuBar(mb);
For the label and the game engine being added to the content pane: A JFrame uses the BorderLayout layout manager as default for placing the components on the pane. See the documentation of JFrame:
The default content pane will have a BorderLayout manager set on it.
Because you haven't specify the locations where exactly you want to place the components with the BorderLayout layout manager, the component will be placed in the CENTER. However there can only be one component at a slot defined by the BorderLayout layout manager. The latest added component will override previous placed components (in the same slot), so your added JLabel will be overridden by the later added game engine. Depending on how/where you want to place the JLabel and your game engine, you have to use a different layout manager or specify where you want to place the components with the BorderLayout layout manager.
The way to add a JMenuBar to a JFrame is not:
frame.getContentPane().add(mb);
but:
this.frame.setJMenuBar(mb);
Take a look at Creating Menus section in the Java Tutorial.

JMenuBar not showing

I am trying to get my JMenuBar to display in the GUI, however it just appears as a 1-pixel line at the top.
This is my code...
public LibraryView() {
setBounds(100,100,640,480);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JMenuBar b1 = new JMenuBar();
JMenu m1 = new JMenu("Test");
JMenuItem i1 = new JMenuItem("Item1");
this.setJMenuBar(b1);
}
Could someone please help me to understand what is wrong.
You need to add the JMenu and JMenuItem to the JMenuBar. You also need to pack() and setVisible(true); at the end of the method, just before the GUI is shown...
public LibraryView() {
setBounds(100,100,640,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JMenuBar b1 = new JMenuBar();
JMenu m1 = new JMenu("Test");
JMenuItem i1 = new JMenuItem("Item1");
m1.add(i1); // ADDED
b1.add(m1); // ADDED
this.setJMenuBar(b1);
pack(); // ADDED
setVisible(true); // MOVED
}
You should call setVisible(true) only after adding all components to the top level window. Also don't forget to use layout managers, to let these managers and your component's preferredSize set the sizes of components, and don't forget to call pack().
call repaint() right before you call set visible(true) this NEVER FAILS.
setJMenuBar(menuBar);
menuBar.add(jMenuItem);
repaint(); //then
setVisible(true); //Assured NEVER FAILS

adjust size of frame components in swings when it is maximized

Hello Please tell me the code to size frame components in swings so that they get same screen percentage as provide before.
I have used Three panels one main and other are subpanels.The main menu has border layout.One submenu has border and other gridlayout.When I maximize the window the upper panel moves upward and bottom one sticks to the bottom
My code.:
JPanel mainPane = new JPanel(new BorderLayout());
mainPane.add(buttonPane, BorderLayout.SOUTH);
mainPane.add(chatPane, BorderLayout.NORTH);
In Swing, you can use layout managers to have your components resized as you want.
Probably the most used for this cases is BorderLayout:
JFrame myframe = new JFrame("Hello world!");
myframe.setLayout(new BorderLayout());
myframe.setSize(500,500);
//The CENTER component expands whenever the window is resized
myframe.add(new JScrollPane(new JTextArea()), BorderLayout.CENTER);
//The other components don't expand, they shrink to their minimum size
myframe.add(new JButton("Do something"), BorderLayout.SOUTH);
myframe.add(new JButton("Click me"), BorderLayout.NORTH);
myframe.setVisible(true);
I recommend you to see the Swing tutorial. It's an excellent resource to get you started.

Swing: Showing a transparent Panel hovering over another Panel

I want to show a textArea showing some text (will show log lines) , and have an animated gif hoovering above it. I tried the solution described here , but all I get is a grey screen. Hints?
public class TestLayeredPanes {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
public TestLayeredPanes() {
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
//Build the animated icon
JLabel buildingIcon = new JLabel();
buildingIcon.setIcon(new ImageIcon(this.getClass().getResource(
"/com/ct/tasks/cmviewer/gui/progress_bar.gif")));
JPanel iconPanel = new JPanel();
iconPanel.add(buildingIcon);
//Build the textArea
JTextArea textLog = new JTextArea("Say something");
JPanel textPanel = new JPanel();
textPanel.add(new JScrollPane(textLog));
//Add the panels to the layered pane
lpane.add(textPanel, 0);
lpane.add(iconPanel, 1);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new TestLayeredPanes();
}
}
Try putting your animated GIF on the glass pane of your root pane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
JXLayer make easier to do that. Look at JXLayer samples.
You also can take a look at code of XSwingX
Since you started with a working example, why did you remove lines of code from the example you copied?
Layered panes don't use a layout manager therefore the size of your components are (0, 0), so there is nothing to display. The setBounds(...) method in the example are there for a reason.

Java: Difficulty with Swing

I'm trying to modify a GUI. It is hosting a GLCanvas displaying JOGL content.
Here is the code to set it up:
private void setupWindow() {
this.frame = new JFrame(WINDOW_TITLE);
frame.setSize(width, height);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(listener);
menu.add(exitItem);
frame.setJMenuBar(menuBar);
}
Currently, the canvas takes up the entire space in the window, aside from the menu bar. I'd like to make space for other controls in the window, like buttons and list boxes. How can I do it?
I tried inserting the following, but it didn't work:
private void setupWindow() {
this.frame = new JFrame(WINDOW_TITLE);
frame.setSize(width, height);
// ** inserted the following:
JPanel canvasPanel = new JPanel(new BorderLayout());
canvasPanel.add(canvas);
canvasPanel.setSize(30, 40);
canvasPanel.setVisible(true);
// **
frame.add(canvasPanel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(listener);
menu.add(exitItem);
frame.setJMenuBar(menuBar);
}
This doesn't modify the appearance of the window at all.
What should I be doing here? I'm not too familiar with Java GUIs.
Update: Changing the constructor's argument from BorderLayout to FlowLayout causes the GLCanvas to disappear.
Per Anon's answer, you really need to better understand the layout managers. Your question is too open ended.
In case this helps though:
JPanel canvasPanel = new JPanel(new BorderLayout());
frame.add(canvasPanel, BorderLayout.CENTER);
canvasPanel.add(canvas);
Box leftBtnsBox = Box.createVerticalBox();
frame.add(leftBtns, BorderLayout.WEST);
leftBtns.add(new JButton("Button 1"));
leftBtns.add(new JButton("Button 2"));
This code sets a BordeLayout explicitly, but I think the default layout manager for panels (and content panes) is BorderLayout. This code will put two buttons to the top left of the canvas panel arranged vertically. Because you are using these layout managers, your second code example's setSize had no effect.
I suggest checking out this guide to layout managers - it will give you enough info to see what you should try.

Categories