Java Swing and windows listview control

Hi,
many programming languages have the listview control, to which you can define diferents kinds of data's organizers as Icon, Detail, List, etc.
Somebody knows if java swing have a component as the listview ?
thanks

Yes. It's called JList. Have a look at the tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/index.html

Similar Messages

  • Java Swing and WIndows User Localles

    HI,
    We need to develop a java applet/application which can be switchable at run time to accept and display characters in multilanaguages. This java applet will run on a windows 2000 server with multilanguage pack installed. Is it possible to dynamically switch languages at run time. The machines default localle and operating system installation is US English.
    We have tried some tests with traditional chinese and UI components in Swing but with no success in displaying or inputing valid chinese characters. Has anyone attempted a simliar thing and have any insights that may help.
    Thanks in Advance,
    Jono

    Yes. It's called JList. Have a look at the tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/index.html

  • Performance issue Java Swing under Windows 7

    Hello,
    we have MDI Java Swing application running under Window7. We got a big problem with performance in MDI Windows using AERO.
    Exist any way for Java to tell Win7 that it should not use Aero??
    Thank you,
    David.
    Edited by: 969767 on 6.11.2012 3:33

    If you try to open properties dialog for application (or app shortcut) there you can see "Compatibility" tab and there is check box "Disable visual themes". If we disable this theme our java swing app is very better performance under Windows 7. We need to disable visual theme via java code, if it is possible...
    Thanks.

  • Swing and windows system tray

    could anyone please tell me whether it is possible to have a java swing application running on a windows machine, minimise into the system tray?
    Thank-you.

    AFAIK no. This is because the system tray is rather platform specific. I believe it is possible to have your app tuck itself neatly away in a corner of your desktop, and obviously you can minimise to the task bar. However without soem kind of native addition (maybe a custom launcher?) you can't minimise to the system tray.
    Sorry
    :-(

  • Java Swing and Flash

    Is it possible to create flash components (swf files) and include them in a Swing JFrame and be able to post data from and to it.
    For example, if the swf gui is showing a list of items, the list is coming from the Java application and then the SWF file gets included in a Swing JFrame. When user chooses an item from the list and clicks a button then the results is passed back to the Java application.
    Has anyone done something similar to this? How is this possible? Code samples would be appreciated.

    May be you can try out JFlash ...
    https://jflash.dev.java.net/
    They support only upto Flash 2 player... and it is still under development i guess...

  • Java Swing and Socket Programming

    I am making a Messenger like yahoo Messenger using Swing and Socket Programming ,Multithreading .
    Is this techology feasible or i should try something else.
    I want to display my messenger icon on task bar as it comes when i install and run Yahoo Messenger.
    Which class i should use.

    I don't really have an answer to what you are asking. But I am developing the same kind of application. I am using RMI for client-server and server-server (i have distributed servers) communication and TCP/IP for client-client. So may be we might be able to help each other out. My email id is [email protected]
    Are you opening a new socket for every conversation? I was wondering how to multithread a socket to reuse it for different connections, if it is possible at all.
    --Poonam.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Java swing and threading issue

    i am trying to update swing components from some thread. at my first try, i noticed this was a disaster. upon further research, i found out about the single-threaded model of swing events (i.e. event dispatching thread).
    i did more research and could NOT find a good example on how to update swing components from other threads. many examples were showing too much.
    can someone post a simple example on here? i just want to see how to properly update a swing component from a non-swing class using threading.

    I think its a simple example:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=621226
    Let us know what you think.

  • Wrong image refresh in java applet AND Windows 7

    I'm managing since few days a java applet that takes an image from an industrial product and shows it in a browser (simple web server).
    There is following issue: the image is not correctly loaded and refreshed in its own window if just opened but only if the applet is running on Windows 7. The issue is solved as soon as I reduce the window to icon (minimize) and then resize it. I'm looking into java documentation on how to manage the image rendering, maybe I have to override some update or redraw method more, but it is taking time and I'm still learning. NOTE that the window opened does show the issue only when the windows size is not completely shown in the monitor.
    Meanwhile I'm wandering if some of you faced and solved similar problem.
    Thank you in advance,
    Elena

    Thank you Nitin for your quick reply!
    These are the calling of the functions in the code. As you can see, before to show the image, we are managing a zoom/unzoom by mouse wheel and an addition of writings data on the image itself. All is working fine except on W7:
    public void paint(Graphics g)
         //System.out.println("[ImageFrame::paint my paint!!!]");
         this.PaintImage(g);
    public void update(Graphics g)
         // System.out.println("[ImageFrame::update]");
         this.repaint();
    public void PaintImage(ImageInfo imageInfo, float fps)
         if (imageInfo != null)
              if (this.getImageInfoRef() == null){
                   this.setVisible(true);
              this.setTitle("Image Viewer (" + imageInfo.bufferedImage.getWidth() + "x" + imageInfo.bufferedImage.getHeight() + ") ");
              this.resizeImageFrame((int) (imageInfo.bufferedImage.getWidth() * zoomFactor), (int) (imageInfo.bufferedImage.getHeight() * zoomFactor));
              this.lastFps = fps;
              this.setImageInfoRef(imageInfo);
              //System.out.println("this.getWidth(): " + this.getWidth() + " this.getHeight(): " + this.getHeight() + " zoomFactor: " + zoomFactor);////
              this.PaintImage(this.getGraphics());
    public synchronized void PaintImage(Graphics g)
         ImageInfo locImageInfo = getImageInfoRef();
         if (locImageInfo != null)
              try
                   this.resizeImageFrame((int) (locImageInfo.bufferedImage.getWidth() * zoomFactor), (int) (locImageInfo.bufferedImage.getHeight() * zoomFactor));
                   BufferedImage bufferedImage = this.resizeBufferedImage(locImageInfo.bufferedImage, zoomFactor);
                   this.overlayCodeInfo(bufferedImage, locImageInfo.decInfoStruct, zoomFactor);
                   this.overlayPointerInfo(bufferedImage, locImageInfo.scaling, zoomFactor, this.crossX, this.crossY);
                   this.overlayFrameRateInfo(bufferedImage, this.lastFps);
                   Image img = Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
                   boolean retdraw;
                   retdraw = g.drawImage(img, frameUpperBondarySizeX, frameUpperBondarySizeY, this);
              catch (Exception ex)
                   System.out.println("[ImageFrame::PaintImage] Exception: " + ex);
    Thank you again
    Elena

  • Java, Cygwin, and Windows.... oh my.

    So the problem can be summarized as follows. I have a call out to the operating system from Java to obtain the value of “vmstat” which returns a value from every “x” seconds based on the refresh rate. To do This I use a command similar to this
    Runtime rt = Runtime.getRuntime();
    String[] comm = new String[] {"vmstat","1"};
    Process proc = rt.exec(comm);
    I can then loop around capturing the output and parsing it. Simple.... This approach works on Linux, Mac OS, HP-UX, AIX and Solaris however it fails under Windows. It sits there and simply waits. Now this appears to be a common problem based on the number of postings on it. This article(http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html) suggests a number of approaches to solve the problem. I’ve tried them all and the good news is that they appear to work for a single atomic call that returns all of its output then exits. However commands such as “vmstat/iostat/sar” etc. return output periodically based on their refresh rates and don’t seem to work at all.
    There are plenty of hits in google but no one really seems to suggest a solution. Now Im sure this worked in the past and Im almost certain that I haven’t changed the code.
    I’ve tried calling
    java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"
    Which returns the first line of the directory and then exits.
    java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"
    simply hangs.... whilst
    java myexec cmd /c dir
    Works fine.... So my believe its the combination of java and cygwin and the way io is redirected. If anyone has a chance to look at it.... I’ll be very grateful. Code for simple testcase below.
    import java.util.*;
    import java.io.*;
    class StreamGobbler extends Thread {
    InputStream is;
    String type;
    StreamGobbler(InputStream is, String type) {
    this.is = is;
    this.type = type;
    public void run() {
    try {
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while (true) {
    if (is.available() == 0) {
    try {
    Thread.sleep(10);
    } catch (InterruptedException ie) {
    continue;
    line = br.readLine();
    if (line == null) {
    System.out.println("Error");
    } else {
    System.out.println(line);
    } catch (IOException ioe) {
    ioe.printStackTrace();
    public class myexec {
    public static void main(String[] args) {
    try {
    String osName = System.getProperty("os.name");
    System.out.println(osName);
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(args);
    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Throwable t) {
    t.printStackTrace();
    Edited by: dgiles on Apr 3, 2009 6:27 AM

    Apologies for the code tags.... now included below
    +"I’ve tried calling+
    +java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"+
    +Which returns the first line of the directory and then exits.+
    +java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"+
    +simply hangs"+
    should read
    "I’ve tried calling
    java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"
    Which returns the first line of the directory and then exits.
    *java myexec cmd /c "c:\\cygwin\\bin\\vmstat.exe*"
    simply hangs"
    I agree with the comments about the quality of the example.... it was a quick and dirty hack. It dosen't take away from the issue that it works on every combination except windows and cygwin....
    Any other thoughts?
    import java.util.*;
    import java.io.*;
    class StreamGobbler extends Thread {
      InputStream is;
      String type;
      StreamGobbler(InputStream is, String type) {
        this.is = is;
        this.type = type;
      public void run() {
        try {
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line = null;
          while (true) {
            if (is.available() == 0) {
              try {
                Thread.sleep(10);
              } catch (InterruptedException ie) {
              continue;
            line = br.readLine();
            if (line == null) {
              System.out.println("Error");
            } else {
              System.out.println(line);
        } catch (IOException ioe) {
          ioe.printStackTrace();
    public class myexec {
      public static void main(String[] args) {
        try {
          String osName = System.getProperty("os.name");
          System.out.println(osName);
          Runtime rt = Runtime.getRuntime();
          Process proc = rt.exec(args);
          // any error message?
          StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
          // any output?
          StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
          // kick them off
          errorGobbler.start();
          outputGobbler.start();
          // any error???
          int exitVal = proc.waitFor();
          System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t) {
          t.printStackTrace();
    }

  • Java Swing and Microsoft Excel

    How do you open a Microsoft Excel  file from swing?
    please give a example.

    Hi,
    try these APIs:
    [http://poi.apache.org/]
    [http://jexcelapi.sourceforge.net/]
    Once you get the content of the files, it's up to you how you display it in your swing application.
    Demos and tutorials can be found on these sites as well.

  • Flash Player 10.3 and Windows 7 control panel

    Hello,
    I have installed flash player 10.3 in my system Windows 7 Prof. 32 with classic design but the text in the control panel is incomplete
    http://www.picfront.de/d/8chS
    I have read that with aero design the text is complete.

    Yes, unfortunately there is an issue with the Native Control Panel under the Classic theme. If you use the standard Windows XP theme or the Aero-based themes, the Native Control Panel text should not be truncated.

  • Java time and windows Regional Option

    I want to return a time,so ,how is set on window's regional option.
    I set time on windows Regional Option so - 13:38:29, bat java return another format 1:38:29.
    for example. time is 13:40 PM on windows.
    Calendar date=Calendar.getInstance();
    date.get(Calendar.HOUR); this returns 1 not 13.
    What to do to return 13 not 1.

    Google (or even just the search box on this forum) is your friend:
    http://forum.java.sun.com/thread.jspa?forumID=16&threadID=318720
    http://forum.java.sun.com/thread.jspa?forumID=16&threadID=244377
    http://forum.java.sun.com/thread.jspa?forumID=16&threadID=544738
    http://forum.java.sun.com/thread.jspa?threadID=626159&messageID=9468678

  • Java Swing/AWT and FX is so old school! Give me HTML and CSS for GUI!

    Dear Java,
    I am a seasoned programmer and I feel it's time JAVA implements a GUI system where it uses HTML and CSS for the GUI. For the love of god just look at the interfaces you can make using HTML and CSS alone. I am a big fan of Java Swing and the recent GUI designer for FX is quite cool. But they are just not as simple as HTML and CSS. And JavaFX has some interesting requirements for the graphics.
    I know it is possible to use JavaFX and implement the WebView/WebDriver and make it load a HTML page, etc... but why go through all the trouble?
    Just imagine... if you make Java where it has powerful back-end to do what it does best and the HTML/CSS powered GUI on the front-end. It will make the lives of many developers much much easier.
    I am not sure whether a Swing designed GUI will be faster than a HTML designed GUI... but if you look at a traditional browser and how fast it renders HTML/CSS, I am sure if Java had a native Form where it uses HTML and CSS to render the GUI, Java will make the dreams of many programmers a reality.
    Make it happen!!!!

    Check this i solve problem just now using this
    https://wiki.archlinux.org/index.php/Ja … ow_Manager

  • Problem with java look and feel

    Hi! This is my first time posting here. Do apologize me if I am not familiar with the regulations here. Thanks!
    Currently, I am developing a project using NetBeans IDE. It is using RMI, and some basic UI. I am facing the following error when I tried applying the java look and feel code. Please see below for the code used and the error message.
    try {   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) { }
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.plaf.ColorUIResource cannot be cast to java.util.List
    at javax.swing.plaf.metal.MetalUtils.drawGradient(MetalUtils.java:196)
    at javax.swing.plaf.metal.MetalInternalFrameTitlePane.paintComponent(MetalInternalFrameTitlePane.java:384)
    at javax.swing.JComponent.paint(JComponent.java:1027)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
    at javax.swing.JComponent.paint(JComponent.java:1013)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1797)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Java Result: 1

    Thanks for everyone's help!
    Below is the executable code generated using NetBeans which is enough to generate the error message. Sometimes you can get the error message just by running the program. Sometimes the error will occur when you go into the Menu and click on Item.
    * NewJFrame.java
    * Created on January 8, 2008, 1:11 PM
    package client;
    import javax.swing.UIManager;
    * @author  Yang
    public class NewJFrame extends javax.swing.JFrame {
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
        private void initComponents() {
            jDesktopPane1 = new javax.swing.JDesktopPane();
            jInternalFrame1 = new javax.swing.JInternalFrame();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
            jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
            jInternalFrame1Layout.setHorizontalGroup(
                jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 190, Short.MAX_VALUE)
            jInternalFrame1Layout.setVerticalGroup(
                jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 95, Short.MAX_VALUE)
            jInternalFrame1.setBounds(80, 40, 200, 130);
            jDesktopPane1.add(jInternalFrame1, javax.swing.JLayeredPane.DEFAULT_LAYER);
            jMenu1.setText("Menu");
            jMenuItem1.setText("Item");
            jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem1ActionPerformed(evt);
            jMenu1.add(jMenuItem1);
            jMenuBar1.add(jMenu1);
            setJMenuBar(jMenuBar1);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
                    .addGap(20, 20, 20))
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
            pack();
        }// </editor-fold>                       
        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
            jInternalFrame1.setVisible(true);
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
        // Variables declaration - do not modify                    
        private javax.swing.JDesktopPane jDesktopPane1;
        private javax.swing.JInternalFrame jInternalFrame1;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        // End of variables declaration                  
    }Edited by: Boxie on Jan 7, 2008 11:23 PM

  • Chess: java swing help need

    Hi, I have just learn java swing and now creating a chess game. I have sucessfully create a board and able to display chess pieces on the board. By using JLayeredPane, I am able to use drag and drop for the piece. However, there is a problem which is that I can drop the chess piece off the board and the piece would disappear. I have try to solve this problem by trying to find the location that the chess piece is on, store it somewhere and check if the chess piece have been drop off bound. However, I could not manage to solve it. Also, I am very confuse with all these layer thing.
    Any suggestion would be very helpful. Thanks in advance
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
    Object currentPosition;
    Color currentColor;
    Dimension boardSize = new Dimension(600, 600);
    JLayeredPane layeredPane;
    JPanel board;
    JPanel box;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    public ChessBoard()
    // Use a Layered Pane for this this application
    layeredPane = new JLayeredPane();
    getContentPane().add(layeredPane);
    layeredPane.setPreferredSize( boardSize );
         layeredPane.addMouseListener( this );
    layeredPane.addMouseMotionListener( this );
    // Add a chess board to the Layered Pane
    board = new JPanel();
         // set "board" to the lowest layer (default_layer)
    layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
    board.setLayout( new GridLayout(8, 8) );
    board.setPreferredSize( boardSize );
    board.setBounds(0, 0, boardSize.width, boardSize.height);
         addShade();
         addPiece();
    public void addShade()
         for (int i = 0; i < 64; i++)
    box = new JPanel( new BorderLayout() );
    board.add( box, BorderLayout.CENTER );
         if( ((i / 8) % 2) == 0)
                   if( (i % 2) ==0)
                        box.setBackground(Color.lightGray);
                   } else {
                        box.setBackground( Color.white);
              } else {
                   if( (i % 2) ==0)
                        box.setBackground(Color.white);
                   } else {
                        box.setBackground(Color.lightGray);
    //Method for adding chess pieces to the board
    public void addPiece()
    // Add a few pieces to the board
         //black pieces
         for (int i = 0; i < 64; i++)
              //adding black pieces
              if (i < 8)
                   String fileName = i + ".gif";
                   JLabel blackPieces = new JLabel( new ImageIcon( fileName ) );
                   JPanel panel = (JPanel)board.getComponent( i );
                   panel.add( blackPieces );
              //adding black pones
              if ((i > 7) && (i < 16))
                   String fileName = "8.gif";
                   JLabel blackPones = new JLabel( new ImageIcon( fileName ) );
                   JPanel panel = (JPanel)board.getComponent( i );
                   panel.add( blackPones );
              //jump to white position (Component 48)
              if (i == 16) i = 48;
              //adding white pones
              if(( i > 47 ) && (i < 56))
                   JLabel whitePones = new JLabel( new ImageIcon("48.gif") );
                   JPanel panel = (JPanel)board.getComponent( i );
                   panel.add( whitePones );
              //adding white pieces
              if (i > 55)
                   String fileName = i + ".gif";
                   JLabel whitePieces = new JLabel( new ImageIcon( fileName ) );
                   JPanel panel = (JPanel)board.getComponent( i);
                   panel.add( whitePieces );
    ** Add the selected chess piece to the dragging layer so it can be moved
    public void mousePressed(MouseEvent e)
    chessPiece = null;
    Component c = board.findComponentAt(e.getX(), e.getY());
         c.setBackground(Color.red);
    if (c instanceof JPanel) return;
    Point parentLocation = c.getParent().getLocation();
    xAdjustment = parentLocation.x - e.getX();
    yAdjustment = parentLocation.y - e.getY();
    chessPiece = (JLabel)c;
    chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
    chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
    layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    ** Move the chess piece around
    public void mouseDragged(MouseEvent me)
    if (chessPiece == null) return;
    chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
    ** Drop the chess piece back onto the chess board
    public void mouseReleased(MouseEvent e)
         Component c = board.findComponentAt(e.getX(), e.getY());
         int x = (int)c.getBounds().getX();
         int y = (int)c.getBounds().getY();
         System.out.println(c.getLocation());
         System.out.println(x + " "+ y);
         c.setBackground(currentColor);
    if (chessPiece == null) return;
    chessPiece.setVisible(false);
    if (c instanceof JLabel)
    Container parent = c.getParent();
         //remove the piece that is capture
    parent.remove(0);
    parent.add( chessPiece );
    else
    Container parent = (Container)c;
    parent.add( chessPiece );
    chessPiece.setVisible(true);
    public void mouseClicked(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    private static void createAndShowGUI()
         //Make sure we have nice window decorations
         JFrame.setDefaultLookAndFeelDecorated(true);
         JFrame frame = new ChessBoard();
         frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
         //Display the window
         frame.pack();
         frame.setResizable( false );
         frame.setLocationRelativeTo( null );
         frame.setVisible(true);
    public static void main(String[] args)
         //Schedule a job for the event-dispatching thread:
         //creating and showing this application's GUI
         javax.swing.SwingUtilities.invokeLater( new Runnable()
                   public void run()
                        createAndShowGUI();
    }

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class ChessBoardRx extends JFrame implements MouseListener, MouseMotionListener
        Object currentPosition;
        Color currentColor;
        Dimension boardSize = new Dimension(600, 600);
        JLayeredPane layeredPane;
        JPanel board;
        JPanel box;
        JLabel chessPiece;
        int xAdjustment;
        int yAdjustment;
        Container lastParent;
        public ChessBoardRx()
            // Use a Layered Pane for this this application
            layeredPane = new JLayeredPane();
            getContentPane().add(layeredPane);
            layeredPane.setPreferredSize( boardSize );
            layeredPane.addMouseListener( this );
            layeredPane.addMouseMotionListener( this );
            // Add a chess board to the Layered Pane
            board = new JPanel();
            // set "board" to the lowest layer (default_layer)
            layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
            board.setLayout( new GridLayout(8, 8) );
            board.setPreferredSize( boardSize );
            board.setBounds(0, 0, boardSize.width, boardSize.height);
            addShade();
            addPiece();
    //        populateBoard();
        public void addShade()
            for (int i = 0; i < 64; i++)
                box = new JPanel( new BorderLayout() );
                board.add( box, BorderLayout.CENTER );
                if( ((i / 8) % 2) == 0)
                    if( (i % 2) ==0)
                        box.setBackground(Color.lightGray);
                    } else {
                        box.setBackground( Color.white);
                } else {
                    if( (i % 2) ==0)
                        box.setBackground(Color.white);
                    } else {
                        box.setBackground(Color.lightGray);
        //Method for adding chess pieces to the board
        public void addPiece()
            // Add a few pieces to the board
            //black pieces
            for (int i = 0; i < 64; i++)
                //adding black pieces
                if (i < 8)
                    String fileName = i + ".gif";
                    JLabel blackPieces = new JLabel( new ImageIcon( fileName ) );
                    JPanel panel = (JPanel)board.getComponent( i );
                    panel.add( blackPieces );
                //adding black pones
                if ((i > 7) && (i < 16))
                    String fileName = "8.gif";
                    JLabel blackPones = new JLabel( new ImageIcon( fileName ) );
                    JPanel panel = (JPanel)board.getComponent( i );
                    panel.add( blackPones );
                //jump to white position (Component 48)
                if (i == 16) i = 48;
                //adding white pones
                if(( i > 47 ) && (i < 56))
                    JLabel whitePones = new JLabel( new ImageIcon("48.gif") );
                    JPanel panel = (JPanel)board.getComponent( i );
                    panel.add( whitePones );
                //adding white pieces
                if (i > 55)
                    String fileName = i + ".gif";
                    JLabel whitePieces = new JLabel( new ImageIcon( fileName ) );
                    JPanel panel = (JPanel)board.getComponent( i);
                    panel.add( whitePieces );
        private void populateBoard() {
            int[][] pos = {
                { 9, 7, 5, 1, 3, 5, 7, 9 },
                { 8, 6, 4, 0, 2, 4, 6, 8 }
            for(int j = 0; j < 8; j++) {
                String fileName = "chessImages/" + pos[0][j] + ".jpg";
                JLabel label = new JLabel( new ImageIcon( fileName ) );
                JPanel panel = (JPanel)board.getComponent( j );
                panel.add( label );
            for(int j = 8; j < 16; j++) {
                String fileName = "chessImages/" + 11 + ".jpg";
                JLabel label = new JLabel( new ImageIcon( fileName ) );
                JPanel panel = (JPanel)board.getComponent( j );
                panel.add( label );
            for(int j = 56, k = 0; j < 64; j++, k++) {
                String fileName = "chessImages/" + pos[1][k] + ".jpg";
                JLabel label = new JLabel( new ImageIcon( fileName ) );
                JPanel panel = (JPanel)board.getComponent( j );
                panel.add( label );
            for(int j = 48; j < 56; j++) {
                String fileName = "chessImages/" + 10 + ".jpg";
                JLabel label = new JLabel( new ImageIcon( fileName ) );
                JPanel panel = (JPanel)board.getComponent( j );
                panel.add( label );
         ** Add the selected chess piece to the dragging layer so it can be moved
        public void mousePressed(MouseEvent e)
            chessPiece = null;
            Component c = board.findComponentAt(e.getX(), e.getY());
            c.setBackground(Color.red);
            if (c instanceof JPanel) return;
            lastParent = c.getParent();
            Point parentLocation = c.getParent().getLocation();
            xAdjustment = parentLocation.x - e.getX();
            yAdjustment = parentLocation.y - e.getY();
            chessPiece = (JLabel)c;
            chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
            chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
            layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
         ** Move the chess piece around
        public void mouseDragged(MouseEvent me)
            if (chessPiece == null) return;
            chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
         ** Drop the chess piece back onto the chess board
        public void mouseReleased(MouseEvent e)
            Component c = board.findComponentAt(e.getX(), e.getY());
            if(c == null) {
                layeredPane.remove(chessPiece);
                lastParent.add(chessPiece);
                Rectangle r = lastParent.getBounds();
                chessPiece.setLocation(r.x+xAdjustment, r.y+yAdjustment);
                lastParent.validate();
                lastParent.repaint();
                return;
            int x = (int)c.getBounds().getX();
            int y = (int)c.getBounds().getY();
            System.out.println(c.getLocation());
            System.out.println(x + " "+ y);
            c.setBackground(currentColor);
            if (chessPiece == null) return;
            chessPiece.setVisible(false);
            if (c instanceof JLabel)
                Container parent = c.getParent();
                //remove the piece that is capture
                parent.remove(0);
                parent.add( chessPiece );
            else
                Container parent = (Container)c;
                parent.add( chessPiece );
            chessPiece.setVisible(true);
        public void mouseClicked(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        private static void createAndShowGUI()
            //Make sure we have nice window decorations
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new ChessBoardRx();
            frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
            //Display the window
            frame.pack();
            frame.setResizable( false );
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        public static void main(String[] args)
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI
            javax.swing.SwingUtilities.invokeLater( new Runnable()
                public void run()
                    createAndShowGUI();
    }

Maybe you are looking for

  • Third party dropshipment process and system setup

    Hi gentlemen, I have a few question to check with you. 1 - What is the difference between Third Party Shipment, and Third Party Drop Shipment in term of process? 2 - What is the system setup requirement for Third Party Drop shipment (I know TAS is re

  • Compliance Calibrator Issue  with Backend system

    Hi Experts, We have situation where we are unable to run any risk analysis from CC to our backend system. The error we are getting is " Can not execute BAPI USERLIST, Invalid value in OPTION field of value table for IN itab operator" and the RFC ID i

  • How do I change Margins to Inches, mine are millimeters?

    I have margins set to Millimeters, have changed all to US since I had it set to Canada - I need inches for my margins? I am sure I answered a wrong question when setting up Firefox but now I can't undo my error.

  • ITunes Store Window too wide

    The iTunes window is too wide, and the columns too far apart. When I want to purchase a tune, I can't see the BUY icon from the song list, and if I move the screen to the right, I can't see which song I'm purchasing. I have to keep moving the screen

  • After update to 10.1 most projects are gone

    I just updated from FCP X to FCP 10.1 During the update it wanted to update all my projects and events to libraries. I did get a note about some missing files that may need to be relinked, but that's it. However after the update, only one project mad