Hot questions for Using Ubuntu in swing

Question:

I have written a simple Java animation program in Ubuntu 14.4.1. A ball moving inside a JPanel. But at execution, the ball moves quite jerky in the JPanel. This problem continues until I move the mouse inside the JPanel. At the time of moving the mouse inside the JPanel the ball movement is quite smooth. It should be said that I've run this program in Windows 10, and no problem occurred. The code for my program is as follows:

import java.awt.*;
import javax.swing.*;

public class BouncingBall extends JPanel {
    Ball ball = new Ball();

    void startAnimation() {
        while( true ) {
            try {
                Thread.sleep( 25 );
                ball.go();
                repaint();
            } catch( InterruptedException e ) {}
        } // end while( true )
    } // end method startAnimation()

    protected void paintComponent( Graphics g ) {
        super.paintComponent( g );
        ball.draw( g );
    } // end method paintComponent


    // inner class Ball
    class Ball {
        int x;
        int y;
        int diameter = 10;
        int xSpeed = 100;
        int ySpeed = 70;

        void go() {
            x = x + (xSpeed*25)/1000;
            y = y + (ySpeed*25)/1000;

            int maxX = getWidth() - diameter;
            int maxY = getHeight() - diameter;
            if( x < 0 ) {
                // bounce at the left side
                x = 0;
                xSpeed = -xSpeed;
            } else if( x > maxX ) {
                // bounce at the right side
                x = maxX;
                xSpeed = -xSpeed;
            } else if( y < 0 ) {
                // bounce at the top side
                y = 0;
                ySpeed = -ySpeed;
            } else if( y > maxY ) {
                // bounce at the bottom size
                y = maxY;
                ySpeed = -ySpeed;
            } // end if-else block
        } // end method go()

        void draw( Graphics g ) {
            g.fillOval( x , y , diameter , diameter );
        } // end method draw
    } // end inner class Ball


    public static void main( String[] args ) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        BouncingBall animation = new BouncingBall();
        animation.setPreferredSize( new Dimension( 500 , 500 ) );
        animation.setBackground( Color.white );
        window.add( animation );
        window.pack();
        window.setVisible( true );

        animation.startAnimation();
    } // end method main
} // end class BouncingBall

What is the problem? Do I have to change some settings in my Ubuntu? I've also put some test code inside the paintComponent method as follows:

protected void paintComponent( Graphics g ) {
    System.out.println( "paintComponent call number: " + counter );
    ++counter;
    super.printComponent( g );
    ball.draw( g );
}  

with variable counter initial value of 0 declared in class MovingBall. I observed that the number of paintComponent's calls per second is much more than the actual refresh rate of the JPanel as it appears.


Answer:

Video acceleration is enabled by default in Windows, but is not enabled by default in Linux. (This has been true for many years now; I could have sworn this default was changed for recent Java releases, but evidently I was wrong.)

You can enable OpenGL to get accelerated performance:

public static void main( String[] args ) {
    System.setProperty("sun.java2d.opengl", "true");

    JFrame window = new JFrame();

Alternatively, you can set the property on the command line:

java -Dsun.java2d.opengl=true BouncingBall

Question:

I'm using UBUNTU/LINUX I'm trying to build swing application for myself(this app need to work other platforms too) and i can not execute some commands on java.I tried to execute "java -version" Here is my code:

 Runtime run = Runtime.getRuntime();

    Process p = run.exec("java -version");

    try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {

        for (String output = br.readLine(); output != null; output = br.readLine()) {
            System.out.println(output);
        }

    } catch (IOException e) {
        System.out.println(e);
    }

It returns blank page no output!

However i can execute run.exec("ls") / or ("gedit") and so on... and I GET DATA Also i can execute internal programs that in my computer. Why i cant execute .sh files or built-in java commands and getting blank page?


Answer:

You should use ProcessBuilder to mix standard and error outputs:

    ProcessBuilder run = new ProcessBuilder("java", "-version");
    run.redirectErrorStream(true);

    Process p = run.start();

    try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {

        for (String output = br.readLine(); output != null; output = br.readLine()) {
            System.out.println(output);
        }

    } catch (IOException e) {
        System.out.println(e);
    }

Check that java parent directory is in your PATH variable (output System.getenv("PATH")) or use a full path to the binary.

For script, use /bin/sh like that ProcessBuilder run = new ProcessBuilder("/bin/sh", "/path/to/your/script");

All path can be absolute starting with /, or relative (without /) to System.getProperty("user.dir").

Question:

i'm new in java . i just learn JPanel and JFrame. i got this note from java software solutions:

" The pack method of the frame sets its size appropriately based on its contents—in this case the frame is sized to accommodate the size of the panel it contains."

so i wrote this code :

public static void main (String [] args){
    JFrame frame = new JFrame("test");
    JPanel panel = new JPanel();
    JLabel label1= new JLabel("");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    //frame.setSize(1000, 500);
    frame.getContentPane().add(panel);
    Color darkBlue = new Color(8,40,94);
    panel.setSize(1000, 500);
    panel.setBackground(darkBlue);
}

but it the result is a really tiny window that i should maximize it with mouse to see the content but when i set frame size every thing work great! and i use Ubuntu. so what's the reason of this problem?


Answer:

From the order of your code:

JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
JLabel label1= new JLabel("");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();

You did not add anything into the frame before you pack() it. pack() means let the frame decide its size based on the components being added to it.

Since you have no components added to it before you pack() it, you receive a small window with visually nothing inside (until you resize the window).

When the frame is being resized, paintManager will be consulted to paint the contentPane, hence if you add before pack(), not only the frame will be resized nicely for you, the components within it will be painted as well.


To see the components within the JFrame:

public static void main (String [] args){
    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JLabel label1= new JLabel("");
    panel.add(label1);    //Add label to panel
    frame.add(panel);     //Add panel (with label) to frame 
    frame.pack();         //Let the frame adjust its size based on the added components
    frame.setVisible(true);
}

Question:

Well I'm following this tutorial to create my own animation and when I run it everything works well on Windows but in Ubuntu 17.04 the animation is smooth only if the mouse is moving over the app window. If don't it works like " in fits and starts" (I think is a good translation). here is my code:

import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JFrame {

private Panel pan = new Panel();
private JButton play = new JButton("play");
private JButton pause = new JButton("pause");

public Window(String titre) {

    this.setTitle(titre);
    this.setSize(900, 600);
    this.setLocationRelativeTo(null);
    this.setAlwaysOnTop(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box b1 = Box.createHorizontalBox();
    b1.add(play);
    b1.add(pause);

    Box b2 = Box.createVerticalBox();
    b2.add(b1);
    this.setContentPane(pan);
    this.getContentPane().add(b2);
    this.setVisible(true);
    this.go();

}

private void go() {
    // Les coordonnées de départ de notre rond
    int x = pan.getPosX(), y = pan.getPosY();
    // Le booléen pour savoir si l'on recule ou non sur l'axe x
    boolean backX = false;
    // Le booléen pour savoir si l'on recule ou non sur l'axe y
    boolean backY = false;

    // Dans cet exemple, j'utilise une boucle while
    // Vous verrez qu'elle fonctionne très bien
    while (true) {
        // Si la coordonnée x est inférieure à 1, on avance
        if (x < 1)
            backX = false;

        // Si la coordonnée x est supérieure à la taille du Panneau moins la taille du
        // rond, on recule
        if (x > pan.getWidth() - 50)
            backX = true;

        // Idem pour l'axe y
        if (y < 1)
            backY = false;
        if (y > pan.getHeight() - 50)
            backY = true;

        // Si on avance, on incrémente la coordonnée
        // backX est un booléen, donc !backX revient à écrire
        // if (backX == false)
        if (!backX)
            pan.setPosX(++x);

        // Sinon, on décrémente
        else
            pan.setPosX(--x);

        // Idem pour l'axe Y
        if (!backY)
            pan.setPosY(++y);
        else
            pan.setPosY(--y);

        // On redessine notre Panneau
        pan.repaint();

        // Comme on dit : la pause s'impose ! Ici, trois millièmes de seconde
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}

seconde class:

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Panel extends JPanel {

private int posX = -50;
private int posY = -50;

public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    g.setColor(Color.red);
    g.fillOval(posX, posY, 50, 50);
}

public int getPosX() {
    return posX;
}

public void setPosX(int posX) {
    this.posX = posX;
}

public int getPosY() {
    return posY;
}

public void setPosY(int posY) {
    this.posY = posY;
}
}

and the main:

import javax.swing.JFrame;
public class test {
public static void main(String [] args) {
    Window window = new Window("Animation");

}

}

Thank you in advance to any one who may be able to give me some ideas :)


Answer:

Try calling

Toolkit.getDefaultToolkit().sync();

After you do

pan.repaint();

Question:

It seems that there is a bug with Ubuntu (maybe only unity). The decoration of the JFrame is taken into account for getLocation() and getSize(), but not for setLocation() and setSize(). This leads to weird behaviour. For instance, if you use pack() after the frame is displayed and the dimensions changed, the frame will go down 20 pixels...

To illustrate a concrete case when it becomes really annoying, I made a SSCCE. It's a JFrame with a basic JPanel. If you drag the panel, the JFrame is supposed to move along.

Under Windows, it works as expected. Under Ubuntu, if I do setUndecorated(true) it will also work fine, but if I let the decoration, the JFrame turn crazy !

public class Test {
    private static JFrame mainFrame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(300,20);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                Container pane = mainFrame.getContentPane();
                pane.addMouseMotionListener(new MouseMotionListener() {
                    private int posX = 0, posY = 0;

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        int x = e.getX() - posX + mainFrame.getX();
                        int y = e.getY() - posY + mainFrame.getY();
                        mainFrame.setLocation(x, y);
                    }

                    @Override
                    public void mouseMoved(MouseEvent e) {
                        posX = e.getX();
                        posY = e.getY();
                    }

                });

            }
        });
    }
}

