Advertisement
resacr

ClAndroidToast

Jun 30th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package SispakLaptop;
  2.  
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7. import java.awt.event.ComponentAdapter;
  8. import java.awt.event.ComponentEvent;
  9. import java.awt.geom.RoundRectangle2D;
  10. import javax.swing.*;
  11.  
  12. @SuppressWarnings("serial")
  13. public class ClAndroidToast extends JDialog {
  14.  
  15.     String msg;
  16.     JFrame frame;
  17.     // you can set the time for how long the dialog will be displayed
  18.     public static final int LENGTH_LONG = 2000;
  19.     public static final int LENGTH_SHORT = 1000;
  20.  
  21.     public ClAndroidToast(JFrame frame, boolean modal, String msg) {
  22.         super(frame, modal);
  23.         this.msg = msg;
  24.         this.frame = frame;
  25.         initComponents();
  26.     }
  27.  
  28.     private void initComponents() {
  29.         setLayout(new FlowLayout(FlowLayout.CENTER));
  30.         addComponentListener(new ComponentAdapter() {
  31.             // Give the window an rounded rect shape.
  32.  
  33.             @Override
  34.             public void componentResized(ComponentEvent e) {
  35.                 setShape(new RoundRectangle2D.Double(0, 0, getWidth(),
  36.                         getHeight(), 30, 30));
  37.             }
  38.         });
  39.         setUndecorated(true);
  40.         setSize(300, 50);
  41.         setLocationRelativeTo(frame);// adding toast to frame or use null
  42.         getContentPane().setBackground(Color.GRAY);
  43.  
  44.         // Determine what the GraphicsDevice can support.
  45.         GraphicsEnvironment ge = GraphicsEnvironment
  46.                 .getLocalGraphicsEnvironment();
  47.         GraphicsDevice gd = ge.getDefaultScreenDevice();
  48.         final boolean isTranslucencySupported = gd
  49.                 .isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
  50.  
  51.         // If shaped windows aren't supported, exit.
  52.         if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
  53.             System.err.println("Shaped windows are not supported");
  54.         }
  55.  
  56.         // If translucent windows are supported,
  57.         // create an opaque window.
  58.         // Set the window to 50% translucency, if supported.
  59.         if (isTranslucencySupported) {
  60.             setOpacity(0.5f);
  61.         } else {
  62.             System.out.println("Translucency is not supported.");
  63.         }
  64.  
  65.         JLabel label = new JLabel();
  66.         label.setForeground(Color.WHITE);
  67.         label.setText(msg);
  68.         add(label);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement