Reputation: 9446
I am trying to put 2 JPanels in borderLayout. Such that, one JPanel will be of size pane1(600,600)and the other one would be pane2(200,600). I am hoping to pack them such that the big one would be on the left and other one would be on right. I am setting the size of each jpane but it looks like both of them occupy the complete space and kind of overlap on each other.
I am a complete newbie here and have no clue what is going wrong. Any help is appreciated.
JFrame frame = new JFrame("Simple Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
int pane1_width = FRAME_WIDTH-200;//800-200
Pane1 myPlots = new Pane1(graph_panel_size, FRAME_HEIGHT);
frame.add(myPlots);
Pane2 simpleInfo = new Pane2(200,FRAME_HEIGHT);
frame.add(simpleInfo);
frame.pack();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
This is a base frame class. For JPanel
public Pane2(int width,int height){
this.setSize(width,height);
Border blackline = BorderFactory.createTitledBorder("ola");
this.setBorder(blackline);
The other panel also has similar constructor. But both the Jpanels overlap each other and I cant place them separately.
Upvotes: 0
Views: 163
Reputation: 47637
The problem is that you are calling setSize()
. When using LayoutManager
's (which you should absolutely always do), using setSize/setBounds/setLocation
is completely useless.
Additionnaly, calling setPreferredSize()/setMinimumSize/setMaximumSize
is not recommended and can be counterproductive.
Either your component have a good reason to have a given size (because you are doing custom painting, for example), then you should override getPreferredSize
, or you simply don't have to do anything, and only use an appropriate LayoutManager
.
You can also check out the tutorial on LayoutManager of Oracle
See this example with overrides of getPreferredSize()
:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestCustomPainting {
private static class MyTriangle extends JPanel {
private final int width;
private final int height;
public MyTriangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillPolygon(new int[] { 0, getWidth(), 0 }, new int[] { 0, 0, getHeight() }, 3);
}
}
protected static void initUI() {
JFrame frame = new JFrame("test");
frame.add(new MyTriangle(200, 45), BorderLayout.EAST);
frame.add(new MyTriangle(85, 600), BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}
NB: I must admit, there are more interesting to paint :-)
Upvotes: 4