Draging a JLabel

Hi I have a problem while draging a JLabel in a JFrame, could someone help me here's the code. While draging I loose control of the JLabel + the cursor doesn't stay with the JLabel
2 classes:
test.java
MoveLabel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
class testMove
public static void main (String[] args)
final JFrame frame = new JFrame("Prototype");
final MoveLabel lmv = new MoveLabel();
frame.getContentPane().add(lmv);
frame.setSize(500,500);
frame.setVisible(true);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
class MoveLabel extends JLabel implements MouseListener, MouseMotionListener
public int x;
public int y;
public int mx;
public int my;
public int new_mx;
public int new_my;
public MoveLabel()
// super("Print", icon, JLabel.CENTER);
this.setIcon(new ImageIcon("print.jpg"));
this.addMouseListener(this);
this.addMouseMotionListener(this);
public void mouseClicked(MouseEvent e)
if (e.getClickCount() == 2)
System.out.println("You have DoubleClicked ");
public void mouseDragged(MouseEvent e)
new_mx = e.getX();
new_my = e.getY();
x += new_mx - mx;
y += new_my - my;
System.out.println("depl de " + x +" y " + y);
this.setText("Printing");
this.setLocation(x,y);
mx = new_mx;
my = new_my;
public void mouseMoved(MouseEvent e)
public void mousePressed(MouseEvent e)
mx = e.getX() ;
my = e.getY() ;
this.setText("Printing");
public void mouseReleased(MouseEvent e)
System.out.println("You have released at " + e.getX()+ " " + e.getY());
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
}

I changed some codes in your original program. It works now.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
public class testMove
public static void main (String[] args)
     JFrame frame = new JFrame("Prototype");
     frame.getContentPane().setLayout(null);
     MoveLabel lmv = new MoveLabel();
     lmv.setBounds(10,10,100,20);
     frame.getContentPane().add(lmv);
     frame.setSize(500,500);
     frame.setVisible(true);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
public class MoveLabel extends JLabel implements MouseListener, MouseMotionListener
public int x;
public int y;
public int mx;
public int my;
public int new_mx;
public int new_my;
private int compX;
private int compY;
public MoveLabel()
// super("Print", icon, JLabel.CENTER);
this.setIcon(new ImageIcon("print.jpg"));
this.addMouseListener(this);
this.addMouseMotionListener(this);
public void mouseClicked(MouseEvent e)
if (e.getClickCount() == 2)
System.out.println("You have DoubleClicked ");
public void mouseDragged(MouseEvent e)
     Point p=SwingUtilities.convertPoint(this,e.getPoint(),this.getParent());
     int x1 = p.x;
     int y1 = p.y;
     int diffX=x1-mx;
     int diffY=y1-my;
/*new_mx = e.getX();
new_my = e.getY();
x += new_mx - mx;
y += new_my - my;*/
//System.out.println("depl de " + x +" y " + y);
this.setText("Printing");
setLocation(compX+diffX,compY+diffY);
//mx = new_mx;
//my = new_my;
public void mouseMoved(MouseEvent e)
public void mousePressed(MouseEvent e)
Point p=SwingUtilities.convertPoint(this,e.getPoint(),getParent());
               mx = p.x;
               my = p.y;
               compX=getBounds().x;
               compY=getBounds().y;
this.setText("Printing");
public void mouseReleased(MouseEvent e)
System.out.println("You have released at " + e.getX()+ " " + e.getY());
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
}

