JXDatePicker wird hinter anderen Feldern angezeigt

Firefork

Grünschnabel
Hallo,

ich teste gerade die JXDatePicker-Komponente von SwingX. Wenn ich hier ein Datum auswählen will, wird mir der Dialog zur Datumsauswahl immer hinter anderen Feldern angezeigt. Der folgende Screenshot zeigt das Problem:

Bildschirmfoto-JGoodies Form-Test.png

Der Quelltext sieht wie folgt aus:

Code:
package de.wicker.jgoogdies.forms.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTitledPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;


public class Main extends JFrame {

	private static final long serialVersionUID = 8981866449762917834L;

	public Main() {
		initComponents();
	}


	private void initComponents() {

		// JFrame Eigenschaften
		setTitle("JGoodies Form-Test");
		setSize(800, 600);
		setPreferredSize(new Dimension(800, 600));
		setLocationRelativeTo(null); 
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JTabbedPane tabbedPane = new JTabbedPane();
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		tabbedPane.add("Test 1",test1());
		tabbedPane.add("Test 2",test2());
		tabbedPane.add("Test 3",test3());
		
		List<String> lofNames = new ArrayList<String>();
		for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
			lofNames.add(info.getClassName());
		}
		JComboBox edtLookAndFreel = new JComboBox(lofNames.toArray());
		getContentPane().add(edtLookAndFreel, BorderLayout.NORTH);
		edtLookAndFreel.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				setLookAndFree((String) e.getItem()); 
			}
		});
		
		
	}


	private void setLookAndFree(String lnfName) {
		try {
			UIManager.setLookAndFeel(lnfName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(this);
		pack();	
	}
	

	private JPanel test1() {
		FormLayout layout = new FormLayout(
				"pref, 4dlu, 50dlu, 4dlu, min",  // columns 
				"pref, 2dlu, pref, 2dlu, pref"); // rows

		layout.setRowGroups(new int[][]{{1,3,5}});
		JPanel panel = new JPanel(layout);
		CellConstraints cc = new CellConstraints();
		panel.add(new JLabel("Label1"), 	cc.xy(1, 1));
		panel.add(new TextField(), 			cc.xyw(3, 1, 3));
		panel.add(new JLabel("Label2"), 	cc.xy(1, 3));
		panel.add(new TextField(), 			cc.xy(3, 3));
		panel.add(new JLabel("Label3"), 	cc.xy(1, 5));
		panel.add(new TextField(),			cc.xy(3, 5));
		panel.add(new JButton("..."),		cc.xy(5, 5));
		return panel;
	}



	private JPanel test2() {
		FormLayout layout = new FormLayout(
				"right:max(50dlu;p), 4dlu, 75dlu, 7dlu, right:p, 4dlu, 75dlu",			// columns
				"p, 2dlu, p, 3dlu, p, 3dlu, p, 7dlu, p, 2dlu, p, 3dlu, p, 3dlu, p");	// rows

		JXPanel formContainer = new JXPanel();
		PanelBuilder builder = new PanelBuilder(layout,formContainer);
		builder.setDefaultDialogBorder();
		CellConstraints cc = new CellConstraints();

		JXDatePicker datePicker = new JXDatePicker(new Date());
		datePicker.setName("datePicker");
		TextField pti = new TextField();
		builder.addSeparator("Segment",   cc.xyw(1,  1, 7));
		builder.addLabel("Identifier",    cc.xy (1,  3));
		builder.add(datePicker,           cc.xy (3,  3));
		builder.addLabel("PTI [kW]",      cc.xy (1,  5));
		builder.add(pti,     			  cc.xy (3,  5));
		builder.addLabel("Power [kW]",    cc.xy (5,  5));
		builder.add(new TextField(),      cc.xy (7,  5));
		builder.addLabel("len [mm]",      cc.xy (1,  7));
		builder.add(new TextField(),      cc.xy (3,  7));
		builder.addSeparator("Diameters", cc.xyw(1,  9, 7));
		builder.addLabel("da [mm]",       cc.xy (1, 11));
		builder.add(new TextField(),      cc.xy (3, 11));
		builder.addLabel("di [mm]",       cc.xy (5, 11));
		builder.add(new TextField(),      cc.xy (7, 11));
		builder.addLabel("da2 [mm]",      cc.xy (1, 13));
		builder.add(new TextField(),      cc.xy (3, 13));
		builder.addLabel("di2 [mm]",      cc.xy (5, 13));
		builder.add(new TextField(),      cc.xy (7, 13));
		builder.addLabel("R [mm]",        cc.xy (1, 15));
		builder.add(new TextField(),      cc.xy (3, 15));
		builder.addLabel("D [mm]",        cc.xy (5, 15));
		builder.add(new TextField(),      cc.xy (7, 15));

		JXTitledPanel panel = new JXTitledPanel("Forms-Layout Test 2 mit JXTitledPanel");
		panel.setContentContainer(builder.getPanel());
		
		return panel;
	}


	
	private JPanel test3() {
		JPanel panel = new JPanel();
		
		panel.setLayout(new GridBagLayout());

		GridBagConstraints cc = new GridBagConstraints();
		cc.gridx = 0; cc.gridy = 0;
		panel.add(new JLabel("datepicker"), cc);
		JXDatePicker datePicker = new JXDatePicker(new Date());
		cc = new GridBagConstraints();
		cc.gridx = 1; cc.gridy = 0; cc.fill = GridBagConstraints.HORIZONTAL;
		panel.add(datePicker, cc);

		cc = new GridBagConstraints();
		cc.gridx = 0; cc.gridy = 1;
		panel.add(new JLabel("PTI"), cc);
		TextField pti = new TextField();
		cc = new GridBagConstraints();
		cc.gridx = 1; cc.gridy = 1; cc.fill = GridBagConstraints.HORIZONTAL;
		panel.add(pti, cc);
		
		return panel;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			// UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); // Metal
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // System
//		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
//		        if ("Nimbus".equals(info.getName())) {
//		            UIManager.setLookAndFeel(info.getClassName());
//		            break;
//		        }
//		    }
		} 
		catch (UnsupportedLookAndFeelException e) {
			// handle exception
		}
		catch (ClassNotFoundException e) {
			// handle exception
		}
		catch (InstantiationException e) {
			// handle exception
		}
		catch (IllegalAccessException e) {
			// handle exception
		}		

		// MainFrame anzeigen
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				(new Main()).setVisible(true);
			}
		});
	}

}