I don't know how I can fix that. How can I get the size of the windows decoration ? And I have no idea about which versions of Ubuntu are concerned. And if it is only a Unity problem, I don't even know how to find out if my user is using Unity...

Any idea for a workaround ?

Edit : Ok, MadProgrammer did provide a better code, but the bug still occurs sometimes. I edited my MouseListener accordingly to track the bug :

            pane.addMouseMotionListener(new MouseMotionListener() {
                private int posX = 0, posY = 0;

                @Override
                public void mouseDragged(MouseEvent e) {
                    int x = e.getXOnScreen() - posX;
                    int y = e.getYOnScreen() - posY;
                    mainFrame.setLocation(x, y);
                    System.out.println("drag   :  border ignored / border considered : "+(mainFrame.getY()+e.getY())+"   /   "+e.getYOnScreen());
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                    posX = e.getXOnScreen() - mainFrame.getX();
                    posY = e.getYOnScreen() - mainFrame.getY();
                    System.out.println("move  :  border ignored / border considered : "+e.getY()+"   /   "+posY);
                }
            });

Each time that the 2 values are identical, it means that the bug will occur on the next click. Otherwise, the values are different. On other OS, the values are always the same. Actually, they should be or the same always, or always different. I don't understand how they can be sometimes equal and sometimes different...


Answer:

I don't have Ubuntu to test with, but I've used something similar to this on both MacOS and Windows