Similar Messages

  • Non-English character not showing up in JLabel correctly

    In my program there is a JLabel that displays Chinese characters in several sizes of fonts, it was displaying all of them correctly until my program grew larger and takes longer to initialize, now it is only displaying some sizes of fonts correctly while displaying others as rectangles. Since my program is too large to be imported here, I am trying to demo the problem in a smaller program, but in this smaller program, all sizes of fonts showed up correctly, here it is :
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Java_Test extends JPanel
      static int Total=9000;
      JComboBox ComboBox_Array[]=new JComboBox[Total];
      Font Times_New_Roman_15_Font=new Font("Times New Roman",0,15);
      int Small_Chinese_Font_Size=3;
      String Software_Info_Chinese_Text="<Html><Table Width=100% Border=0 Cellpadding=2 Cellspacing=3><Tr><Td Align=Center><Font Size=6 Color=#3737FF>[ \u4EA7\u54C1\u7BA1\u7406 ] </Font></Td></Tr>"+
                                               "<Tr><Td Align=Center><Font Size=5 Color=green>\u5E2E\u52A9\u4F60\u7BA1\u7406\u4EA7\u54C1, \u5408\u540C, \u53CA\u5176\u5B83\u4FE1\u606F :</Font></Td></Tr>"+
                                               "<Tr><Td><Font Size="+Small_Chinese_Font_Size+" Color=#2255BB>"+
                                                 "<li>\u8F93\u5165, \u7EF4\u62A4\u548C\u6253\u5370\u8BE6\u7EC6\u4EA7\u54C1\u53CA\u4F9B\u8D27\u5546\u4FE1\u606F<Br>"+
                                                 "<li>\u5730\u5740\u548C\u5907\u6CE8\u4FE1\u606F\u53EF\u7528\u591A\u79CD\u8BED\u8A00\u8F93\u5165<Br>"+
                                               "</Font></Td></Tr>"+
                                               "</Table></Html>";
      Java_Test()
        for (int i=0;i<Total;i++) ComboBox_Array=new JComboBox();
    JLabel A_Non_English_Label=new JLabel(Software_Info_Chinese_Text);
    A_Non_English_Label.setFont(Times_New_Roman_15_Font);
    A_Non_English_Label.setForeground(Color.BLUE);
    add(A_Non_English_Label);
    setPreferredSize(new Dimension(500,200));
    static void Out(String message) { System.out.println(message); }
    public static void main(String[] args)
    final Java_Test demo=new Java_Test();
    Dimension Screen_Size=Toolkit.getDefaultToolkit().getScreenSize();
    final JFrame frame=new JFrame("Java Test");
    frame.add(demo);
    frame.addWindowListener(new WindowAdapter()
    public void windowClosing(WindowEvent e) { System.exit(0); }
    public void windowDeiconified(WindowEvent e) { demo.repaint(); }
    public void windowGainedFocus(WindowEvent e) { demo.repaint(); }
    public void windowOpening(WindowEvent e) { demo.repaint(); }
    public void windowResized(WindowEvent e) { demo.repaint(); }
    public void windowStateChanged(WindowEvent e) { demo.repaint(); }
    frame.pack();
    frame.setBounds((Screen_Size.width-demo.getWidth())/2,(Screen_Size.height-demo.getHeight())/2-10,demo.getWidth(),demo.getHeight()+38);
    frame.setVisible(true);
    As you can see, in this smaller program I'm trying to create a Total of 9000 objects to simulate lots of memory use, but to no avail. It is still displaying all sizes of fonts correctly.
    In my large program if I change html font from size 3 to size 5, it will display correctly, it seems that for certain sizes it can't display properly when the program gets too large and complicated, not because I don't have the fonts, but because of the complexity of my program. I wonder if anyone else has similar problems, and how to solve it ?
    Frank                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    I've just made a breakthrough, the large program will work correctly if I comment out : A_Non_English_Label.setFont(Times_New_Roman_15_Font);
    But then the font doesn't look the way I wanted it to be.
    So I changed it to the following :
    ================================================
    Font Courier_New_15_Font=new Font("Courier New",0,15);
    A_Non_English_Label.setFont(Courier_New_15_Font);
    ================================================
    Now it looks not the same but similar to the way I wanted. Thus my question becomes : why it can't work correctly with "Times_New_Roman", but with "Courier_New" in the large program, they both displayed correctly in my small test program ? Is it because it takes too much resource to load the "Times_New_Roman" font ?
    Frank

  • Can not display ImageIcon in a JLabel where you want in the code ???

    Hello, everybody. I need your help to solve a problem I am on it for 2 days now :
    In the constructor of my class, I have a lot of initializing methods. If I try to display an image (with ImageIcon in a JLabel) within one of these methods, there is no problem and the image is correctely displayed. To try that, I use an image stored on my local disk in "c:\\".
    public class Bedes extends javax.swing.JFrame {
    public Bedes() {
    initComponents();
    -- further initializations
    initConnection();
    public void initConnection() {
    try {
    String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
    Properties systemProperties = System.getProperties();
    systemProperties.put("jdbc.drivers", driverName);
    System.setProperties(systemProperties);
    String dbURL = "jdbc:odbc:Bandes Dessin�es";
    String strUtilisateur = "bedes";
    String strMotDePasse = "cdle";
    connectBedes = DriverManager.getConnection(dbURL,strUtilisateur,strMotDePasse);
    catch (Exception e) {
    System.out.println("Exception in DB Contact " + e);
    // Here, I display an image in my JLabel, and it works :
    couvertureAlbumString = "c:\\myImage.jpg";
    couvertureAlbumsIcon = new ImageIcon(couvertureAlbumString);
    couvertureAlbumsJLabel.setIcon(couvertureAlbumsIcon);
    couvertureAlbumsJLabel.repaint();
    }The problem is that I do not want to display these images there, but in a method that read some data's in a Database (with "try"-"catch" blocks). If there is a name of an image store in the DB, then it must be displayed in the JLabel (located in a JFrame -> JDeskTopPane -> JTabbedPane -> JPanel).
    private void searchDetailsForAlbum(String albumsDetailsToSearch) {
    String nomImageCouverture;
    String couvertureAlbumString;
    ImageIcon couvertureAlbumsIcon;
    try {
    PreparedStatement requeteDetailsAlbums = connectBedes.prepareStatement("SELECT * FROM bandes_dess WHERE bede_nom_tome = ?");
    requeteDetailsAlbums.setString(1, albumsDetailsToSearch);
    ResultSet resultatDetailsAlbums = requeteDetailsAlbums.executeQuery();
    resultatDetailsAlbums.next();
    collectionAlbumsJTextField.setText(resultatDetailsAlbums.getString(6));
    genreAlbumsJTextField.setText(resultatDetailsAlbums.getString(9));
    dessinateurAlbumsJTextField.setText(resultatDetailsAlbums.getString(10));
    scenaristeAlbumsJTextField.setText(resultatDetailsAlbums.getString(11));
    coloristeAlbumsJTextField.setText(resultatDetailsAlbums.getString(12));
    prixAchatAlbumsJTextField.setText(resultatDetailsAlbums.getString(13));
    // Here is the name of the Image to de read
    nomImageCouverture = resultatDetailsAlbums.getString(14);
    catch (Exception e) {
    System.out.println("Exception in searchDetailsForAlbum" + e);
    couvertureAlbumString = "c:\\";
    // Here, I check if there is a name for a picture in the DB
    if (!(nomImageCouverture.equals("No"))) {
    // If Yes, I want to display it
    couvertureAlbumString = couvertureAlbumString + nomImageCouverture;
    couvertureAlbumsIcon = new ImageIcon(couvertureAlbumString);
    couvertureAlbumsJLabel.setIcon(couvertureAlbumsIcon);
    couvertureAlbumsJLabel.repaint();
    }My JLabel is well displayed, but empty, without any image on it. WHY ???
    MLK, you asked me to add some debug code. OK, with pleasure, but what is that "debug code" ???
    Thanks a lot in advance if you could help me.
    Christian.

    DEBUG: couvertureAlbumString: c:\Aldebaran-La-Photo
    OK, the extension of the file was NOT present !!!
    I added it and ..... it works pretty good !!!
    Thank you for your idea of the DEBUG.
    Christian.

  • JLabel components not being displayed on the content Panel in Applet

    Hi everyone!!
    Plz give a solution to the following problem:
    I have an applet in which i have JPanel component and on that component i am adding a lot of JLabel components but the problem is that the JLabel components are not displayed on the applet unless i minimize or maximize the applet.
    kindly give solution to the problem.
    Thanks

    Howdy,
    code would be helpful. Here is a very simple JApplet that displays labels okay here in appletviewer. You talk of applets and JLabels. I assume you are using JApplets.
    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    public class AppletPie extends JApplet {
         public void init() {
              Container cp = getContentPane();
              cp.setLayout(new FlowLayout());
              cp.add(new JLabel("Show"));
              cp.add(new JLabel("up"));
              cp.add(new JLabel("you"));
              cp.add(new JLabel("dumb"));
              cp.add(new JLabel("labels"));
    }Perhaps something in this code will help you to solve your problem.
    regards
    sjl

  • Getting values from JLabel[] with JButton[] help!

    Hello everyone!
    My problem is:
    I have created JPanel, i have an array of JButtons and array of JLabels, they are all placed on JPanel depending from record count in *.mdb table - JLabels have its own value selected from Access table.mdb! I need- each time i press some button,say 3rd button i get value showing from 3rd JLabel in some elsewere created textfield, if i have some 60 records and 60 buttons and 60 labels its very annoying to add for each button actionlistener and for each button ask for example jButton[1].addActionListener() ...{ jLabel[1].getText()......} and so on!
    Any suggestion will be appreciated! Thank you!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Testing
      public void buildGUI()
        final int ROWS = 10;
        JPanel[] rows = new JPanel[ROWS];
        final JLabel[] labels = new JLabel[ROWS];
        JButton[] buttons = new JButton[ROWS];
        JPanel p = new JPanel(new GridLayout(ROWS,1));
        final JTextField tf = new JTextField(10);
        ActionListener listener = new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            tf.setText(labels[Integer.parseInt(((JButton)ae.getSource()).getName())].getText());
        for(int x = 0; x < ROWS; x++)
          labels[x] = new JLabel(""+(int)(Math.random()*10000));
          buttons[x] = new JButton("Button "+x);
          buttons[x].setName(""+x);
          buttons[x].addActionListener(listener);
          rows[x] = new JPanel(new GridLayout(1,2));
          rows[x].add(labels[x]);
          rows[x].add(buttons[x]);
          p.add(rows[x]);
        JFrame f = new JFrame();
        f.getContentPane().add(p,BorderLayout.CENTER);
        f.getContentPane().add(tf,BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
    }

  • Problem with HTML in a JLabel

    Is it possible to use styles when using HTML in a JLabel?
    Example:
    <html><hr style="height: 1px; color: black;"></html>Doesn't work correctly as this should give a solid 1px high hr, but doesn't. Instead it gives a black hr with a white shade...
    The JLabel it is used in is not customized in any way.

    I too am interested in getting color on an HR to work. Can you summarize what you have done to date? Sample code?
    Note: An easy way to correct it in a Nonframe instance of html is to subclass HTMLEditor to implement your own ViewFactory and own HRuleView which checks the attribute tag color for the color. I've got this to work, but frames don't work since you can call FrameView (not public) and thus have to use the super class - which then by-passes the ViewFactory from then on.
    So a NON-FRAME Solution to give HR color is... Anybody have a better global solution?
    Here is the one line fix to HRuleView .setProperties. So if you subclass HRuleView specifically to do
    public class MyHTMLEditorKit extends HTMLEditorKit { // StyledEditorKit
        private static final ViewFactory defaultFactory = new MyHTMLFactory();
        public static class MyHTMLFactory extends HTMLFactory {//implements ViewFactory {
             public View create(Element elem) {
             Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
             if (o instanceof HTML.Tag) {
              HTML.Tag kind = (HTML.Tag) o;
              if (kind == HTML.Tag.HR) {
                  return new MyHRuleView(elem);
           return super.create(elem);
    class MyHRuleView extends View  {
         private Color color = null;
         public void setPropertiesFromAttributes() {
              super.setPropertiesFromAttributes();
              try {color = new Color(Integer.decode((String)attr.getAttribute("color")).intValue());} catch (Exception e) {}
         public void paint(Graphics g, Shape a) {
              if (color != null) g.setColor(color);
              super.paint(g, a);
    }

  • Animated gifs on jlabel

    hi
    i am new to java and got a question.
    how do i place a animated gif on a jlabel correct?
    i done it so far with 'seticon' and the gif is visible, but not animated.
    what do i have to do?

    Use setImageObserver()
    ImageIcon myicon = new ImageIcon (...);
    JLabel mylabel = new JLabel (myicon);
    myicon.setImageObserver (mylabel);
    Okay, thank you very much.
    Now, the Image is animated after i maximize and reset my application.

  • JLabel ImageIcon not opaque.

    Hi I am having a little problem with my program that I am trying to develop.
    I have created a JFrame and instead of having the background the usual grey I have used an image.
    I then wanted to create some nice big buttons with nice smooth curved edges but was having trouble using the JButton so decicded to use a JLabel and attach a MouseListener to it. This seems to be working fine at the mo.
    The only problem is that instead of my nice gradient filled background being displayed I get a big white border around the JLabel. The rest of the background is perfectly displayed having only a problem around the JLabel. I have tried not using a background image and it shows the JLabel correctly and the button looks nice and smooth.
    I think it might be something to do with the rendering sequence maybe but I am at a loss. If any one has any ideas on how to fix it or a better way of doing it I would be most grateful.
    Many thanks for your help in advance.
    Matt
    PS now heres my code:
    public class GUI extends JFrame implements MouseListener {
         ImageIcon icon;
         JPanel buttonPanel = new JPanel();
         public GUI(){
              super("Property Works - Estate Agent Management System");
              icon = new ImageIcon("images/bg.jpg");
              JPanel panel = new JPanel(){
                   protected void paintComponent(Graphics g){
                        //g.drawImage(icon.getImage(), 0, 0, null);
                        Dimension d = getSize();
                        //g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);                    
                        super.paintComponent(g);                    
              panel.setOpaque(false);
              addButton();
              panel.add(buttonPanel);
              getContentPane().add(panel);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setExtendedState(JFrame.MAXIMIZED_BOTH);
              setVisible(true);
         public void addButton(){
              ImageIcon icon1 = new ImageIcon("images/buttons/addProperty.gif");
              JLabel addProperty = new JLabel(icon1);
              addProperty.setOpaque(false);
              addProperty.setToolTipText("Add a new property");
              addProperty.addMouseListener(this);
              JLabel tester = new JLabel("Tester");
              tester.setOpaque(false);
              buttonPanel.add(addProperty);
              buttonPanel.add(tester);     
         public void mousePressed(MouseEvent arg0) {
              // TODO Auto-generated method stub
              System.out.println("Button Clicked");
    }

    Swing related questions should be posted in the Swing forum.
    Use the "code" tags when you post code so the formatting is retained.
    The code you posted doesn't compile so we can't see the incorrect behaviour.
    Using code I already have I do not see any white border around the image. I tested using a "Duke" image which is found in many of the Swing tutorial examples. Maybe your image is the problem.

  • JLabel + Animated GIF: replacing the image

    Hi, folks.
    I've run into what looks like an interesting glitch, not necessarily functionality-wise but purely cosmetic.
    I have a JLabel which needs to be switched between a static image and an animated image at different times of execution. I'm doing this more or less like this:
        private JLabel label = new JLabel();
        private ImageIcon inactiveIcon;
        private ImageIcon activeIcon;
        public MyClass()
            inactiveIcon = Utils.loadIcon(...);
            activeIcon = Utils.loadIcon(...);
            setActive(false);
        public void setActive(boolean active)
            label.setIcon(active ? activeIcon : inactiveIcon);
        }It starts up fine and shows the inactive image, which is not animated.
    However, calling setActive(true) seems to cause a flash of grey before showing any animation. The interesting thing is at this point, the image should be completely loaded, so nothing should be blank for any period of time. The other interesting thing is that calling setActive(false) does not cause the flash of grey.
    I've checked the image to make sure all the frames are present and that none are blank.
    I've tried doing this a number of different ways, by Icon.setImage(Image), also by using two labels and setVisible(boolean).
    Has anyone else seen this before? The particular thing which worries me is there isn't a grey flash going the other way. Otherwise I wouldn't think a workaround existed. :-)

    Update. I changed the code to this:
    label = new JLabel() {
    public void update(Graphics g) {
    paint(g);
    public void paint(Graphics g) {
    paintComponent(g);
    protected void paintComponent(Graphics g) {
    ImageIcon icon = (ImageIcon) getIcon();
    icon.paintIcon(this, g, 0, 0);
    };It still greys out. :-/
    I tried label.setUI(new LabelUI() { ... }) with a
    custom paint method also, and it didn't draw anything
    at all, for either label.I'm not sure if you accounted for this but the paint method in the UI is different then the 'public void paintComponent(Graphics g)' method and paint (Graphics g) method in components. The method is 'public void paint(Graphics g, JComponent c)'. Overwriting 'public void paint (Graphics g)' in a UI, is like defining a new method alltogether.
    Did you also make sure to avoid calling 'super.paintComponent()' when overwriting your 'paintComponent(...)' methods? If you did call it, you aren't preventing drawing of previous shapes. This worked for me when I had to do something similar.
    You could also try to draw your icon in your UI, if you haven't done so already.
    Regards,
    Devyn

  • Refreshing animated gif in Jlabel

    Hi,
    I've created a GUI for a cashmachine application and have the problem that my animated GIF's work on the first call, but only display the last frame of the animation on subsequent calls.
    I've tried alot of things, including validate, repaint, refresh but im running out of ideas!
    I have come to this assumption because I have created a copy of the animation and imported that on the second call and it works like it should. However once again on subsequent calls it simply displays the last from and doesn't animate.
    The code looks like this:
    public JLabel getInsertCardGraphic() {
              /* Imports the enter card gif */
              ImageIcon enterCardGraphic= new ImageIcon(LOCATION+"moving-card2.gif");
              JLabel enterCardHolder = new JLabel(enterCardGraphic);
              return enterCardHolder;
         }which is called from:
    public void actionPerformed(ActionEvent evt) {
              playBeep();          //not related to problem     
              eastPane.removeAll();
              eastPane.add(getInsertCardGraphic(), BorderLayout.CENTER); //called here
              eastPane.add(getCashSlotImage(), BorderLayout.SOUTH);
              eastPane.validate();
              String cardNum = cardlist.getSelectedItem().toString(); //unrelated code
              char[] pinArray = new char[4];
              pinArray[0] = cardNum.charAt(12);
                    pinArray[1] = cardNum.charAt(8);
                    pinArray[2] = cardNum.charAt(4);
                    pinArray[3] = cardNum.charAt(0);
              String encryptedPIN = new String(pinArray);
              // Tell controller that a card has been entered.
              Controller.getInstance("Ipl/ATM/Model/Model").cardEntered( cardlist.getSelectedItem().toString(), encryptedPIN);
         }if anyone has any suggestions on what I should do I will be extremely greatful!!
    Cheers,
    Hakan
    Edited by: hakz on Nov 13, 2007 5:01 AM

    Animated gif is working fine for me.
    You can check the delay time between the images in the gif, to see that it's not too short.
    Here's a simple example:import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.imageio.stream.ImageInputStream;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import com.sun.imageio.plugins.gif.GIFImageMetadata;
    import com.sun.imageio.plugins.gif.GIFImageReader;
    public class AnimatedGifTest {
        public static void main(String[] args) {
            try {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
                URL url = new URL("http://www.gifanimations.com/action/SaveImage?path=/Image/Animations/Cartoons/~TS1176984655857/Bart_Simpson_2.gif");
                frame.add(new JLabel(new ImageIcon(url)));
                frame.pack();
                frame.setVisible(true);
                // Read the delay time for the first frame in the animated gif
                GIFImageReader reader = (GIFImageReader) ImageIO.getImageReadersByFormatName("GIF").next();
                ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());
                reader.setInput(iis);
                GIFImageMetadata md = (GIFImageMetadata) reader.getImageMetadata(0);           
                System.out.println("Time Delay = " + md.delayTime);
                reader.dispose();
                iis.close();
            } catch (Exception e) {e.printStackTrace();}
    }

  • Need help with animated JLabel

    I've created an animated extension of the JLabel which imitates a "typewriter" effect. It works smoothly for single-line labels.
    Here is a basic pseudo-code outline of how I went about doing it:
    constructor(String fullText, int delay) {
      create swing timer
      start timer
    //called by timer
    actionPerformed() {
      if (all chars are printed)
        stop timer
      else {
        labelStr += fullText.charAt(pos);
        this.setText(labelStr);
        pos++;
    }The problem is, whenever It recognises an HTML line break (to make it a multi-line label), all of the text gets bumped up. Here's a rough animation showing my animated label and a regular jlabel...
    http://img223.imageshack.us/img223/8459/typewriteranizy1.gif
    As you can see, once the line breaks are added to the animated label's text, all the text is moved up. This creates a choppy scrolling effect, something I'm trying to avoid at the moment.
    Any ideas on how I could keep the animated text in a fixed position, so it doesn't scroll up like this?
    I can provide source code if needed.

    Ah yes, my bad. I tried it now but realised I have tried it before, and it doesn't work. i never increments and so it prints the first character every time the timer fires instead. Here is a SSCCE:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TW extends JFrame implements ActionListener
         JLabel iValue = new JLabel("i = ");
         JTextArea log = new JTextArea(10, 20);
         JButton start = new JButton("Start");
         Timer type;
         public TW()
              setLayout(new BorderLayout(0, 0));
              log.setEditable(false);
              log.setLineWrap(true);
              log.setWrapStyleWord(true);
              add(iValue, BorderLayout.PAGE_START);
              add(log, BorderLayout.CENTER);
              add(start, BorderLayout.PAGE_END);
              start.addActionListener(this);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              pack();
              setLocationRelativeTo(null);
              setVisible(true);
         public void actionPerformed(ActionEvent e)
              ActionListener listen = new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        int i = 0;
                        String msg = "This is a simple typewriter demo.";
                        if (i < msg.length())
                             String str = (String)new Character(msg.charAt(i)).toString();
                             log.append(str);
                             iValue.setText("i = " +i);
                             i++;
                        else
                             type.stop();
              type = new Timer(250, listen);
              type.start();
         public static void main (String[] args)
              new TW();
    }

  • Help with making JLabel visible for a few seconds only

    Hello guys,
    I need help with making a JLabel object visible for a few seconds only and disappear when a JButton has been clicked.
    Here is piece of code where I add mouse listener to the JButton object in my case is (jrbFigure1) and
    when is called sets icon and text to the JLabel jlbl object:
    jrbFigure1.addMouseListener(new MouseAdapter() {
    jrbFigure1.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    jlbl.setIcon(cross);
    jlbl.setText(wrong);
    This piece of code is in a switch statement, the statement is in a method.
    The JLabel jlbl object is added in a panel in the constructor of the class.
    I really hope someone could help me on this issue.
    I would like to thank you in advance for any help or advice given.

    Bobson wrote:
    Sorry for posting my issue twice, I thought, I posted it in a wrong forum:)It's OK. We'll ask the mods to delete the other thread.
    I want the label to appear for X seconds when then button is pressed and disappear after that.Then a Swing Timer is what you want to use.
    I need to do this in a number of cases from a switch statement because I several buttons.Better to have several ActionListeners, one for each button or each type of button, rather than one ActionListener with a large switch statement, a so-called switch-board listener. They're messy to debug and upgrade.
    I tried to use Swing Timer, but I cannot set it properly.Go through the Sun Swing tutorial on Swing Timers. It's all outlined for you there: [http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]

  • Problem in setting background of jLabel when mouse is pressed on JLabel

    I am working on a WindowsExplorer like application.
    I have a panel with null layout.
    On this panel there are several labels with icon on top and text below imageIcon.My requirement is ;
    When mouse is pressed on a JLabel , I want to set the background of text on label to show that this is the selected label.what should i do ?

    Use html tags:
    setText"<html><fontcolor=blue>a111<br>b22222</html>");
    public class MyLabel extends JLabel
         ImageIcon i1 = new ImageIcon("g1.gif");
         String    txt;
    public MyLabel(String s)
         super("",JLabel.CENTER);
         txt = s;
         setText("<html><font color=blue>"+txt+"</html>");
         setIcon(i1);
         setOpaque(true);
         setBackground(Color.pink);
         setBorder(new MatteBorder(1,1,1,1,Color.black));
         addMouseListener(new MouseAdapter()     
         {     public void mousePressed(MouseEvent m)
                        setText("<html><font color=blue>"+txt+"</html>");     
    }Noah
         

  • In iTunes on my windows 7 PC, the Edit/Preferences dropdown window,some of the tab windows are not large enough to see all options that are available.  The borders of the window cannot be draged larger.  As a result, I can not uncheck or check some box's.

    In iTunes on my windows 7 PC, in the Edit/Preferences dropdown window,some of the tab windows are not large enough to see all options that are available.  The borders of the window cannot be draged larger.  As a result, I can not uncheck or check some box's on several of the preferences pages.  The Apple people at the store did not help.  I have lownloaded the latest version of iTunes and this is still an issue.  HELP.

    Is there really nobody else with this problem? Really?

  • I have 2 iphones syncing on one computer and on the same apple ID. Before i updatad my Itunes to the last available update and my Iphones to 5.01 i had no problem draging jpg. and mp3 files into my iphones DCIM folder (the picture folder that opens when c

    I have 2 iphones syncing on one computer and on the same apple ID.
    Before i updatad my Itunes to the last available update and my Iphones to 5.01
    i had no problem draging jpg. and mp3 files into my iphones DCIM folder (the picture folder that opens
    when conecting to Itunes). Since my updates Itune wont let me drag and drop any files' even pictures that
    were taken with my Iphone on 4.3.3.
    Please help as we have no itunes music in Israel, only apps and i cant copy any of my old music from my computer
    to my iphones.
    If it helps, i am using a pc with 32 bit windows 7.
    Other from the obove Itunes works fine and does sync pictures from my computer to my Iphone from "my pictures" folder.
    Thank you for your time
    Yoni Bliss
    [email protected]

    The title of my initial post is a bit misleading. I already took a chance deleting one of the iPhones, hoping if I deleted the wrong one, I can still reauthorize it (all with the same Apple ID acct, so not subject to 90-day waiting period, right?) The iTunes database just updated itself, and it says I have 4 "devices" "in the Cloud", but 5 "computers" associated w my account. How can I find out what computers are associated? Isn't there a list I can see? I dont have a clue what computers they might be. If I use the  "Deauthorize All" option, is it a pain to add them all back in. I assume I would have to log in from each computer to reauthorize each one. Would I have to be running the newest OS or version of iTunes to reauthorize? I think I have a PowerBook G4 Titanium with an older OS and iTunes. I'd like to keep that authorized, if possible.

Maybe you are looking for