what is the purpose of JFrame setBackground - java

When you create JFrame instance, you have setBackground method available from this instance. However, regardless to what color you try to put there, you`ll receive gray background color.
This happens (as i understood), because default JPanel instance is automatically created inside JFrame and lays over it . So, to get color set, you need to call
JFrame.getContentPane().setBackground(Color.RED);
that actually calls setBackground of default JPanel , that exists inside JFrame.
I also tried to do next :
JFrame jf = new JFrame();
//I expect this will set size of JFrame and JPanel
jf.setSize(300, 500);
//I expect this to color JFrame background yellow
jf.setBackground(Color.yellow);
//I expect this to shrink default JPanel to 100 pixels high,
//so 400 pixels of JFrame should became visible
jf.getContentPane().setSize(300, 100);
//This will make JPanel red
jf.getContentPane().setBackground(Color.RED);
After this set of code, I am having solid red square of JFrame size , i.e. 300 x 500.
Questions :
Why jf.getContentPane().setSize(300, 100); does not resize default JPanel , revealing JFrame background?
Why JFrame has setBackground method if anyway you cannot see it and it is covered by default JPanel all the time?

As per the class hierarchies of JFrame as shown below:
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
The method Frame#setBackground() is inherited from Frame and JFrame doesn't do override it.
What JFrame states:
The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case.
You can override default setBackground() of JFrame as shown below:
#Override
public void setBackground(Color color){
super.setBackground(color);
getContentPane().setBackground(color);
}

Related

add image in Jpanel using canvas

i want to add image to my panel which is created by Netbeans GUI Designer.
here's my canvas class
private static class GraphicsClass extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Toolkit tkit;
tkit=getToolkit();
Image img=tkit.getImage("ampera.jpg");
g.drawImage(img,0,0,this);
}
}
and this is how i use it in JFrame class constructor
this.Jpanel1=new GraphicsClass();
but the image doesnt show. How to fix it?
this.Jpanel1=new GraphicsClass();
Won't work. Assuming you just drag and dropped a panel onto the frame and named it Jpanel1, then in the constructor you did the above code. When you first drag and drop the original panel, it gets initialized and laid out. When you do the above code, you are making a new component. The original panel is still the one added, and is not the same referenced panel and the new one you just created. So the new panel will never show, it is just the one (with no reference identifier). Like if you has something like
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
jPanel1 = new GraphicsClass();
}
private void initComponent() {
jPanel1 = new JPanel();
// add jPanel1 to frame
}
private class GraphicsClass extends JPanel {}
private JPanel jpanel1;
}
That's the basic netbeans setup. The code in the initComponents method is autogenerated and is not editable by default. You could edit it, but I would advise against it (unless you really know what you're doing). If you look at the code, you will see exactly what I wan talking about in the beginning.
Fix: Created a panel form from the designer (i.e. New->Swing Form->JPanel form). Then you can simply drag and drop the panel to the frame, as seen here
Other note:
Don't create your images in the paintComponent method. Make it a class member and create it in the constructor.
Instead of a panel, if you want to create an image (say for a background), you can use an ImageIcon and a JLabel and set the layout manager to the label, You can then start adding component onto the label.
Drag and drop an label
Go to the properties of the label and click the ... of the icon property. In the dialog, you can browse for an image.
Then right click the label from the design view, and set a layout manager for the label. You can then start adding components to the label.
You can fix this by using the BufferedImage class, or using the MediaTracker to track when your image has loaded

JPanel filling entire JFrame

I am writing a simple game withing which I am using a JFrame which contains a grid and a JPanel.
Here is my pseudo code:
void MyJframeConstructor()
{
// some basic bootstrap logic
// calling repaint to draw grid
repaint();
// Grid is drawn fine.
// Showing user a confirm dialog box on which I add below JPanel.
if(confirmed)
{
// GameInfoPanel extends JPanel.
infoPanel = new GameInfoPanel(new FlowLayout(FlowLayout.LEFT));
infoPanel.setPreferredSize(new Dimension(400, 100));
infoPanel.setLocation(500, 50);
this.add(infoPanel);
infoPanel.validate();
}
}
My problem here is my JFrame or window is 480 x 680.
Within this I am drawing a grid in 480 x 480 area.
Below which I want the JPanel to be located at 500,50 with dimension 400, 100.
However, when I run this code, once the user confirms with OK, the JPanel fills up the entire JFrame.
How can I keep the panel in its location and consistent in size through out the life of the app ?
Any help is highly appreciated and thanks in advance.
Within this I am drawing a grid in 480 x 480 area.
override PreferredSize for JPanel
then call JFrame.pack() and JFrame.setVisible(true) as last code lines
have to read InitialThread
if is there only one JPanel (JPanel filling entire JFrame) then to use built_in BorderLayout in JFrame f.e. myFrame.add(infoPanel, BorderLayout.CENTER) not FlowLayout
don't to extend JFrame create this Object as local variable

JFrame execution time adding JPanel

I'm working with a JFrame adding JPanel instances dynamically in the following way:
private void addBox(int x, int y){
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
panel.setSize(10, 10);
panel.setVisible(true);
panel.setLocation(x, y);
this.getContentPane().add(panel);
}
The problem is, when I use addBox method, the JPanel instance does not appear in the JFrame. The only way I can see the box I need to manualy resize the window.
Note: I tried using this.pack();, but this did not work.
You need to call revalidate() and repaint() after such structural changes to the GUI.
Note that setSize and setLocation should preferrably be handled by the layout manager.
Related link:
jGuru: What is the difference between repaint() and revalidate() in Swing components?
What are the purpose of the boxes?
If they are purely visual, and you don't intend to add components to them, it would be better to define a Box class (or use a Rectangle2D) and draw or fill them at time of paintComponent().
Alternately, draw them to the Graphics object of a BufferedImage and add the image to a JLabel, as shown here.
This example showing add/remove/pack may help.
private void addBox(int x, int y){
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
add(panel);
//If there isn't another JPanel, then this way you'll occupy
//the whole JFrame area; by defalut, JFrame has BorderLayout,
//and only one JComponent can occupy the central area
revalidate();
repaint();
}

How can I Change the default Icon of a JPanel based swing app (or window)

I've seen many answers for this question when it's a JFrame, but none for JPanel, and all that I've tried didn't work.
So basically I've written this simple class/app that extends JPanel, and all is working fine. Now I'd like to change the Default Icon.
Any ideas?
Just as guys are saying here in comments please reconsider what you are trying to do.
The only option to change an icon is to set it for the frame in which the panel is child, since the icon is a part/belongs to the frame.
If you want setting of the icon to be a functionality of a panel then in addNotify() method, which is called when a component receives a parent, look through the panel's parent and it parent and so on until you will reach the frame and set the icon for it.
Sample showing a number of parent you must go through to get to frame if a panel is its content pane.
JPanel p = new JPanel();
JFrame f = new JFrame();
f.setContentPane(p);
System.out.println(SwingUtilities.windowForComponent(p));

Can I use printComponent without having to draw component to screen first?

I have been using the printComponent that was shown in another question. It works great for printing a JPanel if I first add that JPanel to a JFrame and draw that JFrame to screen. But if I don't do that before I print, I get a blank page printed. Why is this?
I've used code like the following to create a BufferedImage on a panel that is not visible on the frame:
JPanel panel = new JPanel();
... // add components
panel.setSize(300, 300);
panel.doLayout();
that is because the panel you wish to draw has an initial size of 0,0. Once added to a container with a layout manager and is displayed, then it gets its "normal" size.

Categories

Resources