How can I add q menu bar to change the layout of a Jung graph (ie: StaticLayout, SpringLayout, etc)?
Infact this is what I already have:
JFrame frame = new JFrame("JUNG2 based GraphVisualization Tool");
// Create a graph
SparseMultigraph<MyNode, MyEdge> graph = new SparseMultigraph<MyNode, MyEdge>();
// We want to give the Nodes a point where to be (for later use)
//Map<MyNode, Point2D> vertexLocations = new HashMap<MyNode, Point2D>();
// Also we need a Layout
Layout<MyNode, MyEdge> layout = new StaticLayout(graph);
layout.setSize(new Dimension(600, 600));
// VisualizationViewer to Visualize our nodes and edges
VisualizationViewer<MyNode, MyEdge> vv = new VisualizationViewer<MyNode, MyEdge>(layout);
vv.setPreferredSize(new Dimension(650, 650));
// To show the vertex and EdgeLabels
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
// Our mouse should be usable in different modes
EditingModalGraphMouse mouse = new EditingModalGraphMouse(vv.getRenderContext(), MyNodeFactory.getInstance(), MyEdgeFactory.getInstance());
// Default values for new edges
MyEdgeFactory.setDefaultCapacity(100.0);
MyEdgeFactory.setDefaultWeight(5.0);
// Popupmenu
PopupNodeEdgeMenuMousePlugin nodeEdgePlugin = new PopupNodeEdgeMenuMousePlugin();
JPopupMenu nodeMenu = new MyMouseMenus.NodeMenu();
JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
nodeEdgePlugin.setNodePopup(nodeMenu);
nodeEdgePlugin.setEdgePopup(edgeMenu);
// The already existing popup editing plugin has to be removed
mouse.remove(mouse.getPopupEditingPlugin());
// And the new one has to be added
mouse.add(nodeEdgePlugin);
// set up the new mouse for the VisualizationViewer
vv.setGraphMouse(mouse);
// A JFrame to show all the stuff
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(vv);
// To change the mouse modes, the tutorial shows a menuBar. Think it would be nice to have a toolbar here!
JMenuBar menuBar = new JMenuBar();
JMenu modeMenu = mouse.getModeMenu();
modeMenu.setText("Mouse Mode");
modeMenu.setIcon(null);
modeMenu.setPreferredSize(new Dimension(80,20));
menuBar.add(modeMenu);
frame.setJMenuBar(menuBar);
mouse.setMode(ModalGraphMouse.Mode.EDITING);
frame.pack();
frame.setVisible(true);
Sorry I am new to java so it would be great if you suggest me what to do according to my code.Thanks
The fact that you have a Jung graph doesn't really change what you need to do.
1. Create a JMenuBar and attach it to the JFrame you need to menu on.
2. Add JMenuItems that either have Actions or ActionListeners tied to them.
3. Create a method or methods in your GUI that change the layout of the container with the Graph. (Might have to add/remove components or possibly just rebuild that part of the GUI completely).
4. Have the ActionListener call the appropriate method.
Related
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.
I have some experience in Java creating Apps and would like to learn more, and so have decided to create an application that will have different pages. For example the initial frame will show a menu of buttons that will lead to different frames, showing different components and layouts.
I'm not too sure the best practice of implementing pages. I think I could store the JFrame windows in a list, then use a button handler class to maybe change the visibility of the different frames, only allowing the relevant frame to be visible when the user clicks on a button. I think this method could work, but is there a more efficient/practical way of doing this?
I am aware of CardLayout, however for this program I am trying to learn MigLayout; so that won't be possible (as far as I'm aware). I hope this question is not too vague, I'd just like to know the best practice when it comes to creating applications in Java with different pages.
Can use Tabbed Panes, it is the best for storing pages.
https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
Also I noticed that you need to consider top level containers properly, because you don't need to create every time a JFrame for each Page, at least if it was necessary(For example: an editor, create a new window so you need to create a new JFrame, in your case I don't think so) so please consider the link below.
https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Internal frames with components example
Code to create multiple internal frames:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
// frame
static JFrame f;
// label to diaplay text
static JLabel l, l1;
// main class
public static void main(String[] args) {
// create a new frame
f = new JFrame("frame");
// set layout of frame
f.setLayout(new FlowLayout());
// create a internal frame
JInternalFrame in = new JInternalFrame("frame 1", true, true, true, true);
// create a internal frame
JInternalFrame in1 = new JInternalFrame("frame 2", true, true, true, true);
// create a Button
JButton b = new JButton("button");
JButton b1 = new JButton("button1");
// create a label to display text
l = new JLabel("This is a JInternal Frame no 1 ");
l1 = new JLabel("This is a JInternal Frame no 2 ");
// create a panel
JPanel p = new JPanel();
JPanel p1 = new JPanel();
// add label and button to panel
p.add(l);
p.add(b);
p1.add(l1);
p1.add(b1);
// set visibility internal frame
in.setVisible(true);
in1.setVisible(true);
// add panel to internal frame
in.add(p);
in1.add(p1);
// add internal frame to frame
f.add(in);
f.add(in1);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
I have been searching for a while now, but couldnt find a solution so I have decided to ask here.
I am using Java Swing for my gui implementation of calculator. I have custom made layout(which works correctly 100%). I have added all buttons and all buttons are positioned correctly, always. Last component I have inserted is "Inv" and it is checkbox which I cant find a way to center it inside its area. I have tried putting it in panel,in panel with borderlayout.center, setting the horizontal and vertical text alignment, but nothing works.
invert = new JCheckBox("Inv");
invert.setBackground(Color.decode("#8DA336"));
invert.addActionListener(new CommandListener(this,"invert"));
container.add(invert, new RCPosition(5, 7));
This RCPosition is nothing more than object which says in which row and column this component is (nothing wrong with that).
Checkbox is by default left-aligned. Try make it center-aligned:
invert = new JCheckBox("Inv");
invert.setHorizontalAlignment(SwingConstants.CENTER);
// styling and add to container
If it don't help, then you should publish your layout manager.
You could try putting it in a JPanel with BoxLayout, then add horizontal glue on the left and right.
final JFrame frame = new JFrame();
final JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
jp.add(Box.createHorizontalGlue());
final JCheckBox jcb = new JCheckBox("inv");
jp.add(jcb);
jp.add(Box.createHorizontalGlue());
frame.getContentPane().add(jp);
frame.pack();
frame.setLocationRelativeTo(null);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
This is just one way to do it, setHorizontalAlignment should work as well.
I'm learning GUI programming in java, and trying to modify an existing programme to add a new menu bar at the top of the frame.
The main method is below. The MainPanel class extends JPanel and contains the main components of the programme (a basic game).
public static void main(String[] args) {
JFrame frame = new JFrame("Sokuban");
MainPanel panel = new MainPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
I'm not sure if I should add a new JPanel, add it to the JFrame, and then within that add buttons? Or create a JMenuBar, within the existing panel, or frame, and then use BorderLayout.NORTH to arrange it?
Just playing around with things I found on google, I've tried the following snippets separately (haven't put all code in):
JMenuBar menuBar = new JMenuBar();
frame.add(new Button("Button"), BorderLayout.SOUTH);
panel.BorderLayout.SOUTH;
JPanel frame2 = new JPanel();
window.add(frame2, BorderLayout.NORTH);
JButton b1 = new JButton();
frame2.setSize(500,500);
b1.setSize(400,400);
b1.setVisible(true);
b1.setText("Button");
frame2.add(b1);
frame2.setVisible(true);
I can't figure out which direction I should be going in. Any pointers v much appreciated!
First : https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html :)
This is quite easy tutorial of JMenuBar. :)
And if you one want to only a button on JBarMenu :
How to make a JMenu have Button behaviour in a JMenuBar
I'm not sure if I should add a new JPanel, add it to the JFrame, and then within that add buttons? Or create a JMenuBar, within the existing panel, or frame, and then use BorderLayout.NORTH to arrange it?
Here are some personal experience that I can share with you:
I will usually plan the appearance of my user interface first (Either on paper on in my mind). After that, decide a suitable layout manager for the container.
I can have nested panels with more than 1 layout if the UI is complicated.
But ultimately, I will usually have a main panel which hold every other components (subpanels / buttons / textFields..).
Add the main panel into the JFrame. (You can have a customized JPanel, and we rarely need a customized JFrame).
As for the menu bar:
Add JMenuItem to JMenu
Add JMenu to JMenuBar
Add JMenuBar to the Frame using frame.setJMenuBar(menuBar);
The following image should help you in understanding of the hierarchy:
Don't do that, check this out it's what you're looking for
This link is for a JMenuBar
https://docs.oracle.com/javase/7/docs/api/java/awt/MenuBar.html
This link is for a JMenu https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html
This link is for a JMenuItem
https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenuItem.html
You don't need a new JFrame to create a menu, you can use the JMenuBar();
JMenuBar myMenu = new JMenuBar();
//The above snippet does not create or add menus it's simply the container that holds them
JMenu fileMenu = new JMenu("File"); // This will create a menu named file
JMenuItem openChoice = new JMenuItem("Open"); /* This will create an option under
the fileMenu named open*/
//To Actually add these things you would do this
setJMenuBar(myMenu);
myMenu.add(fileMenu); // This adds fileMenu to the menubar
fileMenu.add(openChoice); // This adds openChocie to the JMenu named fileOption
Now the above code was a very basic example as to how to setup a menu I would sugesst following the code I outlined here and as you learn to improve upon this as this is just a starting point for you!
I am new to Java and especially new to GUI and it's super confusing to me right now.
I'm making a program for class that is supposed to have a menu (JComboBox I'm assuming) that opens a new window when an option is selected. I am just working on the first option where you click "The Matrix" and a new window pops up with two buttons called "Red Pill" & "Blue Pill" and thats where I've hit a wall.
I got to the point where I am able to create a new window (not sure if this is even the right route to take for opening the new window) but, When I try to add Buttons to the new pop up window nothing shows up....
Thanks for any help or pointers in the right direction!
public class MultiForm extends JFrame{
private JComboBox menu;
private JButton bluePill;
private JButton redPill;
private static String[] fileName = {"", "The Matrix", "Another Option"};
public MultiForm() {
super("Multi Form Program");
setLayout(new FlowLayout());
menu = new JComboBox(fileName);
add(menu);
TheHandler handler = new TheHandler();
menu.addActionListener(handler);
}
private class TheHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
********************************************************************
//Create a new window when "The Matrix" is clicked in the JCB
JFrame newFrame = new JFrame();
JPanel panel = new JPanel();
newFrame.setLayout(new FlowLayout());
newFrame.setSize(500, 300);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
bluePill = new JButton("Blue Pill", bp);
newFrame.add(bluePill);
Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
redPill = new JButton("Red Pill", rp);
newFrame.add(redPill);
add(panel);
newFrame.setVisible(true);
}
}
public static void main(String[] args) {
MultiForm go = new MultiForm();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}
I tried doing newFrame.add(BluePill) and it created a button that was the size of the entire window and it would not allow me to add both buttons that way
That's because the frame uses a BorderLayout by default. Unless you specify otherwise, the component's will be added to the CENTER position, BUT, BorderLayout will only allow a single component to be managed at each of the it's five available positions, so you are only seeing the last component you added.
See How to Use BorderLayout for more details
so I figured that wasn't the correct way
It's the right approach, you just need to use a layout manager which can accommodate more components or change the position which you are adding the buttons
In this little example, I've just use a FlowLayout, but you can use what ever is going to give you the effect you desire
JFrame newFrame = new JFrame();
newFrame.setLayout(new FlowLayout());
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
bluePill = new JButton("Blue Pill");
newFrame.add(bluePill);
redPill = new JButton("Red Pill");
newFrame.add(redPill);
newFrame.pack();
newFrame.setVisible(true);
As a general rule of thumb, I don't like adding components like this directly to a top level container, I prefer to use a intermediate container, like a JPanel, this gives me more possibilities for re-use, but that's me.
You should also only make the frame visible when it's actually ready, otherwise you may find that some times, the components won't show up
See Laying Out Components Within a Container for more details
You are not using the getContentPane() method from the new JFrame.
You have to actually use getContentPane() first because you're not adding any component to the JFrame itself but to an intermediate "panel".
JFrame newFrame = new JFrame();
JPanel panel = new JPanel();
newFrame.setSize(300, 200);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
bluePill = new JButton("Blue Pill");
panel.add(bluePill);
redPill = new JButton("Red Pill");
panel.add(redPill);
newFrame.getContentPane().add(panel);
newFrame.setVisible(true);
You'll have to add a Layout to the JPanel or/and the JFrame and play with the sizes of the component but with this you're on the right path.
I always put the setVisible method a the end, after adding all the components to the frame.
You made some mistakes.
add(bluePill);
will not do what you want, even if it would, it would still be wrong.
(sounds weird, but I'll explain it)
First the "right" way to do it:
//Create a new window when "The Matrix" is clicked in the JCB
JFrame newFrame = new JFrame();
newFrame.setSize(300, 200);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
bluePill = new JButton("Blue Pill");
newFrame.getContentPane().add(bluePill);
redPill = new JButton("Red Pill");
newFrame.getContentPane().add(redPill);
newFrame.setVisible(true);
Notice I added "newFrame", because you were calling the method of MultiForm.
That's because "add()" is the same as "this.add()" and "this" points to MultiForm. Check it with this line if you want:
System.out.println(this.toString());
The "getContentPane()" is best explained with this image:
You were adding it directly to the JFrame (I don't even know what exactly happens then).
It is also good practice to set the frame visible when it is ready to be visible. Your frame did not contain anything when you made it visible.
Now to the JPanel. A JPanel can hold some elements like JButton,etc. and it can also have a layout. Since you didn't use the JPanel at all, i removed the line from your code. You can still add the JPanel to your ContentPane and add a Layout to the JPanel. (You can also add JPanels to JPanels to create complex layouts)
I hope this was clear for you.