import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    private static JFrame mainFrame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(300, 100);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                Container pane = mainFrame.getContentPane();
                MouseAdapter ma = new MouseAdapter() {
                    private Point offset;

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (offset != null) {
                            Point pos = e.getLocationOnScreen();

                            int x = pos.x - offset.x;
                            int y = pos.y - offset.y;

                            System.out.println(x + "x" + y);

                            SwingUtilities.getWindowAncestor(e.getComponent()).setLocation(x, y);
                        }
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {

                        Point pos = SwingUtilities.getWindowAncestor(e.getComponent()).getLocation();

//                      Point pos = e.getComponent().getLocationOnScreen();
                        offset = new Point(e.getLocationOnScreen());
                        System.out.println(pos + "/" + offset);
                        offset.x -= pos.x;
                        offset.y -= pos.y;

                        System.out.println(offset);
                    }
                };
                pane.addMouseListener(ma);
                pane.addMouseMotionListener(ma);
            }
        });
    }
}

This should work for both decorated and undecorated windows, as it takes the difference between the positions of the component (on the screen) and the windows current position. When dragged, it calculates the distance of movement from the click point and updates the window's location accordingly (allowing for the original offset of the click)

Question:

I have created a Java swing application with an embedded Jx Browser for displaying html and JavaScript pages in application. The application is build in windows environment using Net beans IDE 8.0, but when i try to run it on Ubuntu using command

java -jar demo.jar

enter image description here i get an error

"Failed to extract chromium binaries in /tmp/jxbrowser-chromium-55.0............"

I have already checked that all the jxbrowser libraries a propely imported, it runs successfully​ on windows, i think it can be the problem of permission to /temp directory, so i tried to give permission to my demo.jar file from properties in ubuntu but no luck.

The Java app contains only single main class with following code copied from JxBrowser quick start guide

package pkgtry;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;


public class Try {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadURL("http://www.google.com");
    }
}

I can't figure out whats the problem, please anyone help me out !

jxbrowser-browser.log

Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.Environment traceEnvironment INFO: OS name: Linux Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.Environment traceEnvironment INFO: OS version: 4.10.0-19-generic Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.Environment traceEnvironment INFO: JRE version: 1.8.0_131 64-bit Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.Environment traceEnvironment INFO: JxBrowser version: 6.14.1 Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.Environment traceEnvironment INFO: User temp dir: /tmp Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary INFO: Chromium binaries dir: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1 Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary INFO: Analyzing Chromium binaries dir... Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: The directory doesn't have the following files: Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/he.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/sv.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/mr.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/sk.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/da.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/uk.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/fa.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/zh-TW.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/kn.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/chrome_100_percent.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/icudtl.dat Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/libjxbrowser-chromium64.so Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/gu.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/sr.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/vi.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ja.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/nl.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/te.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/am.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/pl.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/en-US.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/jxbrowser-chromium64 Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ta.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/et.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/bg.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/th.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/resources.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/it.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/lv.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/fr.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ro.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/hr.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/bn.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/fil.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/pt-PT.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ca.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/libjxbrowser-common64.so Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/el.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/snapshot_blob.bin Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/cs.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/es.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/hu.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/tr.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/en-GB.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ml.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/es-419.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/sw.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ms.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/sl.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/zh-CN.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ru.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/natives_blob.bin Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/id.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ko.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/ar.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/pt-BR.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/fi.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/hi.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/nb.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/de.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary FINE: /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/locales/lt.pak Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.b run INFO: Extracting Chromium binaries to '/tmp/jxbrowser-chromium-55.0.2883.87.6.14.1'... Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.XZExtractor a INFO: The '/chromium-linux64.7z' JAR resource URL is jar:file:/home/akb/Documents/try/dist/lib/jxbrowser-linux64-6.14.1.jar!/chromium-linux64.7z Jun 12, 2017 4:15:47 PM com.teamdev.jxbrowser.chromium.internal.XZExtractor a INFO: The '/7zr-linux' JAR resource URL is jar:file:/home/akb/Documents/try/dist/lib/jxbrowser-linux32-6.14.1.jar!/7zr-linux Jun 12, 2017 4:15:54 PM com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor extractChromiumBinariesIfNecessary SEVERE: Failed to extract Chromium binaries into /tmp/jxbrowser-chromium-55.0.2883.87.6.14.1 java.lang.RuntimeException: java.io.IOException: Cannot run program "/tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/7zr-linux": error=2, No such file or directory at com.teamdev.jxbrowser.chromium.internal.XZExtractor.extract(SourceFile:45) at com.teamdev.jxbrowser.chromium.internal.b.run(SourceFile:1067) at java.security.AccessController.doPrivileged(Native Method) at com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor.extractChromiumBinariesIfNecessary(SourceFile:61) at com.teamdev.jxbrowser.chromium.internal.ipc.IPC.(SourceFile:78) at com.teamdev.jxbrowser.chromium.internal.ipc.IPC.create(SourceFile:51) at com.teamdev.jxbrowser.chromium.internal.ipc.IPC$a.(SourceFile:41) at com.teamdev.jxbrowser.chromium.internal.ipc.IPC.getDefault(SourceFile:45) at com.teamdev.jxbrowser.chromium.BrowserContext.(SourceFile:63) at com.teamdev.jxbrowser.chromium.BrowserContext.(SourceFile:25) at com.teamdev.jxbrowser.chromium.Browser.(SourceFile:139) at pkgtry.Try.main(Try.java:29) Caused by: java.io.IOException: Cannot run program "/tmp/jxbrowser-chromium-55.0.2883.87.6.14.1/7zr-linux": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at java.lang.Runtime.exec(Runtime.java:620) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at com.teamdev.jxbrowser.chromium.internal.XZExtractor.extract(SourceFile:2018) ... 11 more Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:247) at java.lang.ProcessImpl.start(ProcessImpl.java:134) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) ... 15 more

