Posts for Tag: swing

JAVA vertical gradient background panel

Recently i had to create a gradient background for one of the panels in our [POS application](, so i though i'd share the code.

You need to pass the top and bottom colors of the gradient and also make sure that any panels inside this one are transparent (i.e. setOpaque(false)), otherwise they will paint above it.

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

import javax.swing.JPanel;

public class VerticalGradientBackgroundPanel extends JPanel {

private Color topColor; private Color bottomColor;

public VerticalGradientBackgroundPanel(Color topColor, Color bottomColor) {

this.topColor = topColor; this.bottomColor = bottomColor; }

@Override protected void paintComponent(Graphics grphcs) {


Graphics2D g2d = (Graphics2D) grphcs;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint gp = new GradientPaint(0, 0, topColor, 0, getHeight(), bottomColor); g2d.setPaint(gp); g2d.fillRect(0, 0, getWidth(), getHeight()); } }

JasperReports - Unsupported major.minor version 51.0

Just ran into something a bit far fetched: My Jasper reports were not working in VOS, our JAVA Swing application at Inforviegas. It complained with an error along these lines: Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedClassVersionError: CCClient_1334580618412_476658 : Unsupported major.minor version 51.0

The only difference from business as usual was that i changed laptops.

We are using the jasperreports-maven-plugin to compile the reports from maven. In our pom.xml files we always define the maven-compiler-plugin target and source versions of the virtual machine. One would assume that the jasper plugin would use the same version of the maven-compiler-plugin, right? Wrong. It uses the default virtual machine set in your computer. And since i had Java 7 as the default and we are compiling to Java 6, the reports end up with the wrong java version, making them not show in our application.

Hope this helps some people save some time, since i couldn't :)

NonUniqueObjectException- a different object with the same identifier value was already associated with the session

If you are developing in Swing and using hibernate you might - at some point in time - run into this exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

What happened, you might ask? Well, turns out that when you close the session that created a given hibernate-managed object, this object becomes "detached" (meaning hibernate no longer has any control over it). So, when you attempt to save or update it in another session, hibernate will complain with that exception, because it can no longer tell which is the "true" version of the object.

The solution(s)? Look inside your code for places where you might be closing the session and take that out. That's the solution in the case where you are using a long session (one that spans the entirety of the application's life). In case you really want to close the session, you might consider using the session's merge() method, which basically merges the fields in both the attached and non-attached instances of the object, and performs an update.

Hope that saves someone half a day of debugging ;)