Is it possible to move JMenuBar without adding it in a JPanel - java

I am facing a problem with my JMenuBar location in my Java application.
I am actually using ComponentResizer.java code from the article.
and everything with the resizing works fine except from the north area of my application
(undecorated JFrame) and its corners (of North Area) because the JMenuBar is preventing me from resizing from that area.
Is there a solution or maybe a hack to move the JMenuBar down a little or enable the Resizing
in the north area?
I also use setJMenuBar() method to add the JMenuBar to the north area of my application.
Code:
public class MyApp extends JFrame {
private MyApp frame;
private JMenuBar menuBar;
public static void main(String[] args) {
frame = new MyApp();
frame.setUndecorated(true);
frame.setVisible(true);
}
public MyApp(){
initComponents();
}
private void initComponents(){
setTitle("MediaForm");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 673, 482);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
}
}

JMenuBar extends JComponent, therefore you can put it anywhere you would put an ordinary component. Note that it could be confusing to the users if you put it to unexpected locations.
Also see this: add JMenuBar to a JPanel?

I've not tested this, but JRootPane is a JComponet, it may be possible to add a EmptyBorder to it, allowing the JMenuBar to be offset.
Failing that, you'll probably need to implement your own JRootPane to manage the layout of the JMenuBar & content pane
UPDATE with Testing
public class TestMenuFrame extends JFrame {
public TestMenuFrame() throws HeadlessException {
setTitle("Test");
getRootPane().setBorder(new EmptyBorder(10, 10, 10, 10));
JMenuBar mb = new JMenuBar();
mb.add(new JMenu("Test"));
setJMenuBar(mb);
setSize(100, 100);
getContentPane().setBackground(Color.RED);
setLocationRelativeTo(null);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new TestMenuFrame().setVisible(true);
}
}

Related

Java basic GUI blank

When I run this program it appears as an empty window until you fullscreen, then it can be resized as you like, why is it doing this/how do I stop it?
the program is very basic just a menubar and two panels split.
public class SplitPane {
public static void main(String[] args) {
window view = new window();
}
private static class window extends JFrame {
public window() {
this.setSize(1000, 750);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//menubar is here, must lower code quantity for stack
//panels
//graph half
JPanel graphRep = new JPanel();
//Background colour - graphRep.setBackground(Color.RED);
graphRep.setVisible(true);
String graphTitle = "Textual Representation.";
Border graphBorder = BorderFactory.createTitledBorder(graphTitle);
graphRep.setBorder(graphBorder);
//text half
JPanel textRep = new JPanel();
textRep.setVisible(true);
String textTitle = "Graphical Representation.";
Border textBorder = BorderFactory.createTitledBorder(textTitle);
textRep.setBorder(textBorder);
//splitpane
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(600, 750);
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(250);
splitPane.setLeftComponent(graphRep);
splitPane.setRightComponent(textRep);
this.add(splitPane);
}
}
this.setVisible(true);
You are making the frame visible BEFORE you add components to the frame. The layout manager is never invoked so the size of all the components remains (0, 0) so there is nothing to paint.
The frame should be made visible AFTER all the components have been added to the frame.
And the code should be:
frame.pack();
frame.setVisible();
So each component is displayed at its proper size. Don't hardcode the size() because you don't know what the size of a users screen might be.

the component does not appear in the west border layout

I Have this code
package com.company;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Alarm extends JFrame {
JFrame frame = new JFrame("Java Alarm Clock");
JMenuBar menuBar = new JMenuBar();
JMenu clock = new JMenu("Clock");
JMenu alarm = new JMenu("Alarm");
JMenu help = new JMenu("Help");
public Alarm() {
super("Java Alarm Clock");
getContentPane().setBackground(new Color(204,204,255));
setLayout(new BorderLayout());
setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
setSize(770,470);
setVisible(true);
add(new pclock() , BorderLayout.WEST);
add(menuBar , BorderLayout.NORTH);
menuBar.setBackground(new Color(204,204,255));
menuBar.add(clock);
menuBar.add(alarm);
menuBar.add(help);
}
class pclock extends JPanel {
public void paintComponent(Graphics g) {
setBackground(new Color(204,204,255));
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillOval(40, 100, 180, 180);
}
}
public static void main(String[] args) {
new Alarm();
}
}
I want the output to be like this Pic
but when i run the code the circle does not appear
it does appear only when i change this code
add(new pclock() , BorderLayout.WEST);
to
add(new pclock());
so how to make it appear in the left side like that picture ?
thank you
Class names SHOULD start with an upper case character. You custom class is named incorrectly.
The WEST area of the BorderLayout will respect the width of any component added to it. Your custom component has a size of (0, 0) so there is nothing to display. You need to override the getPreferredSize() method of your custom class to return the size of the clock.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Separating code into classes (GUI specific) using Java

In a sample program I have one class that places:
GUI components on a JPanel which is inside a JFrame.
A method makeMenu to create a menu bar
An ActionListener inside the makeMenu method to change the JPanels background color when called.
Main method.
public class GUI extends JFrame {
private JPanel jPanelRight;
private JPanel jPanelLeft;
JMenuBar menuBar;
JMenu file, help;
JMenuItem changeColor;
public GUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(makeMenu()); // call constructor to create the menu
this.setSize(800, 600); // set frame size
this.setVisible(true); // display frame
this.setTitle("understanding objects");
setLayout(new BorderLayout()); // layout manager
jPanelLeft = new JPanel(); //left jpanel
jPanelLeft.setPreferredSize(new Dimension(400, 800)); // to set the size of the left panel
jPanelLeft.setBackground(Color.blue);
jPanelRight = new JPanel(); //right jpanel
jPanelRight.setPreferredSize(new Dimension(400, 600)); // to set the size of the right panel
jPanelRight.setBackground(Color.green);
this.add(jPanelLeft, BorderLayout.WEST); //add jpanel to the left side of the frame
this.add(jPanelRight, BorderLayout.EAST); //add jpanel to the right side of the frame
}//end constructor
public JMenuBar makeMenu() {
menuBar = new JMenuBar(); //menu bar
file = new JMenu("File"); //menu item
menuBar.add(file);
help = new JMenu("Help"); //menu item
menuBar.add(help);
changeColor = new JMenuItem("Change Colour"); //sub menu item
changeColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPanelRight.setBackground(Color.red);
}
});
file.add(changeColor);
return menuBar;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI();
}
});
} // end
}//end class
I am trying to separate the code into 3 or 4 classes.
GUI class
ActionListener class
MakeMenu class
Main class (to run program)
one problem that keeps occurring is that i when i separate the code i can only change using System.out.println(); and can not change the GUI i.e. i can print out that the jPanelRight is now red but can not actually change the jPanelRight to red.
I am possibly going about this the wrong way.
A GUI to use a different class to create its menu and another different class to control the actions for the GUI's menu.