Other Files jxbrowser-chromium.log, jxbrowser-ipc.log are empty


Answer:

JFYI: It works with JxBrowser 6.14 version.

Question:

I am just trying to have some of my JButtons to be painted fully orange in addition to the orange border when they have focus. Instead, setFocusPainted(true) turns out to only paint the border:

Many applications seem to use the full orange buttons for focus, but I struggle to find the correct solution for this in Java Swing.

I have no problems implementing my own custom JButtons or whatever. But I would think there should be a simple setting for this, because I shouldn't have to go and find out the RGB color values which Ubuntu uses so I can comply with native UI stuff.

I want this:

Pressing tab changes the JButton to its normal color as it loses focus.

I am obviously using the specific System's LAF. I used setFocusPainted(true) and all (otherwise the orange border wouldn't be there). It is also possible to get this when using MigLayouts specified buttons (okbutton etc.):

So what am I missing?


Answer:

You may be looking for the setDefaultButton() method of JRootPane, shown here, in concert with the GTKLookAndFeel.

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel …

Question:

Ever since my power went out mid project this program has been drawing my window off screen and I can't figure out why. Whenever it launches I can't move the window into a useful position, but when I alt-tab to see all the programs running on my computer I can see my jpanel. If it matters I am on Ubuntu using Eclipse. EDIT: I tested this with a totally different java program I didn't make and it does the same thing, so it appears to be an issue with my IDE/OS

I removed my code because it doesn't appear to have anything to do with the problem.


Answer:

I don't think this is a problem with your code, rather a slip up in the Ubuntu OS when they tried to make something more ergonomic. You might want to try running the program on a virtual machine or on another computer to see where the window is spawned on there. Usually when you move around a window on your screen and then close it, when the same program gets re-opened it will open in the same place it was closed at. This might even happen with all java applications for you now unless you move it and close it somewhere in the middle of your screen. You might be able to fix it by changing your resolution and trying to grab a corner of the screen but I'm not sure.

EDIT: Alt + F7 is a shortcut on ubuntu to move your current window. Move yours to the center of your screen and close it and you might be good.

EDIT: This is not mine but this script can be found here: https://github.com/mezga0153/offscreen-window-restore/blob/master/offscreen-window-restore.sh

#!/bin/bash
#
# This script moves back windows that have been moved off screen
#
# Author: Tine Mezgec tine.mezgec@gmail.com
#

width=`xrandr | grep current | awk {'print $8'}`

`wmctrl -l -G | awk -v w=$width '{
    if ($8 != "unity-dash" && $8 != "Hud") {
        if ($3 >= w || $3 < 0) {
            system("wmctrl -i -r " $1 " -e 0," sqrt($3*$3) % w ",-1,-1,-1");
        }
    }
}'`

This is all I could find, beware all your windows might open in the same place when the script is running though. You also might want to try moving the window with the arrow keys, closing and re-opening it then.

Question:


Answer:

You assume that

the only way for me to run UNIX commands is to download ubuntu bash for windows 10

, but it's not correct.

A rough search with the keyword "run linux command in windows" leads us to various solutions, above all, two, as stated here:

  1. Run Bash Shell from Windows 10
  2. Install Cygwin.

Actually, I have a third way: install Github for Windows to enable Bash in Windows. As stated here. This approach may be more limited to Cygwin, I haven't test it.

I have installed both Cygwin x64 and Github without conflict, so you can just use them in cmd console. I think your question may change into "How can I run Bash command in Java in Windows if the Bash is enabled?"

Now, I don't know if you have been aware of how to run Bash commands from Java. If so, you are done here. If not, ask Google.


A little more search before asking here is always helpful. Google is smarter than we think, and most of the time, even smarter than most of us, may we admit it or not.

Question:

So I have written a Java application, that provides a transparent Heads up display at the top of the screen, it works perfectly on windows, but on my kubuntu 16.04 machine it does not clear the old label when you change the labels text, you end up with a ton of overlapping mess.

because a picture is worth a thousand words, the top is how it looks in windows, the bottom is how it looks under kubuntu:

https://s23.postimg.org/yra0vvlvf/rawr.png

here is the code:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.net.URL;
import javax.swing.*;
import java.io.*;

public class spob extends JFrame implements WindowFocusListener
{
    public spob()
    {
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final TrayIcon trayIcon = new TrayIcon((new ImageIcon("icon.png", "trayicon")).getImage());
        final SystemTray tray = SystemTray.getSystemTray();
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("spO2 pr monitor");        
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }
        setType(javax.swing.JFrame.Type.UTILITY);
        setUndecorated(true);
        getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
        setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
        setSize(400, 35);
        JLabel label = new JLabel("Loading...");
        label.setFont(new Font("Tahoma", Font.BOLD, 28));
        label.setForeground(Color.GREEN);
        add(label);
        setLocation(800, 0);
        addWindowFocusListener(this);
        setAlwaysOnTop( true );
        this.setFocusable(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        URL url = null;
        BufferedReader in = null;
        String[] anArray = new String[10];
        anArray[0] = "<html><font color=green>- spO2:91  pr:65</font></html>";
        anArray[1] = "<html><font color=red>+ spO2:85  pr:77</font></html>";
        anArray[2] = "<html><font color=green>- spO2:90  pr:68</font></html>";
        anArray[3] = "<html><font color=orange>+ spO2:89  pr:76</font></html>";
        anArray[4] = "<html><font color=orange>- spO2:89  pr:72</font></html>";
        anArray[5] = "<html><font color=orange>+ spO2:88  pr:73</font></html>";
        anArray[6] = "<html><font color=red>- spO2:87  pr:78</font></html>";
        anArray[7] = "<html><font color=red>+ spO2:86  pr:73</font></html>";
        anArray[8] = "<html><font color=green>- spO2:92  pr:74</font></html>";
        anArray[9] = "<html><font color=green>+ spO2:90  pr:71</font></html>";
        while (true){
            try {
                Thread.sleep(200);
                //url = new URL("http://192.168.1.153/stat.php");
                //in = new BufferedReader(new InputStreamReader(url.openStream()));
                //label.setText(in.readLine().toString());
                Random randomno = new Random();
                label.setText(anArray[randomno.nextInt(9 - 1) + 1]);
            } catch (Exception ex) {
            } finally {
                //try {
                //  in.close();
                //} catch (IOException e) {
                //}
            }
        }
    }

