Adding buttons to Canvas

Hi
i have a class that extends Canvas.
can i add buttons to the canvas?
i mean, i want to make the images in the canvas clickable.
is this possible in Java?
Thank for help.

This is the complete code:
import java.awt.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.image.*;
import javax.swing.ImageIcon;
class IconCanvas extends Canvas{
Applet app;
Image h;
Image s;
ImageIcon h_icon ;
ImageIcon s_icon ;
public void init(Applet app)
     this.app=app;
     h = app.getImage(app.getCodeBase(), "hidey.bmp");
     s = app.getImage(app.getCodeBase(), "salley.gif");
    h_icon = new ImageIcon( h) ;
    s_icon = new ImageIcon( s) ;
public void paint(Graphics g)
         add(h_icon);  // Error Here because add applied only to awt component not swing component(ImageIcon)
public class CanvasTest extends Applet {
     IconCanvas canvas;
     Panel button_panel;
     public void init()
            canvas = new IconCanvas();
          canvas.init(this);
           button_panel = new Panel();
          button_panel.setLayout(new BorderLayout());
          this.setLayout(new BorderLayout());     
          add("Center", canvas);  
}

Similar Messages

  • Adding Buttons to a ScrollPane??

    Hello Swing people,
    I have had this problem now for quite some time and still have had no luck in getting it right. I am coding an ordering system and on this form is a Jtable displaying product details. Beside the Jtable are category buttons one after the other in a vertical line and there are 18 of them - meaning they cannot fit on the screen!! Therefore i want these buttons to be in some kind of scollpane or list so they can all be viewed.
    I thought this would be a simple matter of adding buttons to a scrollpane and then to the panel but this has proved not the case! Does anyone know how i could do this as i have been trying this for too long without the help of you geniuses???
    To give u an idea of what i was trying here is a snippet of my code:
    import javax.swing.*;
    public class junk2 extends JFrame{
         junk2()    {
         JPanel panel = new JPanel();
         panel.setLayout(new java.awt.GridLayout(0, 1) );
         JButton []button = new JButton[20];
         for(int i=0; i<button.length; i++) {
              button=new JButton(""+(i+1) );
              panel.add(button[i]);
         JScrollPane scroll = new JScrollPane(panel);
         getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
         setSize(50, 100);
         setDefaultCloseOperation( EXIT_ON_CLOSE );
         public static void main(String []args) {
              new junk2().show();
    This code is fine and puts buttons in a vertical line but the buttons only have numbers on them from 1-20, i want the names of the buttons i have declared to be in here - therefore allowing the action performed on these buttons to be recognised and therefore work too! - anyone know how to get around this??
    Also the scrollpane with these buttons in apears in a whole new frame being that it is a small test program but does anyone know how to ensure the buttons appear in the scrollpane only and not in a new frame??
    all 25 dukes if someone can sort this long problem out!!!

    Hi guys thanks a lot for your replies, i compiled both of them and they are fine! the first reply is more what i am looking for but there is still a problem, i am finding it difficult to incorporate this piece of code into my order form class.
    Im trying to take out the current declarations of my JButtons and replace it with the code i was given (reply 1) and obviously keep my action performed methods there as they will be the same but cant seem to do this!
    If anyone can add in the code from reply 1 into my code below or at least help me i will give u the 25 dukes, taking into consideration i still want the form (panel) to be displayed as well as the panel or scrollpane containing the JButtons???
    Can u help - cheers in advance!!
    Pc
    public class OrderForm extends JFrame implements ActionListener {
         private JPanel panel;
         private JButton saveArchive,unCompleted,submitOrder,checkSheet,clear,exit,bread,fruit,dairy,
    salads,cakes,frozen,meat,cheeses,sauces,dry,drinks,soup,cleaning,boxes,cups,cutlery,lids,misc,stick,soup2;
         private JScrollPane pane;
         private JTextField textorderno,textorderdate,textdelivdate,textemp,textshop,textsupp;
         private JLabel heading1,heading2,orderno,orderdate,delivdate,emp,shop,supplier;
         private String todaysDate, deliveryDate;
         private ResultSet result;
         private ResultSet shops;
         private int column_count;
         private JComboBox combobox;
         ResultSetTableModel model = new ResultSetTableModel();
         Result rs = new Result(); 
         Orders orderSaving = new Orders();
         ProductOrders tableSaving = new ProductOrders();
         private boolean save;
         JTable table;
         public OrderForm()throws SQLException {
              displayForm();
              displayFields();//Have not provided coz it aint needed for this
              displayButtons();//Wont need this anymore i dont think
              getContentPane().add(panel);
              setVisible(true);
         public void displayForm() throws SQLException{
              setTitle("Pret Ordering System");
              setSize(700,600);
              // Center the frame
              Dimension dim = getToolkit().getScreenSize();
              setLocation(dim.width/2-getWidth()/2, dim.height/2-getHeight()/2);
              //Creates the coloured bored around the panel
              panel = new JPanel();
              panel.setLayout( null );
              Border paneltitled = BorderFactory.createLineBorder(Color.red.darker().darker(),10);
              panel.setBorder(paneltitled);
              ResultSet result = rs.setBreadProds();
              model.fillTableModel(result);
              table = new JTable(model);
              for(int i = 0; i < column_count; i++) {          
                   table.getColumnModel().getColumn (i).setPreferredWidth (200);                   
              table.setSelectionForeground( Color.white );
              table.setSelectionBackground( Color.red.darker().darker() );
              pane = new JScrollPane(table);
                  pane.setBounds(190, 230, 440, 200);
                  panel.add(pane);
              combobox = new JComboBox();
              ResultSet shops = rs.getSupplierNames();
              while(shops.next()){  
                   combobox.addItem(shops.getString("supplier_name"));
    //Hope you can help - cheers

  • Captiave 2 - Adding Buttons to Question Slides

    I need to use the Captivate Question Slide "maker" to create
    interactive questions with navigation back to the "Jeopardy-esque"
    main page. When doing so, the question slide maker prevents adding
    buttons/navigation, etc.
    Any work arounds, hints, suggestions?
    Thanks...

    Hi Beta Bob and welcome to our community
    As you have seen, you cannot insert interactive objects
    (Buttons, Click Boxes or Text Entry Boxes) on Question slides.
    The only workaround I can think of is to sort of simulate the
    question slide using these objects on a standard slide.
    Cheers... Rick

  • Standard buttons not displaying ALV when added button by creating PF status

    Hi  All,
    I have added a button(using SE41) in application toolbar for ALV grid using Factory method.But the problem is I am unable to see the standard tool bar buttons(Layout,Download,Mail etc).
    Even I tried adding buttons(using standard FCODES) to the PFstatus.Now the buttons are visible but the functionality is not working for all buttons.Its working only for Graphic and Change layout buttons.
    Can any one suggest...??
    -Phaniram

    Disregard DKS's response...that's the older stuff and you're using NetWeaver ALV.
    In SALV you can cause those buttons to appear, but you may need to copy the SALV_STANDARD GUI from one of the SALV* named program using SE80...sounds like you have that part, but you may need to turn functions ON...
    Perhaps something like this would help:
    data:  gr_f_list  type ref to cl_salv_functions_list,
             gr_alv     type ref to cl_salv_table,
    SALV toolbar & GUI functions
      gr_f_list = gr_alv->get_functions( ).     "Get Toolbar functions
      gr_f_list->set_all( abap_true ).          "All On
      gr_f_list->set_view_lotus( abap_false ).  "Lotus 123 off
      gr_f_list->set_view_excel( abap_false ).  "Excel in Place Off
      gr_f_list->set_graphics( abap_false ).    "Graph tool off
    For undesired buttons, set each tool OFF after turning all ON.

  • How to make separation between added buttons into a jToolBar?

    Hi friends,
    I have made a jToolBar (using NetBeans IDE 5.5) with several buttons, but i dont know how to make custom separation between those added buttons. I will appreciate it if anyboy give me some tips.
    Thanks in advance,
    Reza_mp

    I don't know about NetBeans but yeah U can add horizontal atruts and verticals struts between two components, i.e.
    1.) add first component
    2.) add horizontal or vertical struts(according to ur toolbar)
    3.) add second component
    4.) add add horizontal or vertical struts(according to ur toolbar)
    this will definitely solve ur problem.
    If it does, don't forget to pay ur points.

  • Recently added     button accidently deleted How do I get this button back?

    'Recently added' button accidently deleted How do I get this button back?
    I can not find where my tracks off a cd are
    on my imac with out this button or
    icon on the left hand side of itunes.
    HELP I am technically challenged but
    relatively intelligent musician!

    catcowkitty wrote:
    'Recently added' button
    that button is actually a *smart playlist* which you can create yourself quite easily.
    in iTunes, choose +new smart playlist+ from the file menu and recreate the following rules:
    the screenshot is from my original recently added playlist.
    JGG

  • Can i restore the 'recently added' button in the sidebar of iTunes?

    It happend that I accidencially erased the 'Recently Added' button from the sidebar in iTunes which is a bit of a dissaster for me as I often add mp3's from musicproductions elsewere. It makes it quite difficult to find the songs without that button.

    Hi Jimzgoldfinch
    It worked, i have a new smart playlist up and running. That's great, thanks a lot!
    Sam Foh

  • Adding label to Center button on Canvas

    I have an application based on the Canvas class, and I would like to add a label to the center button on the screen. The application is a muliti-choice list, so I would like this label to be variable based on if the item is selected or not.

    ... ... an application based on the Canvas class ... ...j2me apps are based on the MIDlet class. No two ways about it. Your application probably uses or extends Canvas for its primary UI.
    ... ... I would like to ... ...{color:#000080}So what have you done so far? You need to use
    -- pointerPressed(), paint(...)
    -- ItemStateListener, itemStateChanged(...)
    and whatever you code will not be device independent.
    There is at least one Motorola model on which the call button and not the center button fires pointerPressed().
    If you have a problem with your code, ask a specific question. To post code, use code tags --{color} [code]CODE[/code] {color:#000080}is displayed as CODEdb{color}
    edit{color:#000080} Answer this too: Are you going to follow this topic through and let us know whether your problem is solved or are you going to drop it like you did your last topic?
    {color}{color:#0000ff}http://forum.java.sun.com/thread.jspa?threadID=5220400{color}
    Edited by Darryl.Burke

  • Adding button to a custom table cell (and handling them)

    Hi
    I'm wondering if it's possible to add a button on each row of a table view using a custom table cell view. If it's possible, I'm really wondering how I can handle the click events on these buttons.
    Thanks

    No.
    Try creating a view and adding it to a cell. Let us know what problems you have, and we can try to help.

  • How to add a button in  Canvas!?!

    Hi, I need help with programming a Canvas object.
    Excuse my english, I'm not very good.
    My problem is to add a button in a Canvas class,...
    There's no add() method in Canvas so how could I solve this.
    THX
    WSM

    Can I use Panel to create own objects???
    momentary I using Canvas like this,..
    import java.awt.*;
    import java.awt.event.*;
    public class myObject extends Canvas {
    public myObject() {
    public void paint (Graphics g) {
    mouseevents....
    to create my own Object. But I want to give it some functions and odd some other Objects,...
    THX
    WSM

  • 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;
    }

  • Adding Button to Active Form

    Hi,
    I created button in PO  on Form load event. its working fine.but
    how to add a button on the active(PO)  form once My add-on is started?
    Regards,
    Ganesh k

    Hi,
    i checked for the existance of my button in my active form, if not exist i added my button in et_FORM_ACTIVATE  Event. its working now.
    If pVal.EventType = et_FORM_ACTIVATE And pVal.FormTypeEx = "142" And FLAG = False Then
                oOrderForm = sbo_application.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)
                Dim ii As Integer
                For ii = 0 To oOrderForm.Items.Count - 1
                    If oOrderForm.Items.Item(ii).UniqueID = "MyBtnId" Then
                        FLAG = True
                    End If
                Next ii
                If FLAG = False Then
                    ADDBUTTON()
                End If
            End If
    Thanks For the Suggestions..
    regards,
    Ganesh K

  • Problem wiht added button on invoice form

    I have added a custom button on invoice form.
    Scenario 1:
    I open the invoice form then the custom button is added and the et_ITEM_PRESSED event on custom button in catched. All is ok!
    Scenario 2
    I open a custom form and after close it.
    After I open the invoice form then custom button is added but et_ITEM_PRESSED event on custom button in not catched!!
    Could somebody please help me?
    Thanks

    Hi Domenico,
    It could be that the custom form gets an exception and then no other events might be caught. Try and put try...catch clauses around all your code and show messages when an error is received. This might be the cause of the problem - an unhandled exception.
    Hope it helps,
    Adele

  • Adding buttons on title line

    I am relatively new to Flex and I have run into a slight
    problem that I that I think is because of lack of experience. I
    have a panel and I have set the title attribute but now I am
    required to add buttons to the right of the title. I am not sure
    what the best way to do this is or even if it is viable. I have
    read in other places throughout the internet that it is possible to
    add buttons to the title of an object, but I do not know how to do
    to proceed. I have thought about it and I think making an HBox and
    adding a label for the title and then the required buttons might
    work, but when it compiles it leaves a blank space above my new
    HBox that just does not look good. Is there someway to override the
    title of the panel or set its height to 0, or better yet some way
    to add buttons to the title line of the panel? Any help would be
    appreciated.

    Check out post...
    http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=60&catid=585&threadid =1271263&highlight_key=y&keyword1=button

  • Adding scrollbars to canvas!!

    hi everybody,
    a have a simple ??
    i'm working on a chat application. my message displaying area is a canvas.i have added scroll-bars to it using scroll-pane
    "sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);"
    now what i want is the scrollbars should adjust automatically to
    display the last message and also i should be able to adjust them
    manually so that i can view the previous messages.
    i have tried setScrollposition() it works well also but then i'm not
    able to adjust scrollbars manually.
    this interface is in AWT.
    please tell me a solution.its a bit urgent.
    thankx and regards
    asheet

    Because your canvas is growing.(I think)
    The new size of the canvas causes it to be layd out again. And laying out a component usually causes flickering (depending on the plattform). I guess the workaround would be to use a canvas just as big as the shown part of it. Add a scrollbar by its side (not a scrollpane) and use the scrollposition to choose what part of the text that should be painted. That way you can use doublebuffering, and avoid the problem completly. (No resizing of components means nothing is layd out again)
    Ragnvald Barth
    Software engineer

Maybe you are looking for

  • Stock transport scheduling agreement SD Delivery

    Dear Experts. I am using stock transport scheduling agreement in order to show requirements from one plant to another plant belonging to the same company code. Delivery to the Stock Sched Agreem will be managed  thru the SD delivery processes. I use

  • Apps not showing in iphone 5 facebook settings

    I recently did an upgrade on a free game app I have used for months without any problems. This particular game can also be played on Facebook. After the upgrade the game no longer shows in my phone's Settings even though I can access the game through

  • DropTarget check against all objects on the stage

    Hey all, Not sure the best way to do this.  I have a class we will call DropActivity, here is the code package com.activitycontrol           import com.activitycontrol.DropCheck;           public class DropActivity                // Constants:       

  • Counter in Maintenance Plan

    Dear All, i´m not sure. Is it advisable to use a own counter for each maintenance plan and is it better to create a own performance-based maint. plan for each equipment? Thanks for your help! Andreas

  • 1 App-v package for server and clients?

    Hi, Please verify if this setup is correct. The idea is to deploy an App-v 5.0 package to servers as well as pc's. Deploy of an app-v package with 2 deployment types: 1.for a Windows 2008 R2 without any dependency = prereq Windows 2008 R2 (no depende