|
-
Mar 7th, 2008, 03:21 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Placing a JPanel in lower-right corner in Netbeans
Hi all,
I want to place my JPanel in lower-right corner of the desktop, in NetBeans 6. Is that anyone know how to do it.
Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 7th, 2008, 04:17 PM
#2
Re: Placing a JPanel in lower-right corner in Netbeans
The following code will place a JFrame on the bottom right corner of the screen, but unfortunately I don't think you can stop users from moving it
Code:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Util {
public static void main(String[] args) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame panel = new JFrame();
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setSize(new Dimension(200, 100));
panel.setLocation(screenSize.width - panel.getWidth(), screenSize.height - panel.getHeight() - 25);
panel.setResizable(false);
panel.setVisible(true);
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 9th, 2008, 09:20 PM
#3
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Actually what I want to do is, initially display the window in that place. Basically using a tray icon. At the time I don't think about it.
Now you may have an idea what I try to do. Display the full frame in that place, lower-right corner of the desktop(or the viewable area of the screen). In this code you get that task-bar height as 25. But it is not common fro all. So I try to combine this with your previous code.
I'll try it and let you know.
Do you have any suggestion on my work for me.
Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 9th, 2008, 10:59 PM
#4
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Ok, at the end I got the way to do this.
Actually your trick works nicely on calculating task-bar height. But one thing to let you know. In classic view(I mean the screen view) it perfectly ok. But in XP style, I can't see the bottom edge of the frame. Seems that it caused the design of the task-bar. You can easily see that, when you minimize any window there is a gap, approximately 1pix, between the upper edges of task-bar and minimized window.
I think you got my point. Any idea about that.
Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 10th, 2008, 01:17 AM
#5
Re: Placing a JPanel in lower-right corner in Netbeans
What are you trying to accomplish?
Are you trying to make something similar to the "System tray notification icon"?
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 10th, 2008, 03:46 AM
#6
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Yes, but rather than a pop-up-menu I want to use a dialog(few controls are included) which can be open by clicking on a try icon.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 10th, 2008, 04:16 PM
#7
Re: Placing a JPanel in lower-right corner in Netbeans
So...
Instead of using the PopupMenu show your dialog on mouseClick
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 10th, 2008, 09:12 PM
#8
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Yes, here what I have try. I'm stuck with that how to execute my frame. Here is the code.
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Toolkit;
public class Main extends JFrame
{
protected JButton buttonOne, buttonTwo;
public Main()
{
buttonOne = new JButton("Ok");
buttonOne.setVerticalTextPosition(AbstractButton.CENTER);
buttonOne.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
//buttonOne.setMnemonic(KeyEvent.VK_D);
//buttonOne.setActionCommand("disable");
buttonTwo = new JButton("Exit");
buttonTwo.setVerticalTextPosition(AbstractButton.BOTTOM);
buttonTwo.setHorizontalTextPosition(AbstractButton.CENTER);
// buttonTwo.setMnemonic(KeyEvent.VK_M);
// Set the actions
buttonOne.addActionListener(this);
//buttonTwo.addActionListener(this);
// Add components to the container
add(buttonOne);
add(buttonTwo);
final TrayIcon trayIcon;
if (SystemTray.isSupported())
{
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
MouseListener mouseListener = new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e)
{
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Exiting...");
System.exit(0);
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Peformed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.err.println("TrayIcon could not be added.");
}
}
else
{
System.err.println("System tray is currently not supported.");
}
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Main Frame!");
frame.setSize(new Dimension(300, 300));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int frameHeight = frame.getHeight();
int taskbarHeight = screenHeight - frameHeight;
System.out.println(screenHeight);
System.out.println(frameHeight);
System.out.println(taskbarHeight);
Main newContentPane = new Main();
//newContentPane.setOpaque(true); // Opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Main main = new Main();
}
}
I try to add two buttons at the from load, I mean at the constructor. But it wont work. I can't do two things.
1. I can't implement 'ActionListener' for buttons.
2. Can't set the 'setOpaque' of my content pane.
Can you tell me where I'm going wrong.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 10th, 2008, 10:23 PM
#9
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
I try to call the createAndShowGUI() in actionListener and gives a bunch of exceptions.
Simply like this,
Code:
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Try to simply call the GUI here
createAndShowGUI();
}
}
Here is the exception I got.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 11th, 2008, 01:37 AM
#10
Re: Placing a JPanel in lower-right corner in Netbeans
Here is your code with some fixes
Code:
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main extends JFrame {
protected JButton buttonOne, buttonTwo;
/**
*
*/
public Main() {
super("Test Frame");
getRootPane().setOpaque(false);
buttonOne = new JButton("Ok");
buttonOne.setVerticalTextPosition(AbstractButton.CENTER);
buttonOne.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
//buttonOne.setMnemonic(KeyEvent.VK_D);
//buttonOne.setActionCommand("disable");
buttonTwo = new JButton("Exit");
buttonTwo.setVerticalTextPosition(AbstractButton.BOTTOM);
buttonTwo.setHorizontalTextPosition(AbstractButton.CENTER);
// buttonTwo.setMnemonic(KeyEvent.VK_M);
// Set the actions
buttonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createAndShowGUI();
}
});
//buttonTwo.addActionListener(this);
// Add components to the container
add(buttonOne);
add(buttonTwo);
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
setVisible(true);
}
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Peformed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
System.err.println("System tray is currently not supported.");
}
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Main Frame!");
frame.setSize(new Dimension(300, 300));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int frameHeight = frame.getHeight();
int taskbarHeight = screenHeight - frameHeight;
System.out.println(screenHeight);
System.out.println(frameHeight);
System.out.println(taskbarHeight);
Main newContentPane = new Main();
//newContentPane.setOpaque(true); // Opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Main main = new Main();
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 11th, 2008, 01:49 AM
#11
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Thanks for your help pal. Actually I slightly change the way I do this. Simply used another class (a JFrame) and do all the design of UI and call it from my main class.
It's work, and it is easy to do rather than coding a lot.
One thing to know. On a button click event I want to dispose the fame. I simply try this.
Code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
}
But it doesn't work. No any error either. Is that wrong.
I just want to dispose the frame, not to exit the application.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 11th, 2008, 04:09 AM
#12
Re: Placing a JPanel in lower-right corner in Netbeans
call the dispose method instead. This line you're using is to tell Java to dispose this frame when the close button (The tiny X button in the top right corner of the screen) is clicked
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 11th, 2008, 04:23 AM
#13
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Yes pal, I got it.
As you said my previous code dispose the window when I click the close button of the window. Say I point to the JFrame which I used as,
Code:
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Is that same. I try to dispose the frame. Not the application.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 11th, 2008, 05:35 AM
#14
Re: Placing a JPanel in lower-right corner in Netbeans
If you call frame.dispose() this will only cause the frame to dispose, nothing else.
You should keep the setDefaultCloseOperation call but put it in the Frame constructor not in the actionPerformed body
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Mar 11th, 2008, 06:03 AM
#15
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
Oops, I got the point pal. All those testing are done on the wrong place.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Mar 11th, 2008, 09:40 PM
#16
Thread Starter
PowerPoster
Re: Placing a JPanel in lower-right corner in Netbeans
At the end I completed my GUI developing using AWT/Swing. Now I try to implement some functionality on it. So it is better to mark solved this thread and start a new ones for my next questions.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|