Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 653

How to make a jlabel resizable according to the window

I am using netbeans to design a JFrame. enter image description here

now when i compile and run.It executes and opens a window enter image description here

now the problem is when i maximixe the window..I want the image or Jlabel to cover the width of the jframe. enter image description here

SO how can i do it.Plz Help

Upvotes: 1

Views: 2664

Answers (2)

camickr
camickr

Reputation: 324197

A JLabel always paints the Icon at it actual size.

If you want dynamic scaling of the image then you need to do custom painting. Basically you would override the paintComponent(..) method of a JComponent with code like:

super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

Of course you would need to pass the image as a parameter when you create the class. It also assumes you add the component directly to the content pane which uses a BorderLayout by default.

For fancier or more flexible solutions you could:

  1. Use the Background Panel
  2. Use Stretch Icon on a JLabel

Upvotes: 3

AlexR
AlexR

Reputation: 115388

First stop using visual designer. Stop now. Immediately. Forget that it exists. Visual designers create non-maintainable code. They save 5 minutes at the beginning and cause you to spend hours later.

Now, take a look on any java layouts tutorial. For example this one: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Once you read and understood it go back to your application. Think what layout(s) you need. Design the application. Implement it. If you still have problem that you cannot solve yourself within reasonable time ask more specific question again.

When you understand layouts good enough and want to implement real application with a lot of dialogs take a look on MigLayout.

Upvotes: 4

Related Questions