Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:59:13+00:00 2026-06-16T16:59:13+00:00

Below is a simple swing app in which i was experimenting certain custom techniques.The

  • 0

Below is a simple swing app in which i was experimenting certain custom techniques.The code is as follows :-

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class ThemeComponents extends JFrame{
  public static void main(String args[])
  {
    SwingUtilities.invokeLater(new Runnable(){public void run(){new    ThemeComponents();}});
  }

  public ThemeComponents()
  {
    super("HACK 1:Creating Image Themed Components ");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    CustomPanel p1=new CustomPanel();
    p1.add(new CustomLabel());
    add(p1);
    pack();
    setVisible(true);
  }
}

class CustomPanel extends JPanel
{
  BufferedImage img;
  CustomPanel()
  {
    try
    {
      img=ImageIO.read(new File("src/background.jpg"));
    } catch(IOException e){
      System.out.println("Error in loading background image "+e);
    }
  }
  public void paintComponent(Graphics g)
  {
    g.drawImage(img,0,0,getWidth(),getHeight(),null);
  }
  public Dimension getPreferredSize()
  {
    return new Dimension(img.getWidth(),img.getHeight());
  }
}

class CustomLabel extends JLabel
{
  ImageIcon img;
  CustomLabel ()
  {
    img=new ImageIcon("src/tornado.gif");


    setSize(img.getIconWidth(),getHeight());
    setIcon((Icon) img);
    //setOpaque(false);
    //setIconTextGap(0);
    setLocation(10,10);
  }
}

Now I have following questions :-

1)When i set the layout to null setLayout(null) in my main class ThemeComponents then why the frame size shrinks to null with only title bar ?I expected it to take the size of CustomPanel as i have used pack() for the frame.(using layouts such as flowlayout , borderlayout however produces correct output)

2)Is using getPreferredSize() better for setting the size of component instead of setPreferredSize().Actually i don’t find any difference between them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:59:14+00:00Added an answer on June 16, 2026 at 4:59 pm
    1. If you use null-layout, preferred size will return (0,0) and hence you will only see the title bar. pack() validates your JFrame and then sets the size of the JFrame to the preferred size of the content pane (which is 0,0) and add the required space for the title bar, menus, etc…

    2. Most likely you should avoid calling setPreferredSize() and rather override getPreferredSize(). Calling setPreferredSize() leaves the possibility to others to modify that value. In such situation it probably means that the preferred size is not an intrinsic part of your component and therefore you should not need to call setPreferredSize(). While overriding getPreferredSize() gives you full control and results in the preferred size being an intrinsic part of your component.

    3. You should also call super.paintComponent(g); in your CustomPanel.

    4. In your CustomLabel, it does not make any sense to call setLocation (the parent layout will change that anyway)

    5. In your CustomLabel, this does not make any sense either: setSize(img.getIconWidth(),getHeight()); since the parent layout will change those values anyway (and btw, getHeight() returns 0 in this case)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I'm writing a simple Swing app. I tried adding a checkbox as listed below.
I am trying simple java applet program to display tiff image as below import
The code pasted below is a simple JSF program, with an idea of having
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(
I am having trouble with some javascript. The code snippet below creates a simple
The simple code block below can be served up in a static HTML page
The below code is very simple. I have a jQuery autocomplete bound to an
Below is my code for a simple form in the create_session.php page. I am
I made a desktop app with the NetBeans platform using Java Swing technology. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.