JMenuBar is hidden by Canvas

Hi,
I have a class that extends JFrame, this has a JMenuBar set to it (set with setJMenuBar method of JFrame) and is displayed fine until i try to add a java.awt.Canvas object to the center of the JFrame(using a BorderLayout) . When this canvas has been added then you can still click on the JMenu names in the JMenuBar but the JMenu itself is no hidden behind the canvas. Here is some of the code
Class that extends JFrame's constructor
//call the super constructor
          super("My Image Viewer");
          //get the content pane
          c = getContentPane();
          //set the layout of the frame
          c.setLayout(new BorderLayout());
          //create a new MenuHandler
          menuHandler = new MenuHandler();
          //create a new JFileChooser
          fileChooser = new JFileChooser("C:/Documents and Settings/"
                    + "Irene Stephanie/My Documents");
          //create a new file filter
          filter = new ImgFilter();
          //assign the file filter
          fileChooser.setFileFilter(filter);
          //create a new window canvas
          canvas = new WindowCanvas();
          add("Center", canvas);
          //set up the menus
          initMenu();
          //set the JMenuBar
          setJMenuBar(menuBar);
          //Set the window size
          setSize(640, 480);
          //make the window visible
          setVisible(true);Class that extends Canvas
public class WindowCanvas extends Canvas{
     public WindowCanvas(){
          super();
     public void paint(Graphics g){
     }Please not that it works if you add the JMenuBar to the South position, and that you can draw in the canvas, but you cannot see the JMenu's.
Any help will be greatly apreciated
Andy

A Canvas is a heavyweight component. By default, Swing menus are lightweight components. You need to call
JPopupMenu.setDefaultLightWeightPopupEnabled(false);before creating your menus.

Similar Messages

  • JMenuBar hides under Canvas

    Hello,
    I am doing a program that has a JMenuBar and Canvas,
    The Menu Bar shows but when i click on it the JMenuItems are not shown they are hidden under the canvas which is stretched on the frame,
    when i dont add the canas the menu bar works .
    Thanks for help.

    Don't mix AWT (Canvas) and Swing (JMenuBar) unless you have very strong reasons and know what you are doing. Instead of a Canvas, can you use a JPanel? Hopefully all is in a Swing root container such as a JFrame or JApplet.

  • JMenubar hidden under JScrollPane

    Hi,
    JDK 1.3
    quick question... is there any way to stop JMenuItems in a JMenu, displayed on a JMenubar at the top of a Swing application from hidding behind the content of a Frame below it?
    I.e. when the FILE JMenu option is selected the drop down menu is hidden behind the content of the Frame.
    is there anyway to ensure the JMenuItems are always displayed 'on top'?
    The Frame is used to encapsulate the platform's WWW browser, displaying webpages. The class used to build this native frame extends java.awt.Canvas (this is the only way we could get around NT proxy server authentication) and therefore cannot be changed to use a JEditiorPane or JPane.
    Any ideas?

    All SWING components are lightweight. But you can make all popup menus to be heavyweight (like in AWT) for this call first the static method javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(false);

  • Tab canvas(tab page hidden)

    Hello Everybody,
    I have two blocks.each block have one canvas(first block have content canvas andthe second block have tab canvas),in the first block i want to to make button to make some tab pages hidden,
    how to make one tab page hidden?
    Thanks and regards,
    Mona

    Mona wrote:
    how to make one tab page hidden?
    Use the SET_TAB_PAGE_PROPERTY built-in with the VISIBLE property set to FALSE, you can set it visible/invisible dynamically.
    Your code should look like something like this:
    SET_TAB_PAGE_PROPERTY('tab_page_name', VISIBLE, PROPERTY_TRUE);Tony
    Edited by: Tony Garabedian on Oct 16, 2008 12:00 PM

  • Stopping a swf when canvas is hidden

    Hi - I have a really really simple little viewstack that is giving me trouble.
    The idea is that a different swf will be loaded into each canvas of the viewstack, and will be active when the view is active, and unloaded when the view gets hidden. Currently the swfs load as expected, but audio continues to play even after the view is hidden.

    Hi Greg - how do I unload it?
    (I'm not sure my pasted-in code is showing)
    I am doing this: hideEffect = decisionLoader.unloadAndStop();
    Where decisionLoader is the ID of the SWFLoader object present in that view.
    Code follows below:
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:ViewStack x="137" y="10" id="decision" width="711.8182" height="623.48486">
            <mx:Canvas label="Decision Table" width="100%" height="100%" hideEffect="decisionLoader.unloadAndStop();this.unloadAndStop();">
                <mx:Panel x="0" y="0" width="704" height="609" layout="absolute" title="Decision Table" cornerRadius="20">
                    <mx:SWFLoader x="0" y="0" source="../ethics/Slides/Decisiontable.swf" scaleContent="true" autoLoad="true" id="decisionLoader"/>
                </mx:Panel>
            </mx:Canvas>
            <mx:Canvas label="Evaluation Table" width="100%" height="100%"  hideEffect="evaluationLoader.unloadAndStop();this.unloadAndStop();">
                <mx:Panel x="0" y="0" width="704" height="609" layout="absolute" cornerRadius="20" title="Evaluation Table">
                    <mx:SWFLoader x="0" y="0" source="../ethics/Slides/EvaluationTable_2.swf" scaleContent="true" autoLoad="true" id="evaluationLoader"/>
                </mx:Panel>
            </mx:Canvas>
        </mx:ViewStack>
        <mx:Image x="10" y="10" source="../images/icon1_IDissues.png" width="127" height="135"/>
        <mx:LinkBar direction="vertical" x="10" y="153" width="119" height="149" dataProvider="decision">
        </mx:LinkBar>
    </mx:Application>

  • Photoshop canvas is hidden

    I keep having issues with photoshop in Lion. I open a file in photoshop, but the file cannot be seen. If I go to the photoshop menu inder windows, I can see the file name listed as an open window, but that open window just does not show. I go up to mission control and it is not there anywhere. All hte photoshop menus ect are showing, just not the main canvas of the file open. Kind of pointless to open a file when you cant see the file that is open.
    I have to keep quitting photoshop and then relaunching it and resetting the preferences to defualt.

    This problem happened to me twice!!! I ended up blogging about my problem and all the troublshooting I went through, and how (on two seperate occassions), I ended up solving my problem I hope this is helpful!
    http://www.caramooneydesigns.com/blog/2013/07/29/photoshop-support-photoshop-can vas-is-hidden/

  • Menu list Hidden by a

    I have a problem with the following program. When I click on the Menu , the dropdown list is hidden by the canvas object. If I replace Canvas by a JPanel, I do not have the problem.
    Now If I add the Cavas to the JPanel, the problem occurs again.
    Note : I really need a Canvas because I have to draw curves into it.
    Thanks to everyone for advices.
    Gerard
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TestDial extends JDialog {
         private JPanel  mypanel = null;
         private JMenu  JMenu1 = null;
         private JMenuItem  Item1 = null;
         private JMenuItem  Item2 = null;
         private JMenuBar  mybar = null;
         private java.awt.Canvas  Canevas = null;
    public TestDial( ) {
         super( );
               setSize(400, 405);
               Item1 = new JMenuItem();
               Item1.setText("Item1");
               Item2 = new JMenuItem();
               Item2.setText("Item2");
               JMenu1 = new JMenu();
               JMenu1.setText("My Menu");
               JMenu1.add( Item1);
               JMenu1.add( Item2 );
               mybar = new JMenuBar();
               mybar.add( JMenu1);
               mypanel = new JPanel();
               mypanel.setLayout(null);
               mypanel.setBackground(Color.cyan);
               Canevas = new Canvas();
               Canevas.setBackground( Color.black);
               Canevas.setBounds(7, 10, 321, 327);
               mypanel.add( Canevas);
               setJMenuBar(mybar);
               setContentPane(mypanel);
    public static void main(java.lang.String[] args) {
              TestDial td;
              td =  new TestDial();
              td.setModal(true);
              td.addWindowListener(new  WindowAdapter()
              {public void windowClosing( WindowEvent e)
               {System.exit(0);};});
              td.setVisible(true);
    }

    After posting this append, I re-read the swing documentation, and found the solution myself.
    I defined a super class of JPanel and changed the paint method by paintComponent(g). It works fine now.

  • Place image on canvas

    Hi.
    I am trying to place image on canvas.
    However the image doesn't show!!!!!!!!!!!!! :(
    no error and compiles well....
    private void AddTile(String image, int startx, int starty, int endx, int endy)
    try{
    tiles = ImageIO.read(new File(image));
    catch(Exception e)
    System.out.println("Image not Found");
    Graphics2D a =(Graphics2D)tiles.getGraphics();
    a.drawImage(tiles, 20,20,20,20,TileCanvas);
    if the above function is called the image should be placed on canvas right?
    TileCanvas is a canvas, and tiles is BufferedImage.
    just in case I'll put my source code followed by this
    * MapEditor.java
    * Created on 2006�� 7�� 20�� (��), ���� 11:48
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.imageio.*;
    import java.io.*;
    * @author SeiKwon
    public class EditorComponent extends javax.swing.JFrame
    private java.awt.Button AddTile;
    private java.awt.Button DeleteTile;
    private javax.swing.JMenu FileMenu;
    private javax.swing.JMenuItem LoadFile;
    private javax.swing.JPanel ManageTilePanel;
    private java.awt.Canvas MapCanvas;
    private javax.swing.JMenuBar MenuBar;
    private javax.swing.JMenuItem SaveFile;
    private javax.swing.JScrollPane ShowMapScroll;
    private java.awt.Canvas TileCanvas;
    private javax.swing.JScrollPane TileScroll;
    private BufferedImage tiles;
    private int tilecount = 0;
    /** Creates new form MapEditor */
    public EditorComponent()
    initComponents();
    AddTile("test.jpg", 0, 0, 10, 10);
    private void initComponents()
    ManageTilePanel = new javax.swing.JPanel();
    TileScroll = new javax.swing.JScrollPane();
    TileCanvas = new java.awt.Canvas();
    AddTile = new java.awt.Button();
    DeleteTile = new java.awt.Button();
    ShowMapScroll = new javax.swing.JScrollPane();
    MapCanvas = new java.awt.Canvas();
    MenuBar = new javax.swing.JMenuBar();
    FileMenu = new javax.swing.JMenu();
    SaveFile = new javax.swing.JMenuItem();
    LoadFile = new javax.swing.JMenuItem();
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    ManageTilePanel.setLayout(null);
    ManageTilePanel.setBorder(new javax.swing.border.TitledBorder("Tiles"));
    TileCanvas.setBackground(new java.awt.Color(255, 255, 255));
    TileScroll.setViewportView(TileCanvas);
    ManageTilePanel.add(TileScroll);
    TileScroll.setBounds(10, 22, 100, 230);
    AddTile.setLabel("Add");
    ManageTilePanel.add(AddTile);
    AddTile.setBounds(10, 260, 50, 26);
    DeleteTile.setLabel("Delete");
    ManageTilePanel.add(DeleteTile);
    DeleteTile.setBounds(60, 260, 50, 26);
    getContentPane().add(ManageTilePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 10, 119, 290));
    ManageTilePanel.getAccessibleContext().setAccessibleName("TilesPanel");
    MapCanvas.setBackground(new java.awt.Color(255, 255, 255));
    MapCanvas.addMouseListener(new java.awt.event.MouseAdapter()
    public void mouseEntered(java.awt.event.MouseEvent evt)
    MapCanvasMouseEntered(evt);
    ShowMapScroll.setViewportView(MapCanvas);
    getContentPane().add(ShowMapScroll, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 10, 290, 290));
    FileMenu.setText("File");
    FileMenu.setContentAreaFilled(false);
    SaveFile.setIcon(new javax.swing.ImageIcon("D:\\java\\MapEditor\\Image\\Save16.gif"));
    SaveFile.setText("Save");
    SaveFile.addActionListener(new java.awt.event.ActionListener()
    public void actionPerformed(java.awt.event.ActionEvent evt)
    SaveFileActionPerformed(evt);
    FileMenu.add(SaveFile);
    LoadFile.setIcon(new javax.swing.ImageIcon("D:\\java\\MapEditor\\Image\\Open16.gif"));
    LoadFile.setText("Load");
    LoadFile.addActionListener(new java.awt.event.ActionListener()
    public void actionPerformed(java.awt.event.ActionEvent evt)
    LoadFileActionPerformed(evt);
    FileMenu.add(LoadFile);
    MenuBar.add(FileMenu);
    setJMenuBar(MenuBar);
    pack();
    private void MapCanvasMouseEntered(java.awt.event.MouseEvent evt)
    Cursor cursorshape = MapCanvas.getCursor();
    MapCanvas.setCursor(cursorshape.getPredefinedCursor(CROSSHAIR_CURSOR));
    private void AddTile(String image, int startx, int starty, int endx, int endy)
    try{
    tiles = ImageIO.read(new File(image));
    catch(Exception e)
    System.out.println("Image not Found");
    Graphics2D a =(Graphics2D)tiles.getGraphics();
    a.drawImage(tiles, 20,20,20,20,TileCanvas);
    private void LoadFileActionPerformed(java.awt.event.ActionEvent evt)
    FileDialog fd = new FileDialog(this, "Open", FileDialog.LOAD);
    //fd.setFile("*.map");
    fd.setDirectory(".");
    fd.setVisible(true);
    String filename = fd.getFile();
    System.out.println(filename);
    private void SaveFileActionPerformed(java.awt.event.ActionEvent evt)
    FileDialog fd = new FileDialog(this, "Save", FileDialog.SAVE);
    //fd.setFile("*.map");
    //fd.setFilenameFilter("*.map"); // HOW TO USE?????????????
    fd.setDirectory(".");
    fd.setVisible(true);
    String filename = fd.getFile();
    System.out.println(filename);
    public static void main(String args[])
    java.awt.EventQueue.invokeLater(new Runnable()
    public void run()
    new EditorComponent().setVisible(true);
    }

    You have at least two problems.
    1) You are mixing AWT and Swing. Instead of using a Canvas you should use either a JComponent or a JPanel.
    2) In Swing, drawing such are you are doing should be done within the paintComponent(Graphics g) method using the Graphics provided as an argument to the method.
    P.S. I can't test you code because for some reason you feal the need to use a NetBeans specific layout manager.

  • Forms 6i, Stacked & Tabbed Canvas- Wierd behaviour

    Hi,
    I am having a wierd problem:
    Scenario:
    I have a datablock of 40 columns.
    I put 1 (say col-1 on page_1 and col-21 on page_2) column each on two different TAB pages.
    I created 2 stacked canvases: page_1_stacked & page_2_stacked
    I put col-2 to col-10 on page_1_stacked
    and col-22 to col-40 on page_2_stacked
    I used the following code to show/hide the appropriate cols/stack canvases when the users changes Tab pages:
    [when-tab-page-changed
    if (:system.tab_new_page ='PAGE_1')
         then
         show_view('PAGE_1_STACKED');
         hide_view(''PAGE_2_STACKED'');
    elsif (:system.tab_new_page ='PAGE_2')
         then
         hide_view('PAGE_1_STACKED');
         show_view(''PAGE_2_STACKED'');
    end if;
    Problem:
    Everything seems to work fine, the stacked canvases show/hide as expected, but --->as soon as I put my cursor in any column on TABBED PAGE (here col-1 or col-21), all the stacked canvases vanish
    -->if as a result of above case where I put the cursor in a tabbed-page col and canvas vanished, I navigate to the next field (which is on canvas), the canvas shows up (which is normal)....but then this canvas wont get hidden with my hide view... and end up with all stacked canvases shown together or none at all
    I have played around with all the properties and am really trapped.
    Please help!
    Thanks.

    Devang, are you aware that if you put the cursor focus in a field, Forms will ignore any show_view or hide_view commands if they cause the cursor to disappear? And if your cursor focus is in a field in a stacked view, hide_view will not work. Forms will not let you hide or cover up what ever item is identified by :System.Cursor_Item.
    I do not know why you need Col_1 or col_21 on the tab canvas. Put them on your stacked canvasses, too.
    Use a when-new-item-instance block-level trigger to determine which tab page should be displayed. If :System.trigger_item = col_1 through col_20, make sure Page_1 is displayed, and for col_21 through col_40, make sure Page_2 is displayed.
    Similarly, use a when-tab-page-changed trigger to move the cursor to the correct canvas. You wouldn't even need to use show_view or hide_view, Forms will do that for you each time you move into an item on either canvas.
    And why are you using two canvases anyway? Since you are using a horizontal scrollbar, why not put them all on a single canvas? (And then you wouldn't need the tab pages.)

  • (another) stacked canvas on content canvas problem

    I've searched the forums for the last 2+ hours and can't find a solution for my problem, although there are hundreds of questions on this subject.
    I have a stacked canvas on top of a content canvas. The canvases share the same window. They also share a data block. I added radio buttons on the main canvas to "show/hide" the stacked (my ultimate goal is to have 2-3 stacked and toggle. When hide, the stacked canvas is hidden, but when i click show, it is not. I understand reading all the posts that the block with focus will display, and I've added both a go_block and go_item prior to the show_view. Below is my code. MEL_DETAILS is content and MEL_LINE_ITEM is stacked.
    IF :stack = 'SHOW_STACK' THEN
    GO_BLOCK('MEL_ITEM');
    GO_ITEM('MEL_ITEM.MIS_PROJECT_NO');
    SHOW_VIEW('MEL_LINE_ITEM');
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_true);
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', display_position, 718, 583);
    SET_ITEM_PROPERTY('PROJECTS.STACK',MOUSE_NAVIGATE,PROPERTY_FALSE); -- radio button
    ELSif :stack = 'HIDE_STACK' THEN
    HIDE_VIEW('MEL_LINE_ITEM');
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_false);
    SET_ITEM_PROPERTY('PROJECTS.STACK',MOUSE_NAVIGATE,PROPERTY_TRUE);
    END IF;

    user12198246 wrote:
    I've searched the forums for the last 2+ hours and can't find a solution for my problem, although there are hundreds of questions on this subject.
    I have a stacked canvas on top of a content canvas. The canvases share the same window. They also share a data block. I added radio buttons on the main canvas to "show/hide" the stacked (my ultimate goal is to have 2-3 stacked and toggle. When hide, the stacked canvas is hidden, but when i click show, it is not. I understand reading all the posts that the block with focus will display, and I've added both a go_block and go_item prior to the show_view. Below is my code. MEL_DETAILS is content and MEL_LINE_ITEM is stacked.
    IF :stack = 'SHOW_STACK' THEN
    GO_BLOCK('MEL_ITEM');
    GO_ITEM('MEL_ITEM.MIS_PROJECT_NO');
    SHOW_VIEW('MEL_LINE_ITEM');
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_true);
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', display_position, 718, 583);
    SET_ITEM_PROPERTY('PROJECTS.STACK',MOUSE_NAVIGATE,PROPERTY_FALSE); -- radio button
    ELSif :stack = 'HIDE_STACK' THEN
    HIDE_VIEW('MEL_LINE_ITEM');
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_false);
    SET_ITEM_PROPERTY('PROJECTS.STACK',MOUSE_NAVIGATE,PROPERTY_TRUE);
    END IF;Here, it seems two problem..
    The line..
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_true);
    and
    SET_VIEW_PROPERTY('MEL_LINE_ITEM', visible, property_false);No needed
    Hope this helps
    Hamid

  • Can I rollover an image to "remove" it to have clickable links show up once at hidden state?

    I have a design wherein I want certain elements to change states on rollover. With this action, there would be a line of text with each one clickable to a different link. Right now I cannot find a good way to have one image "hide" on rollover, reveal text below AND have the text clickable. This is most likely due to the object's opacity being at 0% rather than truly "hidden". Under the text, there is a different image to make text appear more clearly.
    Am I missing a fundamental element with using states? Even when trying to use the State Widget it doesn't seem to be able to work within different layers and no combination of state changes allows my text to be hidden behind image on the load, rollover to remove object obscuring text AND having it truly "hidden" or "gone" at the entire time the object is being rolled over.
    Thanks in advance for your time, and hope there is a (somewhat) easy solution.

    The simplest way:
    Go to the widget library and you will find a state button. Place it onto the canvas.
    In normal state set opacity of the text box to "0" and fill the button (not the text box) with an image.
    In active state reduce the opacity of thebachground  image to, lets say, 15 % and the opacitiy of the text box to 100%. Assign a link to the text box using the link command in the upper command strip..
    Choose the rollover and mousedown state as you like.

  • Adding a JMenuBar

    I am trying to create a simple JMenuBar in the application enlosed below.
    I keep getting the following error when ever i compile
    "Screen.java": Error #: 300 : method setJMenuBar(javax.swing.JMenuBar) not found in class random.Screen at line 67, column 9". Can some one please enlighten me.
    Class Screen is the main class. This is where the GUI is set up. it calls class Canvas which in turn calls class Nodes. GUI draws a bus network which holds a minimum of two nodes and a maximum of 10. The node number is under user control. The aim is to allow a user to select the configure menu to update the number of nodes on display in the GUI.
    package random;
    import javax.swing.JPanel;
    import javax.swing.border.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    * <p>Title: Simulation of the random load balancing algorithm </p
    public class Screen extends JPanel {
    static int screenWidth = (int)(Toolkit.getDefaultToolkit().getScreenSize().width*0.85);
    static int screenHeight = (int)(Toolkit.getDefaultToolkit().getScreenSize().height*0.95);
    private Container container;
    JButton add;
    Canvas canvas;
    JMenuBar menubar;
    JMenu configure;
    Thread animatorThread;
    RandomApplet RApplet;
    public Screen(RandomApplet RApplet){
         this.RApplet = RApplet;
         makeGui();
    public Screen(){
         makeGui();
    public void makeGui(){
    addMenu();
    GridBagLayout gridbag = new GridBagLayout();
         setLayout(gridbag);
         GridBagConstraints c = new GridBagConstraints();
         c.insets = new Insets(1,1,1,1);
         //add drawing canvas
         canvas = new Canvas();
         c.gridx = 0;
         c.gridy = 0;
         c.weightx=1.0;
         c.weighty=1.0;
         c.fill=GridBagConstraints.BOTH;
         gridbag.setConstraints(canvas, c);
         add(canvas);
         c.gridx=0;
         c.gridy=1;
         gridbag.setConstraints(canvas,c);
         add(add=addButton("Add"));
    private void addMenu(){
         menubar = new JMenuBar();
    setJMenuBar(menubar);
         configure = new JMenu("Configure");
         configure.add(new JMenuItem("add"));
         menubar.add(configure);
    private JButton addButton(String name) {
              JButton b = new JButton(name);
              ButtonHandler handler=new ButtonHandler();
              b.addActionListener(handler);
              b.setFont(new Font("Dialog", Font.BOLD, 12));
              b.setMargin(new Insets(0,0,0,0));
              return b;
    class ButtonHandler implements ActionListener{
         public void actionPerformed(ActionEvent e){
              if (e.getSource()== add){
                   if (canvas.addNode()){
                   repaint();
    public void run(){
         Thread thisThread = Thread.currentThread();
         while (animatorThread == thisThread){
              try {
              Thread.sleep(100);}
              catch (Exception e){
              break;}
    public static void main(String[] args) {
    JFrame f=new JFrame("Random");
         f.setSize(screenWidth, screenHeight);
         f.getContentPane().add(new Screen());
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    //class Canvas
    package random;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.*;
    import java.awt.Graphics;
    import java.awt.image.*;
    import javax.swing.*;
    public class Canvas extends JPanel{
    int width, height;
    Dimension size;
    Image canvasImage = null;
    Graphics canvasGraphics;
    Image networkMem;
    int NodeWidth, NodeHeight;
    Nodes[] n;
    int numberOfNodes;
    int x1, y1,x2,y2;
    public Canvas(){
    // canvas a perfect square
         height = 400;
         width = 400;
         size=new Dimension(width,height);
         setBackground(Color.white);
         networkMem=Toolkit.getDefaultToolkit().createImage("images/computer6.gif");
         NodeWidth = networkMem.getWidth(this);
    NodeHeight = networkMem.getHeight(this);
         //network must hold a minimum of 2 nodes
         n = new Nodes[11];
         n[0] = new Nodes(0,this);
         n[1] = new Nodes(1,this);
         numberOfNodes = 2;
    public void paintComponent(Graphics g)
    super.paintComponent(g);
         //create canvas image
         if (canvasImage==null){
              canvasImage=createImage(getSize().width,getSize().height);
              canvasGraphics = canvasImage.getGraphics();
         //adjust above to fit canvas window
    if ((canvasImage.getWidth(this) != getSize().width) || (canvasImage.getHeight(this) != getSize().height)){
         canvasImage = createImage(getSize().width,getSize().height);
              if (canvasImage!=null){
              canvasGraphics.dispose();
              canvasGraphics = canvasImage.getGraphics();
         //create a rectangular border outlining canvas area
         canvasGraphics.drawRect(0,0,getSize().width-1,getSize().height-1);
         x1=width-100;
         y1=height-100;
         x2=width-300;
         y2=width-100;
    canvasGraphics.drawLine(x1,y1,x2,y2);
    //canvasGraphics.drawLine(width-300,height-100,width-300,height-50);
         //canvasGraphics.drawLine(width-100,height-100,width-100,height-50);
         for(int i=0; i<numberOfNodes; i++){
         n.drawNode(canvasGraphics);
         g.drawImage(canvasImage, 0, 0, this);
         super.paintComponent(canvasGraphics);
    public int getNumberOfNodes()
         return numberOfNodes;
    public boolean addNode(){
         //add a node to network
         if (numberOfNodes < 10) {
         n[numberOfNodes]= new Nodes(numberOfNodes, this);
         numberOfNodes++;
         repaint();
         return true;
         return false;
    public boolean removeNode(){
         boolean removed = false;
         if (numberOfNodes >2) {
         numberOfNodes=numberOfNodes - 1;
         removed = true;
         else{
         removed = false;
         repaint();
         return removed;
    //class Nodes
    package random;
    import javax.swing.JPanel;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics;
    public class Nodes{
    int nodeNumber;
    Canvas canvas;
    public Nodes(int number, Canvas c) {
    //each node has a number id
         nodeNumber=number;
         canvas=c;
    public void drawNode(Graphics g){
         if (nodeNumber%2 == 0){
    canvas.x1=canvas.x1-10;
         System.out.println(""+canvas.x1);
         g.drawLine(canvas.x1,canvas.y1,canvas.x1,canvas.y2-50);
         g.drawImage(canvas.networkMem,canvas.x1,canvas.y1-80,canvas);
         else{
         canvas.x1=canvas.x1-30;
         System.out.println(""+canvas.x1);
         g.drawLine(canvas.x1,canvas.y1,canvas.x1,canvas.y2+50);
         g.drawImage(canvas.networkMem,canvas.x1,canvas.y2+50,canvas);

    Thanks for the reply but i figured it out in the end.
    I can make use of the following commands
    setMenuBar(MenuBar mb)
    getMenuBar()
    to set the main frame menubar to that declared in addmenu() .
    if I add the following code to main() in the screen class the menu appears
    Screen screen = new Screen(null);
    f.setJMenuBar(screen.getMenuBar());

  • Dropdown Menu bar being hidden by the Textarea

    My problem is that the menu bar does show up, but the text area is over the dropdown menu. You can see the drop down menu start but then is hidden under the textarea. I hope that a good enough discription.
    The frame is a typical JFrame that contains a BorderLayout. My class TheFrame is an extention of JFrame. In the CenterPanel I have a Text area. In the south panel I have another BorderLayout with a text area as the center and a button for the east panel. The menu is going under the text area in the center panel. This might be a bug with the borderlayout?
    public TheFrame(){
    super("Com");
    getContentPane().setLayout(new BorderLayout());
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    menuBar.add(getMyMenuBar());
    getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
    getContentPane().add(getSouthPanel(), BorderLayout.SOUTH);
    pack();
    private JMenu getMyMenuBar(){
    JMenu menu = new JMenu("File");
    menu.add(createConnectItem());
    menu.add(new JMenuItem("test"));
    return menu;
    private JMenuItem createConnectItem(){
    JMenuItem connect = new JMenuItem("New");
    ActionListener listener = new ConnectMenuItemActionListener(com);
    connect.addActionListener(listener);
    return connect;
    }

    I don't see the textarea in the code you posted.
    But I know that the described problem comes up when
    you use "TextArea" unstead of "JTextArea".
    "TextArea" is an awt component and "JTextArea" is
    the corresponding swing component. You should not
    mix awt and swing components in the same GUI.
    However, "TextArea" has the scrolling facility already included.
    With "JTextArea" you must use JScrollPane if you need
    scrolling.
            JScrollPane scrollpane = new JScrollPane();
            JTextArea textarea = new JTextArea();
            scrollpane.setViewportView(textarea);

  • How to make the scroll bar in flex automate to focus on the new ui component added in the canvas?

    Hi all ,
    There is a canvas container where am adding charts in separate windows.
    So whenever a new chart is added the scroll bar needs to set the focus on the present chart window.
    For this i made the functionality for the canvas container to scroll whenever a new chart is added using the below code
    canvasContainer.verticalScrollPosition = canvasContainer.maxVerticalScrollPosition;
    But the calculations for the vertical position are not precise...
    Is there anything else I should do to make the scroll happen automatically as the chart windows get added in the container ??
    Any suggestions pls
    Regards,
    Ajantha

    Many many thanks to Frank,
    In the css,use the follwing style settings to hide the scroll bar
    af|carousel::spin-bar{    
    visibility: hidden;
    af|carousel::spin-h-previous-icon-style{
    visibility: hidden;
    af|carousel::spin-h-next-icon-style{
    visibility: hidden;
    af|carousel::spin-info{
    visibility: hidden;
    }

  • Re: adding canvas to applet

    Hi
    I am trying to add a canvas to an applet. I am successful in doing so. There is a JMenuBar added to the applet. when I click on the file menu The contents are being displayed behind the canvas and as a result I am neither able to see the buttons nor select them. Could some one help me with this.
    Note: Once you compile the code you wouldn't be able to see the contents right away. Click on left top,immidiately below the applet menu. This will repaint the contentpane. This is some thing I am working on.
    applet class
    package aple;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.geom.AffineTransform;
    import java.util.ArrayList;
    import javax.swing.JApplet;
    public class NewJApplet extends JApplet{
        public Graphics2D g2;
        AffineTransform atf = new AffineTransform();
        sketch sk = new sketch();   
            @Override
        public void init() {      
            getContentPane().add(sk);
           Gui gui = new Gui();
            // menubar
            this.setJMenuBar(gui.Gui());     
        @Override
    gui class
    package aple;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    public class Gui {
        JMenuBar menuBar;
        JMenu file,edit,insert,view,draw,circle;
        JMenuItem nu,close,saveAs,open,centreandradius,line,rectangle,point,arc;
        public JMenuBar Gui(){
            //Menubar
            menuBar = new JMenuBar();
            //Menus
            file = new JMenu("File");
            menuBar.add(file);
            edit = new JMenu("Edit");
            menuBar.add(edit);
            view = new JMenu("View");
            menuBar.add(view);
            insert = new JMenu("Insert");       
            draw = new JMenu("Draw");
            circle = new JMenu("Circle");
            draw.add(circle);
            insert.add(draw);       
            menuBar.add(insert);
            //MenuItems
            nu = new JMenuItem("New");
            file.add(nu);
            open = new JMenuItem("Open");
            file.add(open);
            saveAs = new JMenuItem("SaveAs");
            file.add(saveAs);
            close = new JMenuItem("Close");
            file.add(close);
            line = new JMenuItem("Line");
            draw.add(line);
            centreandradius = new JMenuItem("Centre and Radius");
            circle.add(centreandradius);
            rectangle = new JMenuItem("Rectangle");
            draw.add(rectangle);
            point = new JMenuItem("Point");
            draw.add(point);
            arc = new JMenuItem("arc");
            draw.add("Arc");
            return(menuBar);
    sketch class
    package aple;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    public class sketch extends Canvas{
        public void sketch(){
            this.setBackground(Color.green);
        @Override
        public void paint(Graphics g){
         // g.fillRect(50,50, 50, 50); 
    }

    When you were using JPanel, your "setBackground" didn't work because you were overriding its paint(Graphics) method without calling "super.paint(g);". If you don't do this, the panel won't paint its background.
    You should also override "protected void paintComponent(Graphics g)" in any JComponent, such as JPanel, instead of just "paint(Graphics)". (And don't forget to call super.paintComponent(g) FIRST in your subclass's overridden method, if you want the background painted):
    class MyJPanelSubclass extends JPanel {
       protected void paintComponent(Graphics g) {
          super.paintComponent(g); // Paints background and any other stuff normally painted.
          // Do your custom painting here.
    }

Maybe you are looking for

  • Itunes wont open because of quicktime

    ok i downloaded the latest itunes download and restarted my comp now if i try to open the open itunes i get a message stating itunes cannot run because it detects a problem with quicktime and if i click the help button nothing happens plz help me i h

  • How do I change the original email address that was used to set up iCloud

    I have just noticed that the email under system preferences for iCloud is an old one that does not exist anymore. How do I change this? Can you also tell me if I delete this account - will it affect what I have stored on the computer e.g. what is in

  • How to install PS CS6 Extended on a computer with no disc drive? [was: Help]

    I just purchsed Photoshop CS6 Extended, got home and totally forgot my computer does not have a disk drive. How do I install it now??

  • Sildeshows in Vertical HDTV (1080 wide x 1920 tall)  do not work!

    I have setup a HDTV in vertical form, change my screen resolution and orientation accordingly, everything looks fine from the mac side (snow leopard in a brand new mac mini) when I try to setup a slideshow with iPhoto '11 it just does not do anything

  • Correlation with multi sources

    Hi All I have a probloem like this I have one requirement that need to process with 2 messages but key field of 2 message that is not same at all like this. Msg 1 <row>          <f1>test_0</f1>          <f2>test_0_f2</f2> </row> <row>          <f1>te