Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Canvas covers all

Status
Not open for further replies.

chirpyform

Programmer
Jun 20, 2003
202
FR
Hi,

I've got a serious canvas problem. When I change the size of my window or use a pupup menu the canvas covers everything else. is there anyway of sending the canvas to the background or sending the other components to the foreground ?

Thanks for any help

Chris
 
Please look at setDefaultLightWeightPopupEnabled() in my link
 
// copy from web
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

class JCanvas extends JComponent
{

JCanvas()
{
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
}

public Dimension getPreferredSize()
{
return new Dimension(400, 400);
}

public Dimension getMinimumSize()
{
return getPreferredSize();
}

}
 
thanks for the replies but in the end i chose to use a JPanel instead of the Canvas class.
However I did lose the focuslistener options and now i must use a popup menu to keep the same level of functionality.

Thanks again

Chris
 
Code:
// please extends JComponent
// JMenu part is copied from 
[URL unfurl="true"]http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node79.html[/URL]
package org;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyJPTest extends JFrame implements	ActionListener{
	private final int	ITEM_PLAIN	=	0;	// Item types
	private final int	ITEM_CHECK	=	1;
	private final int	ITEM_RADIO	=	2;

	private	JPanel		topPanel;
	private	JMenuBar	menuBar;
	private	JMenu		menuFile;
	private	JMenu		menuEdit;
	private	JMenu		menuProperty;
	private	JMenuItem	menuPropertySystem;
	private	JMenuItem	menuPropertyEditor;
	private	JMenuItem	menuPropertyDisplay;
	private	JMenuItem	menuFileNew;
	private	JMenuItem	menuFileOpen;
	private	JMenuItem	menuFileSave;
	private	JMenuItem	menuFileSaveAs;
	private	JMenuItem	menuFileExit;
	private	JMenuItem	menuEditCopy;
	private	JMenuItem	menuEditCut;
	private	JMenuItem	menuEditPaste;

    public MyJPTest(){
    	getContentPane().setLayout(new GridLayout(1,1));
    	MyJPanel jPanel = new MyJPanel();
    	getContentPane().add(jPanel);
		menuBar = new JMenuBar();

		// Set this instance as the application's menu bar
		setJMenuBar( menuBar );
		
		// Build the property sub-menu
		menuProperty = new JMenu( "Properties" );
		menuProperty.setMnemonic( 'P' );

		// Create property items
		menuPropertySystem = CreateMenuItem( menuProperty, ITEM_PLAIN,
								"System...", null, 'S', null );
		menuPropertyEditor = CreateMenuItem( menuProperty, ITEM_PLAIN,
								"Editor...", null, 'E', null );
		menuPropertyDisplay = CreateMenuItem( menuProperty, ITEM_PLAIN,
								"Display...", null, 'D', null );
		
		// Create the file menu
		menuFile = new JMenu( "File" );
		menuFile.setMnemonic( 'F' );
		menuBar.add( menuFile );

		// Create the file menu
		// Build a file menu items
		menuFileNew = CreateMenuItem( menuFile, ITEM_PLAIN,
								"New", null, 'N', null );
		menuFileOpen = CreateMenuItem( menuFile, ITEM_PLAIN, "Open...",
								new ImageIcon( "open.gif" ), 'O',
								"Open a new file" );
		menuFileSave = CreateMenuItem( menuFile, ITEM_PLAIN, "Save",
								new ImageIcon( "save.gif" ), 'S',
								" Save this file" );
		menuFileSaveAs = CreateMenuItem( menuFile, ITEM_PLAIN,
								"Save As...", null, 'A',
								"Save this data to a new file" );
		// Add the property menu
		menuFile.addSeparator();
		menuFile.add( menuProperty );	
		menuFile.addSeparator();
		menuFileExit = CreateMenuItem( menuFile, ITEM_PLAIN,
								"Exit", null, 'x',
								"Exit the program" );


		// Create the file menu
		menuEdit = new JMenu( "Edit" );
		menuEdit.setMnemonic( 'E' );
		menuBar.add( menuEdit );

		// Create edit menu options
		menuEditCut = CreateMenuItem( menuEdit, ITEM_PLAIN,
								"Cut", null, 't',
								"Cut data to the clipboard" );
		menuEditCopy = CreateMenuItem( menuEdit, ITEM_PLAIN,
								"Copy", null, 'C',
								"Copy data to the clipboard" );
		menuEditPaste = CreateMenuItem( menuEdit, ITEM_PLAIN,
								"Paste", null, 'P',
								"Paste data from the clipboard" );
	}

	public JMenuItem CreateMenuItem( JMenu menu, int iType, String sText,
								ImageIcon image, int acceleratorKey,
								String sToolTip )
	{
		// Create the item
		JMenuItem menuItem;

		switch( iType )
		{
			case ITEM_RADIO:
				menuItem = new JRadioButtonMenuItem();
				break;

			case ITEM_CHECK:
				menuItem = new JCheckBoxMenuItem();
				break;

			default:
				menuItem = new JMenuItem();
				break;
		}

		// Add the item test
		menuItem.setText( sText );

		// Add the optional icon
		if( image != null )
			menuItem.setIcon( image );

		// Add the accelerator key
		if( acceleratorKey > 0 )
			menuItem.setMnemonic( acceleratorKey );

		// Add the optional tool tip text
		if( sToolTip != null )
			menuItem.setToolTipText( sToolTip );

		// Add an action handler to this menu item
		menuItem.addActionListener( this );

		menu.add( menuItem );

		return menuItem;
	}
	public void actionPerformed( ActionEvent event )
	{
		System.out.println( event );
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        MyJPTest myObj = new MyJPTest();
        myObj.setSize(200,200);
        myObj.setVisible(true);
	}

}
class MyJPanel extends JPanel{
	public MyJPanel(){
		super();
	}
	 public void paintComponent(Graphics g)
	    {
	        super.paintComponent(g);
	        g.setColor(Color.red);
	        g.drawString("Hello",50,100);
	    }

	    public Dimension getPreferredSize()
	    {
	        return new Dimension(400, 400);
	    }

	    public Dimension getMinimumSize()
	    {
	        return getPreferredSize();
	    }
	
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top