    public void windowGainedFocus(WindowEvent e){}
    public void windowLostFocus(WindowEvent e)
    {
        if(e.getNewState()!=e.WINDOW_CLOSED){
            setAlwaysOnTop(false);
            setAlwaysOnTop(true);
        }

    }

    public static void main(String[] args) 
    {
    new spob();
    }
}

Answer:

So, a number of issues

  • You're violating the single threaded rules of Swing, essentially, updating the UI from outside the context of the EDT, this can cause issues if the system is trying to paint something while you're trying to update it
  • getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f)); - Swing doesn't know how to deal with opaque components which have an alpha based color, it tends to not to update the any of the components beneath it.

Transparent windows are ... fun ... they tend to introduce their own issues beyond what we would normally expect.

On my Mac system I was able to reproduce the issue, but inconsistently. This was especially apparent, because the Mac OS keeps rendering a shadow around the text.

The first thing I got rid of was setType(javax.swing.JFrame.Type.UTILITY);, I also added a repaint request of the label's parent container which seems to have solved the symptoms of the problem, but again, I was able to execute the code without at times.

If you want to update the UI periodically, you should use a Swing Timer, see How to use Swing Timers for more details. If you need to do something in the background and then update the UI, you should use a SwingWorker, have a look Worker Threads and SwingWorker for more details

(wow is me, it doesn't like my animated gif :()

The example deliberately uses a translucent background, it's intended to show the frame. Change pane.setAlpha(0.5f); to pane.setAlpha(0.0f); to make it fully transparent (I've tested that as well).

If you have issues, uncomment the line label.getParent().repaint(); in the Timer and see if that helps

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    private JLabel label;
    private String[] anArray = {
        "<html><font color=green>- spO2:91  pr:65</font></html>",
        "<html><font color=red>+ spO2:85  pr:77</font></html>",
        "<html><font color=green>- spO2:90  pr:68</font></html>",
        "<html><font color=orange>+ spO2:89  pr:76</font></html>",
        "<html><font color=orange>- spO2:89  pr:72</font></html>",
        "<html><font color=orange>+ spO2:88  pr:73</font></html>",
        "<html><font color=red>- spO2:87  pr:78</font></html>",
        "<html><font color=red>+ spO2:86  pr:73</font></html>",
        "<html><font color=green>- spO2:92  pr:74</font></html>",
        "<html><font color=green>+ spO2:90  pr:71</font></html>"
    };
    private Random randomno = new Random();

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setAlwaysOnTop(true);
                // Transparent window...
                frame.setBackground(new Color(255, 255, 255, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BackgroundPane pane = new BackgroundPane();
                // Set this to 0.0f to make it fully transparent
                pane.setAlpha(0.5f);
                pane.setLayout(new BorderLayout());

                pane.setBorder(new EmptyBorder(10, 10, 10, 10));

                frame.setContentPane(pane);

                label = new JLabel("Loading...");
                label.setFont(new Font("Tahoma", Font.BOLD, 28));
                label.setForeground(Color.GREEN);
                frame.add(label);

                frame.pack();
                Dimension size = frame.getSize();
                size.width = 400;
                frame.setSize(size);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(200, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        label.setText(anArray[randomno.nextInt(9 - 1) + 1]);
//                      label.getParent().repaint();
                    }
                });
                timer.start();
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private float alpha;

        public BackgroundPane() {
            setOpaque(false);
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        public float getAlpha() {
            return alpha;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(getBackground());
            g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

}

nb I'm not using openJDK, I'm using Java 8, that might make a difference

Testing for capabilities
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

public class Test {

    public static void main(String[] args) {
        GraphicsEnvironment ge
                = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        boolean isUniformTranslucencySupported
                = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
        boolean isPerPixelTranslucencySupported
                = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT);
        boolean isShapedWindowSupported
                = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT);

        System.out.println("isUniformTranslucencySupported = " + isUniformTranslucencySupported);
        System.out.println("isPerPixelTranslucencySupported = " + isPerPixelTranslucencySupported);
        System.out.println("isShapedWindowSupported = " + isShapedWindowSupported);
    }

}

