Overriding paint for customizing JButton??? Shouldn't be paintComponent?

Hi
Shouldn't we always override paintComponent to customize the painting of a component?
Why MetalScrollButton in javax.swing.plaf.Metal overrides paint instead of paintComponent?
Should I override paint if I want to write my own LnF?
Thanks.

As is said in the online doc about paint(), "This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. They're called in the order listed to ensure that children appear on top of component itself. Generally speaking, the component and its children should not paint in the insets area allocated to the border. Subclasses can just override this method, as always. A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent."
That means you can override paint(), , but in this case you'll have to draw the border yourself... and if you have a composite custom component (a panel containing labels, for example), you will have to call the label's paintComponent() methods by yourself...
Hope this helped,
Regards

Similar Messages

  • How to override setText for custom JButton

    Hi
    From the Swings Hack book, I have the code for a custom JButton.
    I would like to setText for this button so that the text is set over the image on the button.
    Would someone know how I can do this?
    import java.awt.*;
    import javax.swing.*;
    public class ImageButton extends JButton {
        public ImageButton(String img) {
            this(new ImageIcon(img));
        public ImageButton(ImageIcon icon) {
            setIcon(icon);
            setMargin(new Insets(0,0,0,0));
            setIconTextGap(0);
            setBorderPainted(false);
            setBorder(null);
            setText(null);
            setSize(icon.getImage().getWidth(null),
                    icon.getImage().getHeight(null));
    }Thanks!

    Something like this should do it...
    import java.awt.*;
    import javax.swing.*;
    public class ImageButton extends JButton {
         private static final long serialVersionUID = 1L;
         public ImageButton(ImageIcon icon, String text) {
              setIcon(icon);
              setMargin(new Insets(0, 0, 0, 0));
              setIconTextGap(0);
              setBorderPainted(false);
              setBorder(null);
              setText(text);
              setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
    }Or just add a public setText() method accepting a String...

  • Override doDML for custom type

    Hi,
    I'm overriding the doDML method of my entity class. Instead of inserting directly into a certain table I redirect the insert handling to a special PL/SQL package method. This works fine, until I want to use a custom as one of the columns of the table / one of the arguments of the package method.
    E.g. the custom type looks like this:
      CREATE OR REPLACE TYPE mylist AS TABLE OF VARCHAR2(10);I use this type for a nested table column in a certain table. The PL/SQL does some pre-/post-processing and inserts a new record in the table my entity object is based on. When I create the entity using the Entity wizard it correctly detects the column as of SQL type mylist and Java type oracle.jbo.domain.Array with elements of type String.
    Anyway, the prepared statement I create looks something like the following:
      String stmt = "BEGIN mypkg.insert(:1, :2); END;";
      PreparedStatement p = e.getDBTransaction().createPreparedStatement(stmt, 2);
      if (getId() != null) p.setInt(1, getId().intValue());
      else p.setNull(1, Types.INTEGER);I want to set the 2nd parameter (which is of the mylist type) in a similar way. But I don't know which SQL type I should choose from. If I choose Types.ARRAY I get an invalid type error, the same goes for Types.OBJECT, and Types.BLOB. And which set... method should I call on the prepared statement? And what should I pass to this method? The Array object of the results of the getData() method of the Array object?
    Regards,
    Peter

    Types.ARRAY should be the right sql-type
    The value should be an oracle.sql.ARRAY instance.
    To prepare and return one of this type you
    first need to prepare the Array domain for DML by calling arrayDomain.prepareForDML(jdbcconn) (passing in the jdbc connection object) and then call arrayDomain.getData()to use in your setObject() call.

  • Design advice for custom painting

    Hi,
    Can someone give me some high-level design advice on designing a JPanel subclass for custom painting? My panel class is becoming very complex, with lots of drawing and scaling methods, so I'm wondering if I could abstract away some of these graphical elements by creating new classes to make the design more object-oriented. However, I'm finding that there are also disadvantages in representing some of my graphic components as classes. Specifically,
    1. It will lead to a much higher level of class coupling. My panel will depend on all these new classes to work correctly. In fact the situation is even worse because my panel is an inner class and, to do some of the scaling, needs to use methods from an object stored in the parent class. I would therefore have to also pass this object reference as an argument to many of these new classes.
    2. It will lead to a lot of awkward passing of data between classes. For example, I need to use g2.drawImage(img, x, y, w, h, this), so I will have to pass not only the graphics context but also the panel reference itself.
    Is it common for panel subclasses that do custom painting to be complex?
    thanks,
    Eric

    I wrote the map view for a commercial GIS system. Drawing and scaling on a JPanel is challenging, but it need not be complex.
    1. To eliminate class coupling, you need to create a couple of interfaces: Renderable (what you want drawn) and Renderer (the thing doing the low-level drawing). Renderer will have before and after setup and reset methods (to do things like scaling and rotation), and methods that the renderables can use to draw graphics. The Renderable interface can be as simple as a single method: draw(Renderer).
    Every type of graphic that you draw on the screen would be a different class that implements Renderable, and which knows how to draw itself using whatever lower-level drawing commands you put in the Renderer. If you construct each Renderable in terms of java.awt.Shape, then Renderable.draw() could call a method Renderer.draw(java.awt.Shape, java.awt.Color).
    2. The Panel becomes fairly simple. It has a Renderer and a collection of Renderable objects. Its paint() method calls the Renderer setup method, calls Renderable.draw(Renderer) on each object, and calls the Renderer reset method. Each Renderable in turn calls Renderable.draw(java.awt.Shape, java.awt.Color) one or more times.
    Renderer should get a Graphics2D from the Panel when the setup method is called. That's when the Renderer does all of the scaling, positioning, and rotation on the Graphics2D. The Renderable implementations shouldn't even need to know about it.
    I don't think custom painting code is necessarily complex, merely challenging to write. If you're only drawing a few lines and circles, you probably don't have to be too concerned about design elegance and code maintainability. The map view I designed for our GIS system, on the other hand, has to handle all kinds of map geometry, icons, text, and aerial photos.

  • Custom paint for a combo box?

    Is there a document anywhere that describes all the parts you need to paint for a combo box? We have a particular need for a special presentation, but combo boxes can be rather complicated to paint all the parts correctly.
    TIA!

    don't know of a document, but an example of a (color) modified comboBox is here
    [http://forums.sun.com/thread.jspa?forumID=57&threadID=5283094]
    you just have to copy all of the code (it does run)

  • Custom JButton

    Can anybody here help me create a class defining a custom JButton?
    I would like to create a Six-sided button that I can stack appropriately for another version of MineSweeper that I'm working on. One way that I can think of is to just make a class that draws it onto a JComponent using the paint() method. Then using a mouseListener for the JComponent and calculating whether this is inside the area or not.
    Of course, if you have lots of buttons this can be very confusing and time-consuming.
    Is there any way I can just override some methods in the JButton class to do the same thing?
    Ideas anybody?
    Thx in advance.
    -Patrick Li

    hi, I would use JButton and overide paint or paintComponent to draw your six side button. What is there to calculate ? You can put all Buttons on a JPanel.If game is in action you can change the Images on the buttons.

  • Custom JButton displays properly on Win2K, not under Solaris

    We have an applet that creates a panel of custom JButtons, each of which has a set of associated icons, two of which are "active" and "inactive" (there are also disabled, rollover, etc. icons). At runtime, when a button is clicked, its icon property is set to the "active" one. If another button in the panel is clicked, the first button has its icon property set to the "inactive" one.
    This displays fine when the applet is running on a Win2k machine (with the Java 1.3 plugin under either Netscape 6.2 or IE 5.5, but when we run it on a SunRay connected to a Sun Ultra80 running Solaris 8 with Netscape 6.2 and the Java 1.3 plugin, the "inactive" icon for one particular button is blank - we get a white rectangle - but when we click on the rectangle (or mouseover it), the appropriate icon appears as it should.
    When I specify a different icon to be used (reusing the .gif file from one of the other buttons)I get the same behaviour. When I reorder the buttons in the panel, the same button (now in a different position) still behaves improperly.
    Any thoughts?
    Thanks,

    No solution, but for what it's worth.
    As part of my "diagnosis by trying things out" (NOT "debugging by..." - or at least I don't admit to that :) I created a second instance of the problem button and stuck it in the panel.
    Lo! the new instance had the problem, BUT THE FIRST WAS FINE. I did a few more experiments, and the first-created instance was the one to have problems, while the second is fine, so the current work-around is to make an instance, ignore it (just leave it as a field of the class that builds the panel, but don't add it to the panel), make another instance and use that in the panel.
    My boss is happy, our client is happy (they have a demo using the a group of Sunrays), but I really wish I knew what was going on. I don't THINK that the problem button was the first instance of its class to be created (I'm at home and the thought just came to me) but it's possible and raises some more hypotheses to test.

  • Transaction code for Custom report

    Hi,
    How to create transaction code for Custom report ?
    the custom report is a drilldown report of COPA, which can seen in tcode  FDI3.
    then how to add this new transaction to the menu path Easy assess->FI->AR->Information system->Reports->customers:items.
    Thanks,
    Swetha

    Hi Swetha,
    There is a special way to assign tcodes to such reports. Normally you create the report via report painter/writer. These reports are part of library and report group.
    Create the tcode in SE93. Then at the bottom of the screen you would find option to specify report group, report name, variant to lauch with. Don't have system to specify the exact way of specifying, if you need I can provide on monday. You can take some existing COPA standard report and check from SE93.
    --Ram

  • Rich Text Editor Option for Custom Metadata

    Hi,
    We have created custom profiles for our clients. In one such profile which was created for news updates, client wants to have rich text editor in case of certain custom metadata fields such as description similar to comments.
    As far i as i understand this can be achieved by creating region defintion which gives the option to create a web asset which can edited using site studio api. But is it possible to bring in rich text editor features for custom metadata fields in profiles ?
    Regards,
    Boopathy P

    I'd suggest looking into the profile rule's "Use custom include" option.
    You need to build the new control on your own, but you'll be able to plug it in via the "Use custom include" profile rule option. It's on the "Edit Rule Field" page.
    You can also override the template, but I believe that's a lil' too hardcoded for best practices. This way, you can create a ckeditor textarea and apply it to any metadata you wish (of course, it's up to you to make sure the datatypes match).
    Let us know if this helps!
    -ryan

  • Report Painter for all module

    Hi,
    Is report painter can be use by MM,PP,SD module ?
    in ECC6, we can use report painter for module FI, how about module MM,SD and PP ? 
    if it can use for those module too, how is the step?

    Hello Callia Raissa,
    Yes report writer can be used in logistics also. One of the way which I am aware of is described below.
    Flexible analyses allow you to can tailor the way in which key figures are combined and aggregated. This means that it is possible to both provide administrators with detailed information and management with aggregated information.
    Flexible analyses enable easy access to the Report Writer, a user-friendly tool with which you can create reports for various analyses. The Report Writer is integrated in other SAP applications, such as Extended General Ledger and Cost Center Accounting.
    Evaluation structures form the interface to the Report Writer. Evaluation structures consist of characteristics and key figures and are easy to construct.An evaluation structure with the same name exists for each information structure in the standard system.Even the self-defined information structures created in Customizing can be evaluated via the flexible analyses.
    Evaluations:You can create an evaluation on the basis of the evaluation structure.
    To define an evaluation, all you need to do is select the characteristics and key figures you require (pick-up technique).One of the especially useful features here is that you have the option of tailoring the layout of your report to suit your particular requirements. You can also define extra key figures for the reports, which are derived from existing key figures by means of calculation formulas. You can thereby multiply the key figures or divide one key figure by another.
    ============================================================
    In addition to the above you can also edit a report in logistics module with the help of a report writer. below mentioned is the process for it.
    It is now possible to edit your report data using the Report Writer. You can also change the layout of the report. The most important functions of the layout design are summarized below.
    Summation levels:In the report screen, you can use the menu sequence View ->Summation level to specify the number of summation used to calculate total values. All totals that do not lie within the specified interval will be hidden. A summation level corresponds to a hierarchical level (for example, material level). Summation level 1 is the lowest hierarchical level. Summation level 2 is the next level up, and so on. The individual values are on the summation level 0.
    The summation levels can be specified both universally (for the entire report) or locally (for specific blocks of rows). In this case, the local settings overwrite global values.
    Report views:If a report is displayed on the screen, the Report Writer will then set page breaks so that exactly one page fits into the current window. This view will be defined as the standard view. As the Report Writer always processes exactly one page, you can only use the page keys and page icons to page up and down; the scroll bars cannot be used.
    The page view can be determined via Settings-> Page view. The page breaks in the page view correspond to those defined in the report layout.
    Hide and show rows:The function Edit->Hide rows exclude certain preselected areas of your report from the display. You can undo this command with Edit ® Show rows.
    Expanding and collapsing report rows:View-> Hierarchy->Collapse allows you to hide the report rows of the sub-trees that are located underneath. View->Hierarchy ->Expand allows you to undo this command level by level.
    If you want to display all the report rows that were hidden by collapsing the hierarchy or restricting the summation levels, select, View->Hierarchy-> Expand all.
    View->Collapse all allows you to reduced every row block to the highest summation level.
    Texts and Annotations:You can create an annotation for your report.
    Select: Extras->Annotation.
    You branch into the text editor of the Report Writer.
    Via the menu sequence Settings->Texts, you can create and format a title page, the last page, as well as headers and footers using word processing functions.
    For example, you can store variables in the header for the author of the report, the date of the selection or the name of the person who last changed the report.
    Layout parameters:Using the menu sequence Settings->Layout you can specify the page format, display form, rows and columns of the report according to your needs and you can determine the settings for the graphics function. You can make these layout settings with Report->Save settings.
    Hope I had been able to help you to some extent. please assign points as reward.
    Rgds
    Manish

  • Latest recommendation for custom screen development?

    easy points here  - get 'em while their hot!  : )
    I'm trying to confirm my thoughts/assumptions on the development of custom screens.  We are installing the various pieces of NW04s and expect the majority of our users to access ECC transactions through the SAP Portal (using Web GUI for HTML).  Also, we will have a significant number of custom screen requirements; some may be enhancements to existing SAP delivered screens; others may be new screen development.
    What is SAP's latest recommendation for custom screen development?
    More specifically, what are various options and their advantages and disadvantages?
    For example:
    **Web Dynpro for Java
    + easy Portal integration
    - requires NW Dev Studio/Infrastructure
    **Web Dynpro for Java
    + Development tools within Workbench
    - more difficult to integrate in the Portal
    **Z transaction development with Screen Painter
    **HTML/JavaScript or similar
    etc....
    Thanks,
    Brian

    Brian,
    For me , I have only two choices
    1. Web Dynpro for Java
             Great front end IDE to work with, only issue could be the performance issue, while Java is trying to communicate with SAP ECC.
    2. Web Dynpro for ABAP
              Relatively new, however a good tool to work with. Performance improvement compared to Java Web Dynpro.
    I don't think you should have a concern of integrating this with Portal, as you have a separate iView for ABA Web Dynpro. The screen can be developed on the SAP ECC and then can be called from the Portal screen.
    You still have other options like developing normal dynpro - but that will not give a look and feel of the web interface. BSP / PCUI are the other choices but given the roadmap of SAP for UI, I would stick to Web Dynpro for ABAP / Java.
    Please let me know if you have any questions.
    Regards,
    Ravi

  • Webserver - setting permissions for Custom Sites

    Quick q on setting permissions for custom sites default. Default home for custom (non-default) web site is:
    /Library/Server/Web/Data/Sites/ 
    and whatever subdirectory you stipulate, e.g. MyServer - or whatever.
    Server sets this as owner:
    drwxrwxr-x   7 root  admin  238 Mar  8 15:34 CustomSitesDefault
    drwxrwxr-x  16 root  admin  544 Mar  8 15:38 Default
    For security, shouldn't the permissions and ownership be changed - to some webamin user WITHOUT root privs? Or will this break Lion Server? Thanks.

    Hello,
    One option would be to disable the automatic Project Site Sync in the User Sync Settings Page, create custom permission levels and groups on your SharePoint Project site template(s) to meet your requirements - make use of default SharePoint groups where
    possible, save the new template(s) and attach the new templates to the EPTs. Then develop a Project Server event handler that adds the users to the Project Site on the Publish event (or what ever event you like). The project owner one is simple - just add
    the project owner to the new SharePoint group, project members - just read the project team and add those users and the visitors just add a domain AD group (Domain Users for example) to that group.
    Default Project Server sync settings / site permissions can be seen here:
    http://technet.microsoft.com/en-gb/library/cc197668(v=office.14).aspx
    Paul
    Paul Mather | Twitter |
    http://pwmather.wordpress.com | CPS

  • Custom JButton with different states, any ideas??

    Hey, hope you guys can help.
    What I want to do is to implement a custom JButton class with different states for the button. The button should have a "pressed" state, where the user has pressed it, and if the action triggered by this pressing gets approved, it should get selected. It will then get a different color and border.
    I will have several buttons of this type in one panel, and they can all have different states at any time.
    I do not want to use JToggleButton, as it is not the user that decides if it should be selected or not, but the logic "behind" (the dataflow).
    So I want like a combination of JButton and JToggleButton...
    How do I do this, any ideas, please?

    I am not to sure if this is the idea you have in mind but...
    This is something i have used before it is a tool bar. the button could be managed by another event it is an event listener at the moment.
    As well as update only a specified button rather than all?
    public class ToolBar extends JToolBar {
        public ToolBar(EventListener el) {
            this.setName("ToolBar");
            String[] imageFiles = { "Left.gif", "Relationgif", "Reload.gif", "TrafficRed.gif", "Home.gif", "Print.gif", "Help.gif" };
            String[] toolbarLabels = { "Entity", "Relationship", "Reload", "Stop", "Home", "Print", "Help" };
            Insets margins = new Insets(3, 3, 3, 3);
            for(int counter = 0; counter < toolbarLabels.length; counter++) {
                ToolBarButton button = new ToolBarButton(imageFiles[counter]);
                button.setToolTipText(toolbarLabels[counter]);
                button.setMargin(margins);
                button.setName(imageFiles[counter]);
                button.setVerticalTextPosition(BOTTOM);
                button.setHorizontalTextPosition(CENTER);
              //  button.setMnemonic(KeyStroke.VK_CAPS_LOCK);
                button.addActionListener(el);
                add(button);
        public void setTextLabels(boolean showLabels) {
            Component c;
            int i = 0;
            while((c = getComponentAtIndex(i++)) != null) {
                ToolBarButton button = (ToolBarButton)c;
                if (showLabels)
                    button.setText(button.getToolTipText());
                else
                    button.setText("");
    //each button on the tool bar is an object of this class
    public class ToolBarButton extends JButton {
        private static final Insets margins = new Insets(3, 3, 3, 3);
        //user selected to view tool bar as image only (default)
        public ToolBarButton(Icon icon) {
            super(icon);
            setMargin(margins);
        //user selected to view tool bar as text only
        public ToolBarButton(String iconName) {
            this(new ImageIcon(iconName));
        //user selected to view tool bar as image and text
        public ToolBarButton(String iconImage, String iconName) {
            this(new ImageIcon(iconImage));
            setText(iconName);
    }

  • Overriding paint method hides my status bar icons

    Hi All,
                I use setStatus on MainScreen to add some image ButtonFields to my status bar.
                It shows up fine.
                But when I override paint method , it hides all the icons I added using setStatus.
               Any clue on this ? Thnx in advance.
              Here is my paint method:
    protected void paint(Graphics graphics) {
    super.paint(graphics);
    Bitmap image = ImageUtility.loadBitMap("header2.jpg");
    graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
    for(int i =0; i < 5; i++ ){
    fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
    protected void paint(Graphics graphics) {         
             Bitmap image = ImageUtility.loadBitMap("header2.jpg");
             graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0); for(int i =0; i < 5; i++ ){           fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
    Aranya

    Try Calling super.paint at the start or at the end ... it should do the trick

  • Overriding paint

    Is there any way to override paint only for one instance. For example, I have an image that I want to display, but after it is displayed, I want to be able to modify it. Problem is is that whenever a window is passed over the image, it updates and calls the paint method, which erases the modifications I made to the image. Is there anyway to only override the paint method only when I want to initially display the image?
    Also how would I scale down an ImageIcon so that its smaller?
    Thanks,
    Rick

    Don't modify the original image. Create a second image, paint your original image into the second image and then modify the second image. Each time paint is called, paint the second image to screen. The second one will always have your modifications, so it's safe to paint it multiple times. If you need to change the image agian, paint your original image over the first image again (to get an new clean copy) and modify it again. That's probably the most simplest thing you could do.
    You could also convert an image to a byte array, modify the byte array and use an ImageProducer (please look up that class) to paint your byte array to the screen. If you marked it as animated, you can update the byte array as often as you want and the changes will be viewable on the next call of paint.

Maybe you are looking for

  • TS3048 Bluetooth and USB ports aren't working can't connect mouse or keyboard.

    Bluetooth and USB ports aren't working, so am unable to use Imac past turning it on. There's no way to connect mouse or keyboard, it started right after a software update for Maverick (OS X bash Update 1.0 – OS X Mavericks), bluetooth symbol not show

  • IPhoto Gallery in iWeb

    When adding an iPhoto Gallery to iWeb, the images are slightly cropped, which make them unacceptable. Is there any way to resize the images shown in the little gallery viewer?

  • Multiple selction in the dropdownlistbox

    Hi All Can any one help me out that I am implementing the Drobdownlist for box for the multiple selections and to save the selected items in a database table. I have written code for the oninputprocessing. But I am getting error. unable to proceed fu

  • Find original calling program or transaction...

    Hello, I want to keep a User-exit from running when it is ran from LT23, but I want it to work from LT11. The problem is when the function to confirm the TO is executed in LT23, it does a call transaction to LT11. So, in the user-exit SY-TCODE has LT

  • Conference call CME 4.0

    Hi guys you know how many consumers in conference call can participate on an only conference with cme 4.0?