JMenu in Popup mit Navigation links und rechts.

Hallo Romsl,

wahrlich das ist sehr kreativ :) !

Wieso hat das Menu "nach Subkontinenten" auf beiden Seiten Pfeile? Das Menu "nach Ländern" jedoch nur auf einer Seite?


Vg Erdal


//edit


Hallo Romsl,

hab noch zwei Vorschläge. Das waren dann aber die letzten ;) .

Die erste Alternative folgt einen etwas anderen Weg, da Swing keine JCheckBoxMenu Komponente bereitstelt, die unbedingt notwendig ist.

Das zweite Beispiel ist sehr sehr nahe an deiner Vorstellung, vielleicht sogar genau so wie du es dir vorgestellt hattest.

Alternative 1:
Java:
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class JModifiedMenuUser extends JFrame {

	private JPopupMenu pm = new JPopupMenu();

	public JModifiedMenuUser() {
		this.setTitle(this.getClass().getSimpleName());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(480, 640);
		this.setAlwaysOnTop(true);
		this.setLocationByPlatform(true);

		JBothArrowCheckBoxMenuItem bacbm1 = new JBothArrowCheckBoxMenuItem(
				"Afrika");
		JBothArrowCheckBoxMenuItem bacbm2 = new JBothArrowCheckBoxMenuItem(
				"Asien");
		JBothArrowCheckBoxMenuItem bacbm3 = new JBothArrowCheckBoxMenuItem(
				"Europa");

		JBothArrowCheckBoxMenuItem bacbm31 = new JBothArrowCheckBoxMenuItem(
				"Nordeuropa");
		JBothArrowCheckBoxMenuItem bacbm32 = new JBothArrowCheckBoxMenuItem(
				"Osteuropa");
		JBothArrowCheckBoxMenuItem bacbm33 = new JBothArrowCheckBoxMenuItem(
				"Westeuropa");

		JMenu jm1 = new JMenu("Auswahl");
		JMenu jm2 = new JMenu("Auswahl");

		pm.add(new JMenuItem("nach Kontinenten"));
		pm.addSeparator();
		pm.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		pm.addSeparator();
		pm.add(bacbm1);
		pm.add(bacbm2);
		pm.add(bacbm3);
		pm.addSeparator();
		pm.add(jm1);
		pm.addSeparator();
		pm.add(new JButton("Filtern"));

		jm1.add(new JMenuItem("nach Subkontinenten"));
		jm1.addSeparator();
		jm1.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		jm1.addSeparator();
		jm1.add(bacbm31);
		jm1.add(bacbm32);
		jm1.add(bacbm33);
		jm1.addSeparator();
		jm1.add(jm2);
		jm1.addSeparator();
		jm1.add(new JButton("Filtern"));

		jm2.add(new JMenuItem("nach Ländern"));
		jm2.addSeparator();
		jm2.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		jm2.addSeparator();
		jm2.add(new JLeftArrowCheckBoxMenuItem("Dänemark"));
		jm2.add(new JLeftArrowCheckBoxMenuItem("Finnland"));
		jm2.add(new JLeftArrowCheckBoxMenuItem("Island"));
		jm2.add(new JLeftArrowCheckBoxMenuItem("Norwegen"));
		jm2.add(new JLeftArrowCheckBoxMenuItem("Schweden"));
		jm2.addSeparator();
		jm2.add(new JButton("Filtern"));

		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					pm.show(JModifiedMenuUser.this, e.getX(), e.getY());
				}
			}
		});

		this.setVisible(true);
	}

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

	class JBothArrowCheckBoxMenuItem extends JCheckBoxMenuItem {

		public JBothArrowCheckBoxMenuItem(String s) {
			super(s);
			this.setBorder(BorderFactory.createEmptyBorder(2, 20, 2, 2));
		}

		public void paint(Graphics g) {
			super.paint(g);
			g.translate(7, 7);
			g.drawLine(4, 0, 4, 7);
			g.drawLine(3, 1, 3, 6);
			g.drawLine(2, 2, 2, 5);
			g.drawLine(1, 3, 1, 4);

			g.translate(this.getWidth() - 21, 0);
			g.drawLine(0, 0, 0, 7);
			g.drawLine(1, 1, 1, 6);
			g.drawLine(2, 2, 2, 5);
			g.drawLine(3, 3, 3, 4);
		}
	}

	class JLeftArrowCheckBoxMenuItem extends JCheckBoxMenuItem {
		public JLeftArrowCheckBoxMenuItem(String s) {
			super(s);
			this.setBorder(BorderFactory.createEmptyBorder(2, 20, 2, 2));
		}

		public void paint(Graphics g) {
			super.paint(g);
			g.translate(7, 7);
			g.drawLine(4, 0, 4, 7);
			g.drawLine(3, 1, 3, 6);
			g.drawLine(2, 2, 2, 5);
			g.drawLine(1, 3, 1, 4);
		}
	}
}