Hatte das schon mal jemand von Euch und gibt es eine Lösung?

Vielen Dank und beste Grüße
Andreas
 

Firefork

Grünschnabel
Hallo,

ich habe die Lösung jetzt selbst gefunden. Ich musste folgende Zeile noch hinzufügen:

Code:
datePicker.setLightWeightPopupEnabled(false);

Die Dokumentation sagt zu dieser Methode:
Sets the lightWeightPopupEnabled property, which provides a hint as to whether or not a lightweight Component should be used to contain the JXDatePicker, versus a heavyweight Component such as a Panel or a Window. The decision of lightweight versus heavyweight is ultimately up to the JXDatePicker. Lightweight windows are more efficient than heavyweight windows, but lightweight and heavyweight components do not mix well in a GUI. If your application mixes lightweight and heavyweight components, you should disable lightweight popups. The default value for the lightWeightPopupEnabled property is true, unless otherwise specified by the look and feel. Some look and feels always use heavyweight popups, no matter what the value of this property.

Viele Grüße
Andreas
 

genodeftest

Erfahrenes Mitglied
Das wundert mich aber…
Du verwendest doch eigentlich Swing, da sollte das keine Probleme machen. Vielleicht ist auch nur der JXDatePicker falsch implementiert, bei „normalen“ Popupmenus funktioniert das gleiche einwandfrei.