View Single Post
  #1 (permalink)  
Old 03-11-2008, 04:21 AM
Eranga's Avatar
Eranga Eranga is offline
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,042
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 September 4, 2008)

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