Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 7 Taskbar Progress Bar in Java

I found out that this feature is included in Java 9. It is part of AWT and it is quity simple too use.

Here is short example:

import java.awt.Taskbar;
import java.awt.Taskbar.State;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 * @author fxl
 */
public class TaskbarSample {

    public static void main(String[] args) {

        // JavaDoc:
        // https://docs.oracle.com/javase/9/docs/api/java/awt/Taskbar.html

        // MSDNDoc:
        // https://msdn.microsoft.com/en-us/library/dd391692(VS.85).aspx
        if (Taskbar.isTaskbarSupported() == false) {
            return;
        }

        JFrame dialog = new JFrame("Test - 50%");
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);    


        Taskbar taskbar = Taskbar.getTaskbar();
        taskbar.setWindowProgressState(dialog, State.ERROR);
        taskbar.setWindowProgressValue(dialog, 50);     
    }

}

this is now possible using SWT please review the code example:
org.eclipse.swt.snippets.Snippet336


This example will do the job:

Task bar:
enter image description here

Code:

import org.bridj.Platform;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.cpp.com.shell.ITaskbarList3;
import org.bridj.jawt.JAWTUtils;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TaskBarListDemo extends JFrame implements ActionListener, ChangeListener
{
    private ITaskbarList3 list;
    private JSlider slider;
    private Pointer<?> hwnd;

    private TaskBarListDemo() throws ClassNotFoundException
    {
        super("TaskbarList Demo (" + (Platform.is64Bits() ? "64 bits" : "32 bits") + ")");

        list = COMRuntime.newInstance(ITaskbarList3.class);

        getContentPane().add("Center", new JLabel("Hello Native Windows 7 World !"));
        Box box = Box.createVerticalBox();

        int min = 0;
        int max = 300;
        int val = (min + max / 2);
        slider = new JSlider(min, max, val);
        slider.addChangeListener(this);
        box.add(slider);

        ButtonGroup group = new ButtonGroup();
        for (ITaskbarList3.TbpFlag state : ITaskbarList3.TbpFlag.values())
        {
            JRadioButton cb = new JRadioButton(state.name());
            group.add(cb);
            cb.putClientProperty(ITaskbarList3.TbpFlag.class, state);
            cb.setSelected(state == ITaskbarList3.TbpFlag.TBPF_NORMAL);
            cb.addActionListener(this);
            box.add(cb);
        }
        getContentPane().add("South", box);

    }

    @Override
    protected void finalize() throws Throwable
    {
        super.finalize();
        list.Release();
    }

    public void setVisible(boolean visible)
    {
        super.setVisible(visible);

        long hwndVal = JAWTUtils.getNativePeerHandle(this);
        hwnd = Pointer.pointerToAddress(hwndVal);
        list.SetProgressValue((Pointer) hwnd, slider.getValue(), slider.getMaximum());
    }

    @Override
    public void stateChanged(ChangeEvent actionEvent)
    {
        list.SetProgressValue((Pointer) hwnd, slider.getValue(), slider.getMaximum());
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent)
    {
        JRadioButton button = ((JRadioButton) actionEvent.getSource());
        if (button.isSelected())
        {
            ITaskbarList3.TbpFlag flag = (ITaskbarList3.TbpFlag) button.getClientProperty(ITaskbarList3.TbpFlag.class);
            list.SetProgressValue((Pointer) hwnd, slider.getValue(), slider.getMaximum());
            list.SetProgressState((Pointer) hwnd, flag);
        }
    }

    public static void main(String[] arguments) throws Exception
    {
        TaskBarListDemo f = new TaskBarListDemo();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

Maven dependencies:

<dependencies>
    <dependency>
        <groupId>com.nativelibs4java</groupId>
        <artifactId>bridj</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>LATEST</version>
    </dependency>
</dependencies>

There is no standard facility in Java for doing so, yet.

Hence you need to talk to Windows directly to do that. So you need to locate the correct Windows routine, and use JNA (probably the easiest) to invoke that routine. I do not know of a vendor or a project who has done this already.


Edit: It appears that the http://code.google.com/p/nativelibs4java/ project may do what you want.