Alternative 2:
Java:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class JCheckBoxMenuUser extends JFrame {

	private JCheckBoxPopupMenu pm = new JCheckBoxPopupMenu();

	public JCheckBoxMenuUser() {
		this.setTitle(this.getClass().getSimpleName());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(480, 640);
		this.setAlwaysOnTop(true);
		this.setLocationByPlatform(true);

		JCheckBoxMenu cbm1 = new JCheckBoxMenu("Afrika");
		cbm1.add(new JMenuItem("nach Ländern"));
		cbm1.addSeparator();
		cbm1.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm1.addSeparator();
		cbm1.add(new JLeftArrowCheckBoxMenuItem("Guinea"));
		cbm1.add(new JLeftArrowCheckBoxMenuItem("Saudi Arabien"));
		cbm1.add(new JLeftArrowCheckBoxMenuItem("Uganda"));
		cbm1.addSeparator();
		cbm1.add(new JButton("Filtern"));

		JCheckBoxMenu cbm2 = new JCheckBoxMenu("Asien");
		cbm2.add(new JMenuItem("nach Ländern"));
		cbm2.addSeparator();
		cbm2.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm2.addSeparator();
		cbm2.add(new JLeftArrowCheckBoxMenuItem("China"));
		cbm2.add(new JLeftArrowCheckBoxMenuItem("Russland"));
		cbm2.add(new JLeftArrowCheckBoxMenuItem("Türkei"));
		cbm2.addSeparator();
		cbm2.add(new JButton("Filtern"));

		JCheckBoxMenu cbm3 = new JCheckBoxMenu("Europa");
		JCheckBoxMenu cbm31 = new JCheckBoxMenu("Nordeuropa");
		JCheckBoxMenu cbm32 = new JCheckBoxMenu("Osteuropa");
		JCheckBoxMenu cbm33 = new JCheckBoxMenu("Westeuropa");

		cbm3.add(new JMenuItem("nach Subkontinenten"));
		cbm3.addSeparator();
		cbm3.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm3.addSeparator();
		cbm3.addCheckBoxMenu(cbm31);
		cbm3.addCheckBoxMenu(cbm32);
		cbm3.addCheckBoxMenu(cbm33);
		cbm3.addSeparator();
		cbm3.add(new JButton("Filtern"));

		cbm31.add(new JMenuItem("nach Ländern"));
		cbm31.addSeparator();
		cbm31.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm31.addSeparator();
		cbm31.add(new JLeftArrowCheckBoxMenuItem("Dänemark"));
		cbm31.add(new JLeftArrowCheckBoxMenuItem("Finnland"));
		cbm31.add(new JLeftArrowCheckBoxMenuItem("Island"));
		cbm31.add(new JLeftArrowCheckBoxMenuItem("Norwegen"));
		cbm31.add(new JLeftArrowCheckBoxMenuItem("Schweden"));
		cbm31.addSeparator();
		cbm31.add(new JButton("Filtern"));

		cbm32.add(new JMenuItem("nach Ländern"));
		cbm32.addSeparator();
		cbm32.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm32.addSeparator();
		cbm32.add(new JLeftArrowCheckBoxMenuItem("Griechenland"));
		cbm32.add(new JLeftArrowCheckBoxMenuItem("Polen"));
		cbm32.add(new JLeftArrowCheckBoxMenuItem("Türkei"));
		cbm32.addSeparator();
		cbm32.add(new JButton("Filtern"));

		cbm33.add(new JMenuItem("nach Ländern"));
		cbm33.addSeparator();
		cbm33.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		cbm33.addSeparator();
		cbm33.add(new JLeftArrowCheckBoxMenuItem("Deutschland"));
		cbm33.add(new JLeftArrowCheckBoxMenuItem("Frankreich"));
		cbm33.add(new JLeftArrowCheckBoxMenuItem("Spanien"));
		cbm33.addSeparator();
		cbm33.add(new JButton("Filtern"));

		pm.add(new JMenuItem("nach Kontinenten"));
		pm.addSeparator();
		pm.add(new JCheckBoxMenuItem("Check/Uncheck all"));
		pm.addSeparator();
		pm.addCheckBoxMenu(cbm1);
		pm.addCheckBoxMenu(cbm2);
		pm.addCheckBoxMenu(cbm3);
		pm.addSeparator();
		pm.add(new JButton("Filtern"));

		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (SwingUtilities.isRightMouseButton(e)) {
					pm.show(JCheckBoxMenuUser.this, e.getX(), e.getY());
				}
			}
		});

		this.setVisible(true);
	}

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

	class JCheckBoxPopupMenu extends JPopupMenu {

		private GridBagConstraints c = new GridBagConstraints();

		private int y = 0;

		public JCheckBoxPopupMenu() {
			this.setLayout(new GridBagLayout());
		}

		public JMenuItem add(JMenuItem mItem) {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.BOTH;
			this.add(mItem, c);
			y++;
			return mItem;
		}

		public Component add(Component comp) {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.NONE;
			this.add(comp, c);
			y++;
			return comp;
		}

		public void addSeparator() {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.BOTH;
			this.add(new JSeparator(JSeparator.HORIZONTAL), c);
			y++;
		}

		public void addCheckBoxMenu(JMenu menu) {
			JCheckBoxMenuItem cbm = new JCheckBoxMenuItem(" ") {
				{
					Dimension d = new Dimension(40, 20);
					this.setPreferredSize(d);
					this.setMinimumSize(d);
					this
							.setBorder(BorderFactory.createEmptyBorder(2, 20,
									2, 2));
				}

				public void paint(Graphics g) {
					super.paint(g);
					g.translate(5, 5);
					g.drawLine(4, 0, 4, 7);
					g.drawLine(3, 1, 3, 6);
					g.drawLine(2, 2, 2, 5);
					g.drawLine(1, 3, 1, 4);
				}
			};
			c.gridx = 0;
			c.gridy = y;
			c.fill = c.VERTICAL;
			c.weightx = 1;
			c.weighty = 1;
			c.gridwidth = 1;
			this.add(cbm, c);
			c.gridx = 1;
			c.gridy = y;
			c.fill = c.BOTH;
			c.weightx = 1;
			c.weighty = 1;
			c.gridwidth = 1;
			y++;
			this.add(menu, c);
		}
	}

	class JCheckBoxMenu extends JMenu {

		private JPopupMenu pm = this.getPopupMenu();

		private GridBagConstraints c = new GridBagConstraints();

		private int y = 0;

		public JCheckBoxMenu(String s) {
			super(s);
			pm.setLayout(new GridBagLayout());
		}

		public JMenuItem add(JMenuItem mItem) {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.BOTH;
			pm.add(mItem, c);
			y++;
			return mItem;
		}

		public Component add(Component comp) {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.NONE;
			pm.add(comp, c);
			y++;
			return comp;
		}

		public void addSeparator() {
			c.gridx = 0;
			c.gridy = y;
			c.gridwidth = 2;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = c.BOTH;
			pm.add(new JSeparator(JSeparator.HORIZONTAL), c);
			y++;
		}

		public void addCheckBoxMenu(JMenu menu) {
			JCheckBoxMenuItem cbm = new JCheckBoxMenuItem(" ") {
				{
					Dimension d = new Dimension(40, 20);
					this.setPreferredSize(d);
					this.setMinimumSize(d);
					this
							.setBorder(BorderFactory.createEmptyBorder(2, 20,
									2, 2));
				}

				public void paint(Graphics g) {
					super.paint(g);
					g.translate(5, 5);
					g.drawLine(4, 0, 4, 7);
					g.drawLine(3, 1, 3, 6);
					g.drawLine(2, 2, 2, 5);
					g.drawLine(1, 3, 1, 4);
				}
			};
			c.gridx = 0;
			c.gridy = y;
			c.fill = c.VERTICAL;
			c.weightx = 1;
			c.weighty = 1;
			c.gridwidth = 1;
			pm.add(cbm, c);
			c.gridx = 1;
			c.gridy = y;
			c.fill = c.BOTH;
			c.weightx = 1;
			c.weighty = 1;
			c.gridwidth = 1;
			y++;
			pm.add(menu, c);
		}
	}

	class JLeftArrowCheckBoxMenuItem extends JCheckBoxMenuItem {
		public JLeftArrowCheckBoxMenuItem(String s) {
			super(s);
			this.setBorder(BorderFactory.createEmptyBorder(2, 20, 2, 2));
		}

		public void paint(Graphics g) {
			super.paint(g);
			g.translate(5, 5);
			g.drawLine(4, 0, 4, 7);
			g.drawLine(3, 1, 3, 6);
			g.drawLine(2, 2, 2, 5);
			g.drawLine(1, 3, 1, 4);
		}
	}
}


Vg Erdal
 
Hi flashray,

Danke für die vielen Beispiele. Die letzten Beiden waren gut, wobei das Aussehen des ersten mit der Funktion des zweiten die Lösung ist ;)

Gruß
-- Romsl
 
Hallo Romsl,

das i-Tüpfelchen überlass ich dir ;) !

Darf ich fragen ob du an einer erweiterten Swingkomponente arbeitest? Oder eine (geographische) Anwendung schreibst?


Vg Erdal
 
Hi,

das wird eine, bzw. ist eine im Anfgangsstadium bestehende, Anwendung um OLAP Daten zu explorieren und analysieren.

Gruß
-- Romsl
 

Neue Beiträge

Zurück