Question:

Why does caret becomes invisible (after key pressed, or left-arrow) under java8 (oracle and openjdk) and higher ? (it works under java7). I am on ubuntu. It works fine on mac-os jdk1.8.0_51 and windows jdk1.8.0_65. Seems to be linked to this bug. It is spécific to JTextField (does not occur on JTextArea).

from OverwritableTextField

public class OverWriteCaret extends DefaultCaret {
    protected static final int MIN_WIDTH = 8;
    private static final Logger logger = Logger.getLogger(OverWriteCaret.class.getName());

    public static void main(String[] args) {
        JFrame f = new JFrame("Big caret");
        JTextField tf = new JTextField(20);
        tf.setCaret(new OverWriteCaret());
        f.getContentPane().add(tf, "North");
        f.pack();
        f.setVisible(true);
    }
    @Override
    protected synchronized void damage(Rectangle r) {
        if (r == null)
            return;
        try {
            JTextComponent comp = getComponent();
            TextUI mapper = comp.getUI();
            Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
            int largeur = r2.x - r.x;
            if (largeur == 0)
                largeur = MIN_WIDTH;
            comp.repaint(r.x, r.y, largeur, r.height);
            this.x = r.x;
            this.y = r.y;
            this.width = largeur;
            this.height = r.height;
        } catch (Exception e) {
            logger.info(e);
        }
    }
    @Override
    public void paint(Graphics g) {
        if (isVisible())
            try {
                JTextComponent comp = getComponent();
                TextUI mapper = comp.getUI();
                Rectangle r1 = mapper.modelToView(comp, getDot());
                Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
                g = g.create();
                g.setColor(comp.getForeground());
                g.setXORMode(comp.getBackground());
                int largeur = r2.x - r1.x;
                if (largeur == 0) 
                    largeur = MIN_WIDTH;               
                g.fillRect(r1.x, r1.y, largeur, r1.height);
                g.dispose();
            } catch (Exception e) {
                logger.info(e);
            }
    }
}

Answer:

found another caret, ok with it

    public class FancyCaret extends DefaultCaret {
    private static final Logger logger = Logger.getLogger(FancyCaret.class.getName());

    protected synchronized void damage(Rectangle r) {
        if (r == null)
            return;

        x = r.x;
        y = r.y;
        height = r.height;
        if (width <= 0)
            width = getComponent().getWidth();

        repaint(); // calls getComponent().repaint(x, y, width, height)
    }

    @Override
    public void paint(Graphics g) {
        JTextComponent comp = getComponent();
        if (comp == null)
            return;

        int dot = getDot();
        Rectangle r;
        char dotChar;
        try {
            r = comp.modelToView(dot);
            if (r == null)
                return;
            dotChar = comp.getText(dot, 1).charAt(0);
        } catch (Exception e) {
            logger.info(e);
            return;
        }

        if ((x != r.x) || (y != r.y)) {
            repaint(); // erase previous location of caret
            x = r.x; // Update dimensions (width gets set later in this method)
            y = r.y;
            height = r.height;
        }

        if (dotChar == '\n') {
            width = r.height / 2;
            if (isVisible())
                g.fillRect(r.x, r.y, width, r.height);
            return;
        }

        g.setColor(comp.getCaretColor());
        g.setXORMode(comp.getBackground()); // do this to draw in XOR mode

        width = g.getFontMetrics().charWidth(dotChar);
        if (isVisible())
            g.fillRect(r.x, r.y, width, r.height);
    }
}