How to set coordinates for components? - java

I'm a new user on Swing, and I have problem with drawing components by coordinates. Please, look at this code:
JFrame frame=new JFrame();
frame.setBounds(new Rectangle(0, 0, 700, 600));
frame.getContentPane().setBackground(Color.yellow);
frame.setVisible(true);
JPanel graph=new JPanel();
graph.setBounds(new Rectangle(0, 0, 700, 300));
graph.setBackground(Color.white);
graph.setOpaque(true);
frame.getContentPane().add(graph);
I need that JPanel closes 50% from JFrame, but now it closes 100%, and JFrame has white color for background. How should I fix the mistake? Also, is there any mean for setting width and height using percents? For example, 50% for width. Or may be exists any containers for my question? Thank you

don't use whatever#setBound(), use Standard LayoutManager, in this case is GridLayout(2, 0) best of ways how to do it, your JFrame and its JPanels will be resizable on both directions

If you really want absolute positioning you could try disabling the default layout manager, as shown in Doing Without a Layout Manager. However, most of the time it's best to (depending what your container is intended to show) choose an appropriate layout manager.

Related

Can I add a rectangle to a Jpanel in java swing and specify constraints for it using GridBagConstraints?

I'm pretty new to GUI and Java as a whole so I hope that I can explain this well enough and understand people's answers.
For a school project, I need to put a bunch of stuff on some rectangles but I'm having issues even adding one rectangle properly.
From researching online, this is what I have (the JPanel and GridBagConstraints are just there to show what I'd like to use):
public class GUI extends JPanel
{
public static void main (String [] args)
{
GUI g = new GUI();
JFrame window = new JFrame("Java Window");
window.setSize(1280, 960);
window.add(g);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel layout = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
}
#Override
public void paintComponent(Graphics g)
{
Color boxColour = new Color(194, 190, 190);
super.paintComponent(g);
g.setColor(boxColour);
g.fillRect(10, 10, 100, 100);
}
}
So right now, the rectangle appears in the window. But how can I add constraints to it? Is that possible? I would think that I should use JPanel to keep everything more organized since there will be many components so I tried adding this:
layout.add(g);
window.add(layout);
window.setVisible(true);
However, the rectangle no longer appeared. What am I doing wrong and how can I fix it and add constraints to my shapes? Thanks!
In your first scenario, the default layout manager of the frame is the BorderLayout and you are adding your "g" panel to the BorderLayout.CENTER. So based on the rules of the BorderLayout your "g" panel will take up all the space available in the frame. So you have plenty of space to paint your rectangle.
However, in your second scenario, your "layout" panel is using the default layout manager of a JPanel which is a FlowLayout which respects the preferred size of any component added to it.
The preferred size of the "g" panel is 10 x 10. So, when you add the "g" panel to the "layout" panel there is nothing to see because all you custom painting is done outside the bounds of the panel.
You need to override the getPreferredSize() method of your GUI panel to return a preferred size of (120, 120) so you can see your rectangle painted with a 10 pixel border around all the edges.
Read the section from the Swing tutorial on Custom Painting for more information and working examples demonstrating how to override the `getPreferredSize() method.
You will also need to read the Swing tutorial on How to Use GridBagLayout for example of using the constraints to add multiple components.

How to position something on a certain point on a JPanel?

Right now I have the following code which adds the JLabel to the top center of the Panel, which I assume is the default
imageLabel = new JLabel();
ImageIcon customer1 = new ImageIcon("src/view/images/crab.png");
imageLabel.setIcon(customer1);
storePanel.add(imageLabel);
imageLabel.setBounds(20, 20, 50, 50);
setBounds obviously isn't putting it at 20,20....so how do you position something to a point within a Panel?
Use the appropriate LayoutManager to place components in the panel.
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
In your case you should be able to use a FlowLayout and set the horizontal and vertical gaps when you create it.
http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html#FlowLayout(int,%20int,%20int)
Seems your storePanel is JPanel and has default FlowLayout manager, because of your setBounds(20, 20, 50, 50); doesn't work. It will be work with null layout (storePanel.setLayout(null);).
But I recommend you to use LayoutManager.
If you don't mind a bit of manual work you can add constraints to your label using a SpringLayout. This allows you to position edges an exact distance from other edges, which by default also sorts the components size (By basically setting the edges a set distance apart when you lay it out) I have demonstrated below with a textArea, but is could easily apply to your label as well.
public class SO {
public static void main(String[] args) {
//Components
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(frame.getSize());
JTextArea text = new JTextArea();
//Add components
panel.add(text);
frame.add(panel);
//Layout add & setup
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
layout.putConstraint(SpringLayout.WEST, text, 10, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, text, 10, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.EAST, text, -10, SpringLayout.EAST, panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
Although not recommended, you can do absolute positioning if you set your layout manager to null.
storePanel.setLayout(null);
// imageLabel initialization code
storePanel.add(imageLabel);
imageLabel.setBounds(20, 20, 50, 50);
Oracle Documentation
My advice is to use a Good IDE + UI Builder combo such as:
Netbeans GUI Builder
Eclipse WindowBuilder
IntelliJ GUI Designer
Thease are WYSIWYG tools that can generate Swing code using flexible Layout Managers such as Group Layout or JGoodies Form Layout.
A layout manager is a must if you want to design good UIs. They not only handle the size and positioning of components, but things such as redistributing / repositioning / resizing components on window resize (which is really hard to get right by hand). Also, those UI designers can hint you so that you stick to the guidelines and best practices in order to design high quality / cross-platform UI.

JPanel Format Problems

I have a 2 JPanels, 1 a button Panel and one a Graphic Panel. I would like the button panel to situated right below the graphic panel but the button panel cuts off the Graphics Panel in the middle. I've been trying the box layout which seems from discussions seems like the best format for what I am trying to do. Can anyone please give me some advice on my formatting problem.
JFrame canvas = new JFrame("Baseball Strike K");
JFrame canvas = new JFrame ("GraphicBoard");
canvas.setVisible(true);
canvas.setSize(1000,1000);
canvas.setDefaultCloseOperation(EXIT_ON_CLOSE);
//create two panels
//add them to contentPane
//set Layout
JPanel buttonPanel = createButtons();
JPanel mainPanel = new Graphic(); //extends JPanel and writes the paint method
mainPanel.setSize(1000, 1000);
Container content = canvas.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(mainPanel);
content.add(buttonPanel);
mainPanel.setSize(1000, 1000);
The job of the layout manager is to determine the size of the component, so you would never invoke the setSize() method of a components.
Instead you give hints to the layout manager on what the size should be. You would do this by overriding the getPreferredSize() method to return an appropriate value. Also, I would pick a more reasonable size (1000, 1000) is a little big to display on most screens. If you really want your painting area this large then I would add the paint panel to a JScrollPane and then add the scrollpane to the frame.
Try getting your code to work using a BoxLayout. Then I would suggest a better layout manager would be to use a BorderLayout. Then you add the paint panel to the CENTER and the buttons to the SOUTH. Now as you resize the frame the paint panel will be adjusted in size.
canvas.setVisible(true);
Also, the placement of that line of code is wrong. You should add all your components to the frame first, before making the frame visible.

Java Swing JPanel Sizes

I would like to be able to have a JPanel within my JFrame of a fixed size 400x400.
I would also like the to be a 20px wide border all around it.
The main problem is the following code doesnt stick it its size.` JScrollPane runningAni = new JScrollPane(new views.cRunningAnimation(
model));
runningAni.setMaximumSize(new Dimension(400,400));
this.setSize(new Dimension(600,600));
this.add(runningAni,BorderLayout.CENTER);`
When doing this the runningAni panel just strethces accross the whole frame.
public void paint(Graphics g) {
this.setBackground(new Color(0,255,0));
}
I know this because my full frame paints itself green rather than just the JPanel (The above paint code is for my panel not the frame)
How would i create the panel so it always stays the same size and so there is always a 20px colored border around it?
BorderLayout ignores the size. You need to set a LayoutManager that either allows you to set the size to a fixed size or one that cares for the sizes set. There are different layout managers that allow this (e.g. GrindBagLayout or no layout manager at all). Some are not that easy to use (e.g. GridBagLayout). What to use depends on the rest of the layout.
You could probably use a layout panel that contains your custom panel. The layout panel needs an appropriate layout manager and could be put into the center of the BorderLayout. This would mean nearly no modifications to existing layout code.
The whole point of BorderLayout is to fill the center with the center component.
Don't override the paint() method to set the color of the panel. Use:
panel.setBackground(...);
When you create the panel.
How would i be able to set a border around my Jpanel
See How to Use Borders.
Just set your layout to null, to what ever class your adding your JPanel.
Then use the setBounds() method to set your location and size!
For example:
public class Main extends JFrame{
YourPanelClass panel = new YourPanelClass();
public Main(){
// I didn't want to put all the, everyday JFrame methods...
setLayout(null);
/*
First two coordinates indicate the location of JPanel inside JFrame.
The seconds set of coordinates set the size of your JPanel.
(The first two coordinates, 0 and 0, tell the JPanel to start at the
top left of your JFrame.)
*/
panel.setBounds(0, 0, 100, 100);
add(panel);
}
}
And i would GREATLY recommend using the paintComponent() method.
For instance:
(Obviously you put this in your JPanel's class.)
public void paintComponent(Graphics g){
super.paintComponent(g); // don't forget this if you are going to use this method.
//Basically this makes your JPanel's background green(I did it this way because I like doing it this way better.)
g.setColor(new Color(0, 255, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}
Please don't forget to thumbs up if this helped!
setPreferredSize()
setMinimumSize()
setMaximumSize()
should do the trick

Using JSplitPane with an AWT component

I have an AWT canvas which I cannot convert to a Swing component (it comes from VTK). I wish to display a few of these canvases inside of a JSplitPane. I've read about mixing heavy and light weight components in Java and know that it's a pain in the butt, but I don't have a choice. If I wrap the AWT canvas inside of a JPanel and then put that on the split pane the split pane doesn't function at all. However, if I put the AWT canvas inside of a JPanel and then that inside of a JScrollPane and then those scroll panes on the JSplitPane the split pane does function, but the AWT canvas components don't resize properly. I'm lost about how to get the AWT canvas components to resize properly when the JSplitPane's divider is moved. I can catch the divider moving operation and operate on the AWT canvases at that time, but I don't know what to do. I've tried calling invalidate() then validate() then repaint(), but that didn't work.
Any ideas?
Here's a example of the problem
import javax.swing.*;
import java.awt.*;
public class SwingAWTError {
public static void main(String[] args) {
Canvas leftCanvas = new Canvas();
Canvas rightCanvas = new Canvas();
leftCanvas.setBackground(Color.RED);
rightCanvas.setBackground(Color.BLUE);
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new BorderLayout());
leftPanel.add(leftCanvas, BorderLayout.CENTER);
rightPanel.add(rightCanvas, BorderLayout.CENTER);
JScrollPane leftScroll = new JScrollPane();
JScrollPane rightScroll = new JScrollPane();
leftScroll.getViewport().add(leftPanel);
rightScroll.getViewport().add(rightPanel);
JSplitPane split = new JSplitPane();
split.setLeftComponent(leftScroll);
split.setRightComponent(rightScroll);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
It is a dirty way but this will solve it:
When you call pack() without resizing the window, not much happens. So, when you first resize the window and then call pack(), your components are correcly drawn. This means you can put this dirty method in your divider moved listener method:
frame.setPreferredSize(frame.getSize()); // store the current size to restore it after packing.
frame.setSize(frame.getWidth() + 1, frame.getHeight()); // resize it!!
frame.pack();
I don't know what it is exactly but it is a strange behavour in Java...
Hope this helps until you've found a better solution...
You are kind of out of luck here. There's a pretty good article on this on the sun/oracle website:
http://java.sun.com/products/jfc/tsc/articles/mixing/
Essentially it boils down to this guideline (taken from that link, under the z-ordering heading):
Do not mix lightweight (Swing) and
heavyweight (AWT) components within a
container where the lightweight
component is expected to overlap the
heavyweight one.
Edit: I kept browsing that site and came across another link, and it would appear that the situation has improved slightly: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/ But I think your case is one of those listed at the bottom in the limitations section:
Limitations
A few situations are not supported:
* Non-opaque lightweight components that have translucent
pixels (0 < alpha < 255) are not
supported. If a partially translucent
lightweight component overlaps a
heavyweight component, the heavyweight
component will not show through.
* Embedded heavyweight components must belong to the process that
created the frame or applet. The
heavyweight component must have a
valid peer within the main process of
the application (or applet).
* Advanced Swing key events, such as those events maintained in an
InputMap, might not work correctly
where lightweight and heavyweight
components are being mixed. There are
no known workarounds.

Categories

Resources