Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-11-2008, 04:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,405
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
GUI components not display
Hi all,

I've create an application that open a dialog on tray icon click. Tray icon is ok now and I try to add a dialog with some controls. Actually two buttons are there right now. Here is the full code I have try.

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(); } }
Can you guys tell me why the dialog is not displayed. I'm weak on AWT/Swing implements.

Thanks.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-11-2008, 06:16 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
SystemTray
You can get some images to use from this reference page Steal Our Code.
Under the header/link
"Learning to Use the System Tray API in Java SE 6"
click on the link:
"Download an Executable JAR File (Source Code Included)"
You can extract the three gif images from the jar file.
See Using JAR Files: The Basics if you want more info about jar files.
You app works okay now.
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 SysTray implements ActionListener { protected JButton buttonOne, buttonTwo; public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); String ac = button.getActionCommand(); // Another way to identify source component: if(ac.equals("disable")) System.out.println("ActionEvent set by buttonOne"); if(button == buttonOne) System.out.println("Ok"); if(button == buttonTwo) System.exit(0); } private Box getContent() { buttonOne = new JButton("Ok"); buttonOne.setVerticalTextPosition(AbstractButton.CENTER); //aka LEFT, for left-to-right locales buttonOne.setHorizontalTextPosition(AbstractButton.LEADING); //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); Box box = Box.createHorizontalBox(); // Add components to the container box.add(Box.createHorizontalGlue()); box.add(buttonOne); box.add(Box.createHorizontalGlue()); box.add(buttonTwo); box.add(Box.createHorizontalGlue()); return box; } private void initSystemTray() { final TrayIcon trayIcon; if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit()//.getImage("tray.gif"); .createImage("images/sysTray/logo.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); SysTray sysTray = new SysTray(); sysTray.initSystemTray(); frame.getContentPane().add(sysTray.getContent(), "Last"); // frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { createAndShowGUI(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-12-2008, 03:16 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,405
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Thanks a lot pal. I got that where I'm going wrong.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on July 13, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is it best to declare swing components? MacNstuff AWT / Swing 1 02-06-2008 01:59 AM
HTML on Swing Components Java Tip Java Tips 0 11-27-2007 10:51 AM
Java Components are not displayed(sometimes) archanajathan AWT / Swing 3 11-05-2007 09:34 AM
Useful Java Application Components 0.9.27 JavaBean Java Announcements 0 10-02-2007 05:14 PM
Gui Components Marty New To Java 1 05-28-2007 05:04 AM


All times are GMT +3. The time now is 12:56 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org