Can't align JFileChooser to the left of BorderLayout.NORTH

I have a window with a JFileChooser and a JTextArea.
The JFileChooser is in the NORTH part of the BorderLayout.
The JTextArea is in the CENTER part of the BorderLayout.
I would like to align to the left ALL my JFileChooser, but it won't move like I want and stay CENTERED.
Furthermore, I would like my JFileChooser take all the length of my window.
EDIT
Here is the main code
public class MainServer
{
public static void main(String[] args)
{
ServerBoard frame=new ServerBoard(1000, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Here is the window code
public class ServerBoard extends JFrame
{
private JButton startserver;
private JButton senddata;
private JButton sendgps;
private JTextArea messagearea;
public ServerBoard(int l, int h)
{
super("ServerBoard");
this.initialize();
this.setSize(l,h);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initialize()
{
// Define a panel
Container c=this.getContentPane();
this.messagearea=new JTextArea(40,60);
c.add(this.createNorth(), BorderLayout.NORTH);
c.add(messagearea, BorderLayout.CENTER);
}
public JPanel createNorth()
{
JPanel panelnorth=new JPanel();
JToolBar toolbarnorth=new JToolBar();
panelnorth.add(toolbarnorth);
this.startserver=new JButton("START SERVER");
startserver.addActionListener(new ServerBoardListener());
toolbarnorth.add(startserver);
this.senddata=new JButton("SEND DATA");
senddata.addActionListener(new ServerBoardListener());
toolbarnorth.add(senddata);
this.sendgps=new JButton("SEND GPS FRAME");
sendgps.addActionListener(new ServerBoardListener());
toolbarnorth.add(sendgps);
return panelnorth;
}
}
Here is my window
I really really want to use this JFileChooser. Can you help me please ?
In advance thank you a lot for the answers.
Simply nest your JPanels. Create a new JPanel, say called northPanel, that uses a BorderLayout, and add it to the main window in the BorderLayout.NORTH position, and then add your JFIleChooser to this northPanel JPanel in its BorderLayout.WEST position.
Option 2: give the northPanel a BoxLayout that is oriented along the line axis, add the JFileChooser, and add glue.

placing a transparent JPanel on top of another JPanel not working

I am trying to place a JPanel on top of another JPanel which contains a JTextArea and a button and i want to the upper apnel to be transparent. I have tried it by making the setOpaque(false) of the upper panel. but it is not working. Can anyone help me to get through this? Thanks in advance!
public class JpanelTest extends JPanel
{
public JpanelTest()
{
super();
onInit();
}
private void onInit()
{
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(100,100),BorderLayout.CENTER);
panel.add(new JButton("submit"),BorderLayout.SOUTH);
JPanel glass = new JPanel();
glass.setOpaque(false);
add(panel,BorderLayout.CENTER);
add(glass,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[])
{
new JpanelTest();
}
}
Indeed, it would be useful to tell the reason why you want panels one over another.
Starting with your code, and changing it a lot, I got it to work, but it might not do what you expect...
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 200);
onInit();
setVisible(true);
}
private void onInit()
{
JLayeredPane lp = getLayeredPane();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(), BorderLayout.CENTER);
panel.add(new JButton("Submit"), BorderLayout.SOUTH);
panel.setSize(300, 150); // Size is needed here, as there is no layout in lp
JPanel glass = new JPanel();
glass.setOpaque(false); // Set to true to see it
glass.setBackground(Color.GREEN);
glass.setSize(300, 150);
glass.setLocation(10, 10);
lp.add(panel, Integer.valueOf(1));
lp.add(glass, Integer.valueOf(2));
}
public static void main(String args[])
{
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Test();
}
});
}
}
If totally transparent, well, it is like it isn't here! When opaque, it just covers some of the GUI, but doesn't prevent mouse clicks, for example.
1) there are a few ways, there no issue to put JPanel, with covering full JFrames/JPanel area or only part of Rectangle / Dimension that returns JFrames/JPanel
use JLayer(Java7) based on JXLayer (Java6)
use GlassPane
use JViewport
use OverlayLayout
use transucent JDialog / JWindow
2) everything depends of if you want to protect against mouse and key events from the top layer to bottom, or not (to avoiding redispatch events from - to and vice versa)
Check out this tutorial on using Swing Root Panes.
The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.

Categories