This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to set this component? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set this component?" Watch "How to set this component?" New topic
Author

How to set this component?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
I was making the following program and found some problem in setting the JLabel Component i.e. background_image. What I was trying to have a container and in this conatiner I was trying to put this JLabel. And thought to make it right align I can use the following alogrithm:
FrameWidth-ImageWidth,FrameHeight-ImageHeight
But it push the image a bit down(about 60 pixels) though in X it is Ok. What could be the reason?
I also tried to find the menubar width and height and try to put in my alogrithm assuming that Panel will start after menubar instead of top, but in vain. Moreover on trying to find out width and height of menubar it is showing me (0,0) value.
Can you help me to set this?
regards,
Arun
Code:-
********
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.net.*;
import java.io.*;
public class test extends JFrame
implements ActionListener
{
JButton toolbutton;
JToolBar toolbar;
JLabel back;
boolean toolbar_flag = true;
makePanel mp=null;
public test()
{
JMenuBar menubar;
JMenu menu;
JMenuItem menuItem;
Containercont = getContentPane();
cont.setBackground(new Color(140,199,222));
cont.setFont(new Font("arial",Font.BOLD,11));
cont.setLayout(null);
menubar = new JMenuBar();
setJMenuBar(menubar);
mp = new makePanel();
//Build File Menu
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menubar.add(menu);
//Items in File Menu
menuItem = new JMenuItem("Change User/Login",KeyEvent.VK_C);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);

//adding separator
menu.addSeparator();
menuItem = new JMenuItem("Exit",KeyEvent.VK_X);
menuItem.addActionListener(this);
menu.add(menuItem);

//setting frame size
setSize(450,400);
//setting Background Image
//mp.setBounds(0,0,338,218);
int w = getWidth();
int h = getHeight();
mp.setBounds((w-338),(h-218),w,h);
cont.add(mp);
mp.main_cont();
System.out.println("Frame width: "+getWidth());
System.out.println("Frame height: "+getHeight());
System.out.println("Panel width: "+mp.getWidth());
System.out.println("Panel height: "+mp.getHeight());
System.out.println("Panel X: "+mp.getX());
System.out.println("Panel Y: "+mp.getY());
//setting windows listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Entered into ActionListener");
Object o = evt.getSource();
String s= ""+o;
if(s.indexOf("JMenuItem")>0)
{
s="";
JMenuItem source = (JMenuItem)(evt.getSource());
s = ""+source.getText();
}
//for File Menus
if("Change User/Login".compareToIgnoreCase(s)==0)
{
System.out.println("From Login");
}
else if("Exit".equals(s))
{
System.exit(1);
}
}
public static void main(String args[])
{
test ls=new test();
ls.setTitle("Phoneyinsta!!");
ls.setVisible(true);
}
}//phoneyinsta
class makePanel extends JPanel
{
JLabel background_image;
public makePanel()
{
super();
setLayout(null);
setBackground(new Color(0,0,0));
background_image = new JLabel(new ImageIcon("images/main_image.jpg"));
}
public void main_cont()
{
background_image = new JLabel(new ImageIcon("images/main_image.jpg"));
background_image.setBounds(0,0,338,218);
add(background_image);
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arun,
It seems to me that you are just trying to add a background image to a panel. Look at the following class and see how it can help you.

Enjoy,
Manfred.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Manfred for your reply.
But I dig out into my problem and find that perhaps problem lies somewhere else. See My objective was:
1. To have a Container(in this case it is JFrame).
2. To put a Panel into this container(perhaps of same size).
3. and try to put an image right alinged.
I tried with Alignement properties also but in vain but now on running the following code I find
that Frame size is including the titlebar but frame (0,0) is starting below this(can I call it workarea).
that means in my code if I put the panel of same size at (0,0) it goes a little beyond the frame size. and on measuring the size I found it is exactly of Titlebar.
Now can I check the title bar size through some method so that I can put the panel exactly into the frame not beyound that.
I hope I am able to exaplin it properly.
Any suggestions?
P.S.: Perhaps this is all because that I am using layoutManager(null).
regards,
Arun
Code:-
*****
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class testPanel extends JFrame
{
makePanel mp = new makePanel();
public testPanel()
{
Container cont = getContentPane();
cont.setBackground(new Color(140,199,222));
cont.setFont(new Font("arial",Font.BOLD,11));
cont.setLayout(null);
setSize(450,400);
int fx = getWidth();
int fy = getHeight();
System.out.println("fx: "+fx);
System.out.println("fy: "+fy);
mp.setBounds(0,0,fx,fy);
cont.add(mp);
mp.cont();
}
public static void main(String args[])
{
testPanel ls=new testPanel();
ls.setTitle("Test Panel!!");
ls.setVisible(true);
}
}
class makePanel extends JPanel
{
public makePanel()
{
super();
setLayout(null);
setBackground(new Color(0,0,0));
}
public void cont()
{
int px = getWidth();
int py = getHeight();
Image image = Toolkit.getDefaultToolkit().getImage( "main_image.jpg" );
MediaTracker mt = new MediaTracker( this );
mt.addImage( image, 0 );
try
{
mt.waitForID( 0 );
}
catch(Exception e){}
int mx = image.getWidth(null);
int my = image.getHeight(null);
JLabel back = new JLabel(new ImageIcon(image));
System.out.println("px: "+px);
System.out.println("py: "+py);
System.out.println("dp: "+getSize());
System.out.println("mx: "+mx);
System.out.println("my: "+my);
back.setBounds((px-mx),(py-my),mx,my);
add(back);
}
}
[ February 20, 2002: Message edited by: arun mahajan ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Instead of using layout manager (null), try setting your layout manager to FlowLayout( FlowLayout.RIGHT ) to right align your label with the image...

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Nathen Thanks a lot for your reply.
I agree with you perhaps this is much easier to settle with other layout Manager. But if I would like to stick to Layout Manager(null), Is there any other solution?
regards,
Arun
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Well, there is no way to check the titlebar size (probably because there is no way for Java code to control this - it is part of the frame native peer...), so you'll just have to hard-code a value for the titlebar size and hope it's right.

To make it always right-aligned, even if the user resizes the screen, you'll have to add a component listener to the frame and change the location of the image.

-Nate
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Nate for you reply.
But than I was wondering how one can put a component in right place. See what I mean in this case when I am putting a Panel into the Frame of same size it would mean it always oes byond the limit and this will depend u[pon the enviorment it is running. I think this would remain the case even for other layout manager also. becuase my image will get right aligned with respect to panel which has cross the limit of frame and I put the frame resize off. That means there will always some portion of image which will not seen. Strange!!
Any comments!!
regardds,
Arun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set this component?
 
Similar Threads
How to listen toolbar buttons?
how to enable/disable this item?
Can you help me to write better code ?
why this component access is multiplying?
Why this object gets null in actionPerformed