I'm a novice Java programmer, and I'm trying to work out the behaviour of BorderLayout in a particular situation.
Say you have:
JFrame frame = new JFrame();
frame.add(new JLabel("Test"));
The defualt layout manager for JFrame is BorderLayout. According to the Java tutorial for BorderLayout, a position must always be specified, e.g. by:
frame.add(new JLabel("Test"),BorderLayout.CENTER);
So I'm confused by the fact that it is possible to add a component without specifying a position. If I modify the above code to be:
JFrame frame = new JFrame();
frame.add(new JLabel("Test"));
frame.add(new JLabel("Test 2"));
frame.add(new JLabel("Test 3",BorderLayout.NORTH);
I get Test 2 being displayed in the center left of the screen, and Test 3 being displayed in the top left of the screen.
Am I right in my understanding that, if no position is specified, BorderLayout will just default to BorderLayout.CENTER and, if so, can anyone show me where this is documented? I'm sure it must be documented somewhere, but I can't find it anywhere!
Thanks
http://download.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html
"As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER"
Related
i made a custom JFrame for the desktop application and i added a JPanel on the very top of the app to serve as a subtitute of the title box. the problem is when i added a button it located right in the middle of the JPanel instead of the usual left top. AND it would not move even if i set it at a different location.
here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
btn.setBackground(Color.white);
btn.setText("Button");
btn.setSize(300, 80);
btn.setLocation(50, 0);
pa.setBackground(Color.red);
pa.setPreferredSize(new Dimension(width,60));
pa.add(btn);
f.setBackground(Color.white);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(width,height));
f.setLocation(200, 200);
f.setVisible(true);
You use a BorderLayout in the frame. You can do the same thing in the panel.
pa.setLayout(new BorderLayout());
pa.add(btn, BorderLayout.WEST);
In general, setLocation tends to fight against the layout manager, so you usually don't want to use it unless you're going to position everything by hand.
that is one way to do it, but BorderLayout way is not very good way because i also want to add another button next to it.
Then what this might need is a FlowLayout using FlowLayout.LEADING as the alignment.
But as general tips:
Provide ASCII art or a simple drawing of the intended layout of the GUI (showing all components) at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
For better help sooner, post a
Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example of your attempt.
Hey everyone I've been using this as my guide and the base for my code that I've been working on:
Java Source
What I want to do, is add a shared button across all of the panes. I dont want to declare a unique button for each one, but one that is shared. My first thought was to change the frame to boxlayout and just toss a button in after it adds the pane to the frame:
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
//Add content to the window.
//frame.add(new GUI(), BorderLayout.CENTER);
frame.add(new GUI());
//setup Find button
//findButton.setSize(110,55);
findButton.setText("Find");
findButton.setVisible(true);
//add button to frame
frame.add(findButton);
However, I get a runtime error: BoxLayout can't be shared. So now I wind up here. While I look into why I'm getting this error, can someone let me know if this is the right approach?
Suggestions:
Consider placing the JButton in a JPanel that is below or above the JTabbedPane so that it is always visible, and you will only need one button.
Or if it must be in a component held in the tabs, then each will need its own unique JButton, but they can share the same Action, which is what I recommend you do: Create an inner private class that extends AbstractAction, create an instance of this inner class, pass it into each JButton via either the JButton's constructor or its setAction(...) method.
Your BoxLayout problem is completely unrelated to your original question and should not even be part of this discussion. Yes, a BoxLayout must be used in one container, and that same container should be passed into the BoxLayout. Likely you're adding it to the JFrame, but in reality this adds it to the JFrame's contentPane, and so for this to work, you must pass frame.getContentPane() into the BoxLayout's first constructor parameter:
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
This is one reason I don't like adding components or setting layouts directly on the top level window since it is nothing but misleading syntactic sugar.
I much prefer:
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
I have reminder class extending JPanel, with boxlayout and
setPreferredSize(new Dimension(500,500))
In the mainclass I have JFrame
rem = new Reminder();
frame.setSize(900,900);
rem.setMaximumSize(new Dimension(500,500));
frame.getContentPane().removeAll();
frame.setLayout(new BorderLayout());
frame.add(rem,BorderLayout.CENTRE);
frame.validate();
frame.repaint();
And I want the reminder panel in the centre of the jframe without changing its size(500,500) and frame should not change it size(900,900). When I run the above code, rem panel is totally expanded. How to make the rem panel in centre?
Easy way: use AbsoluteLayout and position the "rem" exactly where you want with a graphical editor.
Then set invariable size for frame:
frame.setResizable(false);
Do not use a BorderLayout. Why do you use a BorderLayout if it is exactly what you don't want to have?
Edit: BoxLayout is one choice, if you really need to position everything absolutely yourself, you can also decide to use no layout manager, e.g. http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Also if you wanna use a Borderlayout I'm pretty sure it's a typo in your code:
frame.add(rem,BorderLayout.CENTRE);
should be:
frame.add(rem,BorderLayout.CENTER);
Could it be this small thing that's bothering you?
I'm trying to get multiple jfreecharts displaying within a single window. Apparently this is not possible with the included ChartFrame so I attempted to add multiple copies of the same chart to a JFrame which didn't work. Any ideas?
JFrame frame = new JFrame("Chart");
frame.getContentPane().add(new ChartPanel(chart1));
frame.getContentPane().add(new ChartPanel(chart2));
frame.pack();
frame.setVisible(true);
With this code, I only get one chart in the JFrame.
EDIT:
I added another data set and chart but it still only displays one of them.
Cause of your problem is layout of frame.getContentPane().
Default layout at the JFrame content pane - BorderLayout. Read more about BorderLayout here.
This operation
frame.getContentPane().add(new ChartPanel(chart));
equals
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.CENTER);
and add ChartPanel to the CENTER area of the content pane. Second ChartPanel you add to CENTER area too. Then you add two component to the same area the last added one hides all previous added. Thus second ChartPanel hides first one.
You need change layout for frame.getContentPane().
You may use the same chart to two ChartPanel.
Try to change you code to
JFrame frame = new JFrame("Chart");
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.WEST);
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
It is not code for production. It just example to show two ChartPanel on the frame.
You need to have different chart instances. If you use the same reference, the last one that is added to a ChartPanel will be displayed.
I have a question.
I have one main frame, and to the left i have a sidebar with menus.
My question is, is it possible to make another panel within the main frame,
so that if menu1 is clicked, the related contents should be displayed to the second half of the main frame, and when other menus are pressed then obviously the relevant stuff according to what is selected. its a bit hard to explain, sorry. Has anyone got an idea, whether that is possible in Java with Eclipse?
yes this's pretty possible you have look at CardLayout, this LayoutManager could be provide the simple way how to implement switching betweens JPanel in the JFrame
Yes, you can add 2 JPanels to 1 frame.
JFrame frame = new JFrame();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);