JScrollPane - Graphics - Headers

Hi !
I am using a JPanel with a JScrollPane. I have define 2 JPanel to set the columnheader and the rowheader. I paint in this 3 panels with Graphics. My problem is the following :
When I use the scroll bar (moving up and then coming back) there is tracks of the principal Panel in the header...
I don't know if it is a conceptual or a programming problem....
Some one have ever had this problem ?
Thanks and excuse me for my bad english...

Try this sample:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PrinC extends JFrame 
public PrinC()
     addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
          {     dispose();
               System.exit(0);}});
     JScrollPane jsp = new JScrollPane(new BPanel());
     jsp.setColumnHeaderView(new CPanel());
     jsp.setRowHeaderView(new RPanel());
     setContentPane(jsp);
       setBounds(1,1,700,500);
     setVisible(true);
public class BPanel extends JPanel
public BPanel()
     setBackground(Color.pink);
     setPreferredSize(new Dimension(1000,1000));
public void paintComponent(Graphics g)
     super.paintComponent(g);
     g.setColor(Color.black);
     g.drawRect(10,10,600,600);
public class CPanel extends JPanel
public CPanel()
     setBackground(Color.orange);
     setPreferredSize(new Dimension(1000,19));
public void paintComponent(Graphics g)
     super.paintComponent(g);
     g.setColor(Color.black);
     g.drawRect(3,3,getWidth()-7,getHeight()-7);
public class RPanel extends JPanel
public RPanel()
     setBackground(Color.green);
     setPreferredSize(new Dimension(19,1000));
public void paintComponent(Graphics g)
     super.paintComponent(g);
     g.setColor(Color.black);
     g.drawRect(3,3,getWidth()-7,getHeight()-7);
public static void main (String[] args) 
     new PrinC();
}Noah

Similar Messages

  • JScrollpane, Graphics, Image, jpg

    I'm try to load a jpg file in a jscrollpane and would like to draw on this jpg file any circels, lines or rectangle in a method . But it doesn't run, can anybody help me? thanks
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    public class JScroll extends JFrame {
       static Graphics g = null;
       static Image image;
       public static void main(String args[]) {
          JScroll wnd = new JScroll();
       public JScroll() {
          super("JScrollPane");
          setBackground(Color.lightGray);
          setSize(400, 400);
          //WindowListener
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                System.exit(0);
          image = getToolkit().getImage("neutral.jpg");
          g.drawImage(image, 20, 20, this);
          JScrollPane jScrollPane = new JScrollPane(g, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
          jScrollPane.setSize(300, 300);
          JPanel p1 = new JPanel();
          p1.setLayout(new BorderLayout());
          p1.add(jScrollPane, BorderLayout.CENTER);
          getContentPane().add(p1);
          paint(40,40);
          setVisible(true);
       public void paint( int y, int z) {
          g.drawImage(image, y, z, this);
    }

    Thank you for the answers! I have moved my drawMore() Methode, now I can use that Graphics object, but it doesn't draw the black Rectangels, only the red one. Why?
    The paintComponent() redraw all and load the jpg again on every repaint() call. How can I only update the Graphic object?
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    public class JScrollRx extends JFrame {
         AnImagePanel p1;
         public static void main(String args[]) {
              JScrollRx wnd = new JScrollRx();
         public JScrollRx() {
              super("JScrollPane");
              setBackground(Color.lightGray);
              setSize(400, 400);
              setLocation(200, 200);
              //WindowListener
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              JPanel p0 = new JPanel();
              p0.setLayout(new BorderLayout());
              p1 = new AnImagePanel(loadImage());
              p0.add(new JScrollPane(p1), BorderLayout.CENTER);
              p0.add(new JLabel("Title"), BorderLayout.NORTH);
              getContentPane().add(p0);
              setVisible(true);
              p1.drawMore(200,200);
         private Image loadImage() {
              Image image = getToolkit().getImage("neutral.jpg");
              // make sure image is loaded before we try to use it
              MediaTracker tracker = new MediaTracker(this);
              tracker.addImage(image, 0);
              try {
                   tracker.waitForID(0);
              } catch (InterruptedException ie) {
                   System.err.println("interrupt: " + ie.getMessage());
              return image;
         public class AnImagePanel extends JPanel {
              private Graphics g;
              private Graphics2D g2d;
              Image image = null;
              public AnImagePanel(Image im) {
                   image = im;
              public void paintComponent(Graphics g) {
                   super.paintComponent(g);
                   g2d = (Graphics2D) g;
                   int w = getWidth();
                   int h = getHeight();
                   int imageWidth = image.getWidth(this);
                   int imageHeight = image.getHeight(this);
                   int x = (w - imageWidth) / 2;
                   int y = (h - imageHeight) / 2;
                   g2d.drawImage(image, x, y, this);
                   g2d.setPaint(Color.red);
                   g2d.draw(new Rectangle2D.Double(w / 2 - 20, h / 2 - 20, 40, 40));
              public Dimension getPreferredSize() {
                   return new Dimension(image.getWidth(this), image.getHeight(this));
              public void drawMore(int x, int y){
                   g2d.setPaint(Color.black);
                   g2d.draw(new Rectangle2D.Double(x, y, 40, 40));
                   repaint();
    }

  • Database headers

    I must be missing something. I want a text field's content (a name) to appear on every record in a database file with 38 records on a particular layout. Normally, once you define the field and enter the name, the content only appears on that record. I suppose I could use a regular document header, but the fields I want are already defined and it would be redundant to re-enter them as text. I read there are database headers, different from the regular document headers, that will do this. I went to the layout menu, clicked 'insert part' and selected 'header'. It appeared on the document just fine. However, every time I dragged a text field into the header area from the body area, I never saw the field data in the browse mode, only the field label (page view or not). Moreover, once I dragged it into the header area, I could no longer enter data into the field in browse mode. What am I doing wrong? Can I get a text field to display on every record or is this not possible?
    PowerBook   Mac OS X (10.2.x)   Appleworks 6.2.9

    I must be missing something. I want a text field's
    content (a name) to appear on every record in a
    database file with 38 records on a particular layout.
    Normally, once you define the field and enter the
    name, the content only appears on that record.
    As it should. Fields (except Summary type fields) are containers for data in a record. When you enter the name into one record, it appears on that record. Records with ne data in that field will show an empty sapce. Just as when you fill in your name on a single credit card application form it appears on that individual form (and not on the other seven forms that arrived in the mail on the same day).
    I
    suppose I could use a regular document header, but
    the fields I want are already defined and it would
    be redundant to re-enter them as text. I read there
    are database headers, different from the regular
    document headers, that will do this.
    Headers and Footers (in any type of document) are used to show the same material (text and/or graphics) at the top or bottom of each Page of the document. Besides fixed text and graphics, Headers and Footers can contain information (eg. page numbers, current date, current time) calculated from the document itself or calculated from the computer's clock. In Database documents, Headers will appear only once (at the top of the document) unless Page view (in the Window menu) is checkmarked. In Page view, Headers will appear at the top of each Page. If your DB shows only a single Record to a Page, then the contents of Headers will appear with each record.
    I went to the
    layout menu, clicked 'insert part' and selected
    'header'. It appeared on the document just fine.
    However, every time I dragged a text field into the
    header area from the body area, I never saw the
    field data in the browse mode, only the field label
    (page view or not). Moreover, once I dragged it
    into the header area, I could no longer enter data
    into the field in browse mode. What am I doing
    wrong? Can I get a text field to display on every
    record or is this not possible?
    Data fields (except Summary type fields) will display, and are accessible for data entry, only in the Body part of a database layout. Any Text field placed in the Body part of a layout will display its content on every (visible) record. But there must be data in that field in that record for the data to display with a particular record.
    What to do:
    If the name is part of the data of each record, then you must place it into each record. (See below for a couple of ways to go about this.)
    If the name is simply a label and you do not need it as part of the data, then forget about the Text field, and place the name directly onto the Layout, using a Text Frame placed in the Body part if you want it to appear with every record, or into a Header part if you want it to appear at the top of each Page.
    Putting the name into multiple records:
    For a small number of records, go to List view (Layout > List), select the text in the already filled field and Copy (command-C).
    Now alternate pressing command-return (moves you to the same field in the next record) and command-V (pastes the copied text into the current field).
    To set a field to the same value in all of a large number of records it's easier to redefine the field twice.
    Press shift-command-D (Define fields)
    Click the 'Name' field in the list of fields.
    Change the Type to Calculation
    Click Modify (and OK the warning that this may cause you to lose data loss).
    Enter the name, enclosed in quotation marks (eg. "Louis") into the Formula box.
    Set Format result as: to Text.
    Click OK, then click Done.
    This will set the contents of the field to Louis in all records, and will not allow changing that value in an individual record. To change the value in ALL records, repeat the above (skipping the unnecessary steps), using the new value in the formula. If you want to be able to change individual records, change the field type back to Text as below.
    Press shift-command-D (Define fields)
    Click the 'Name' field in the list of fields.
    Change the Type to Text
    Click Modify (and OK the warning that this may cause you to lose data—Your data is compatible with this type of field).
    Click OK, then click Done.
    Regards,
    Barry

  • How do you save a document with a new name?

    OK  I decided to go with Pages as my document editor on my iPad 2.  It seems to be powerful enough to do the kind of text editing that I need to do. I need some tables, a few graphics, headers, footers, etc.  You know the basic stuff.  I am not going to try to write a dissertation or thesis on my iPad, but I need to be able to write memos and some work documents.
    I am having a hard time learning the Apple way of doing things I guess.  Must be too many years on a PC using Microsoft Office.
    But I need to know how to open an existing document, make some changes and then save it with a new name so I don't overwrite my original document? Just a basic "Save As" function.
    Is this possible?  Or has Apple devised a way that I am just not seeing yet?

    Hi
    Someone maybe can tell you another/the right way of doing this but I do it this way. In pages press and hold the document until it wiggles and then select it by touching it so the orange border appears around it, then press the folder button at the top, the one with the + sign on it and this will create an exact copy on the documents screen. Then just open and edit that one thus preserving the content of the original.
    There may well be a better way, but hope this helps.
    Regards
    Matt.

  • Can't Make a Hot Spot!

    Hi there,
    I'm using Dreamweaver 8 to make a new website.
    For some reason I can't make a hot spot. I've only made one
    website before and just checked on those pages and was able to
    click on the hot spot tools and draw a hot spot.
    Then I go to my new website and click on an image and then
    click on the hot spot tools and try to draw a hot spot on the
    picture- what happens is the picture tries to "drag and drop". That
    is the command that dreamweaver says is happening when I try to try
    to draw the hot spot. ???????
    What went wrong???
    Here is the Html code behind the photo I'm trying to draw the
    hot spot on:
    <tr>
    <td colspan="3"><img
    src="graphics-headers/skyfooter.jpg" alt="Sky Footer" width="800"
    height="45" /></td>
    </tr>
    <tr>
    Any help at all would be greatly appreciated as I don't know
    what I did in the code that's preventing me from drawing them.
    As I'm just starting making all my pages off my template-
    found another problem:
    Template won't update all the changes. For instance: Made a
    change to the copyright footer - capitilized first letter- this
    change won't update. Made changes to the page properties with a
    link color change, also changed editable region- these changes
    updated.
    Weird!!! So the pages made from the template will be updated
    only part ways from the original Template!
    What did I do wrong? My first website went very well.
    www.zoodoc-stories.com

    When you create a polygonal hotspot, you don't click and
    drag. You click at
    the vertex of each side. Is that what you are doing?
    Here are seven common mistakes that will result in changes
    not propagating
    from Template to child -
    1. Rename the Templates folder
    2. Move the Templates folder to some other folder level in
    the site other
    than root level
    3. Move a template out of the Templates folder.
    4. Make changes to an editable region of the template (which
    will not
    propagate)
    5. Assume that template changes can be uploaded to the server
    without also
    uploading the changed local files
    6. Corrupt the site cache so that the link between template
    and child page
    is broken
    7, Improperly create your child pages by simply opening the
    template,
    adding page specific content, and resaving the template with
    an *.html
    extension rather that its *.dwt extension. (you can tell if
    this is the case
    by looking at the code on a child page - if you see anything
    like <!--
    TemplateBeginEditable... then this is what you have done)
    The 6th problem can be repaired by using SITE | Advanced >
    Recreate Site
    Cache, and the six others can be fixed by just not doing
    them. Have you
    done any of those?
    Or, read this -
    http://www.adobe.com/go/dd83ba8b
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    ==================
    "sculpturedolls" <[email protected]> wrote
    in message
    news:[email protected]...
    > Hi there,
    >
    > I'm using Dreamweaver 8 to make a new website.
    >
    > For some reason I can't make a hot spot. I've only made
    one website before
    > and
    > just checked on those pages and was able to click on the
    hot spot tools
    > and
    > draw a hot spot.
    >
    > Then I go to my new website and click on an image and
    then click on the
    > hot
    > spot tools and try to draw a hot spot on the picture-
    what happens is the
    > picture tries to "drag and drop". That is the command
    that dreamweaver
    > says is
    > happening when I try to try to draw the hot spot.
    >
    > What went wrong???
    >
    > Here is the Html code behind the photo I'm trying to
    draw the hot spot on:
    >
    > <tr>
    > <td colspan="3"><img
    src="graphics-headers/skyfooter.jpg" alt="Sky Footer"
    > width="800" height="45" /></td>
    > </tr>
    > <tr>
    >
    > Any help at all would be greatly appreciated as I don't
    know what I did in
    > the
    > code that's preventing me from drawing them.
    >
    > ----------------------------
    >
    > As I'm just starting making all my pages off my
    template- found another
    > problem:
    >
    > Template won't update all the changes. For instance:
    Made a change to the
    > copyright footer - capitilized first letter- this change
    won't update.
    > Made
    > changes to the page properties with a link color change,
    also changed
    > editable
    > region- these changes updated.
    >
    > Weird!!! So the pages made from the template will be
    updated only part
    > ways
    > from the original Template!
    >
    > What did I do wrong? My first website went very well.
    > www.zoodoc-stories.com
    >
    >

  • Plain Generic Empty Template Available

    Friends,
    For those of you who desire a completely empty and generic template from which to begin your authoring adventures, desire no longer. I've created one and you may have it for free. Go to this web site and download it:
    http://www.box.com/files#/files/0/f/0/
    It's called, Plain Generic Empty Template. All content is gone, no text, graphics, headers or footers. Even the styles are reduced to the bare minimum. It's as blank a canvas as can be created.
    The world awaits you next great publication. Go forth and create.
    - Fabe

    Same...
    Log in to your account:
    Email/Username:
    Password:
    Remember this login information?
    Log In Forgot password?
    Don't have an account?
    Register for Box or learn more
    Sign in with your Google account 
    Please log in to access Box
    Use the login form to the right to access your files. Don't have a Box account yet? Sign up for free.

  • Jtable (in Jscrollpane) headers missing????

    hello,
    i need to show the results of a resultset in a jtable. i am putting the table in a jscrollpane... but still the headers dont show up. i have tested the array "headers" (Object[] headers) and is full with the correct data.
    what is missing??
    JTable table = new JTable(data,headers);
            table.setSize(jScrollPaneResultados.getSize());
            jScrollPaneResultados.add(table);
            table.setFillsViewportHeight(true);thank�s

    The scrollPane.add(...) method doesn't work the way you expect it to.
    You can use the setViewportView(...) method, or you can read the JTable API for an example of adding a table to the scrollpane when the scrollpane is created.

  • Remove Column Headers from a JTable in a JScrollPane

    Hi,
    I'm just wondering how to remove the column headers from a JTable in a JScrollPane.

    Here are two ways to do it, with different visual outcomes...
    import javax.swing.*;
    public class Test {
        public static void main(String[] args) {
            Object[][] rowData = {{"A", "B"}, {"C", "D"}};
            Object[] columnNames = {"col 1", "col 2"};
            JTable table1 = new JTable(rowData, columnNames);
            table1.getTableHeader().setVisible(false);
            JScrollPane sp1 = new JScrollPane(table1);
            JTable table2 = new JTable(rowData, columnNames);
            final JScrollPane sp2 = new JScrollPane(table2);
            JPanel contentPane = new JPanel();
            contentPane.add(sp1);
            contentPane.add(sp2);
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    sp2.setColumnHeader(null);
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • Custom column headers for JTable in JScrollPane

    I want a heirachical header structure on a scrolled JTable. I've successfully generated a second JTableHeader which moves it's tabs with the normal header. If I add the secondary JTableHeader into the container above the whole scroll pane it's does almost what I want, but it's not quite correctly aligned.
    What I want to do is to put both the automaticaly generated JTableHeader and my extra one into the JScrollPane's column header area.
    I wrapped the two headers together into a vertical Box and tried calling the setColumnHeaderView() on the scrollpane, and then creating a JViewport and using setColumnHeader(). Niether seems to have any effect. The basic table header obstinately remains unaltered.
    There seems to be some special processing going on when JTable and JScrollPane get together, but I can't understand how replacing the column header viewport can be ineffective.

    Thanks. I think I've just cracked it more thoroughly, though. [I found this bug report|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5032464]. This has guided me to a work-around that seems stable so far. I'm using an extended JTable class anyway (mostly to do with table header width behaviour). I've added a field to my own table class and the following override:
    The trick is to work out where the dirty deed is done, having search all the scroll pane related classes for special casing JTable.
    public class STable extends JTable {
        @Override
        protected void configureEnclosingScrollPane() {
            if (secondaryHeader == null) {
                super.configureEnclosingScrollPane();
            } else {
                Container p = getParent();
                if (p instanceof JViewport) {
                    Container gp = p.getParent();
                    if (gp instanceof JScrollPane) {
                        JScrollPane scrollPane = (JScrollPane) gp;
                        // Make certain we are the viewPort's view and not, for
                        // example, the rowHeaderView of the scrollPane -
                        // an implementor of fixed columns might do this.
                        JViewport viewport = scrollPane.getViewport();
                        if (viewport == null || viewport.getView() != this) {
                            return;
                        JPanel hdrs = new JPanel();
                        hdrs.setLayout(new BorderLayout());
                        hdrs.add(secondaryHeader.getHeader(), BorderLayout.NORTH);
                        hdrs.add(getTableHeader(), BorderLayout.SOUTH);
                        scrollPane.setColumnHeaderView(hdrs);
                        //  scrollPane.getViewport().setBackingStoreEnabled(true);
                        Border border = scrollPane.getBorder();
                        if (border == null || border instanceof UIResource) {
                            Border scrollPaneBorder =
                                    UIManager.getBorder("Table.scrollPaneBorder");
                            if (scrollPaneBorder != null) {
                                scrollPane.setBorder(scrollPaneBorder);
        }I'm hopeful that will prevent the column header view from being overwritten by later layout operations.

  • JScrollPane for pane and JTabbedPane and Graphics

    Sort of have two topics with same problem.
    I have a set of Panels. I get a stream of jpegs which I have to decode to the fly.
    There are seperate video feeds in the stream and I have to read jpeg data to see which image goes where.
    I was trying to see if I could have a larger image in smaller area. this is why I tried to draw to panel inside a scrollpane.
    One: I tried to put a JPanel into JScrollPane and tried to draw an image to panel with Graphics g. No Scrollability and even when scroll bars are made to stay on permanently, they get overwritten.
    g=panel.getGraphics();
    if(isVisible()) g.drawImage(image,0,0,320,240,Color.BLUE,null);
    two: When putting the pane with the drawn images into toplevel pane into JtabbedPane. I can turn off the drawing of images with a check to isVisible(). When they are not in top level pane isVisible() does not work and the images get drawn over the other tabbed panes.
    BTW everything is swing. JPanel,JScrollPane,JTabbedPane.

    No Scrollability and even when scroll bars are made to stay on permanently,First of all the scrollbars will only appear when the preferred size of the panel is greater than the size of the scrollpane.
    A think a better design for updating the image is to extend JPanel and create a setImage method. The set image method would set the preferred size of the panel and then invoke repaint() on the panel. You would then also need to override the paintComponent(..) method to do the actual drawing of the image. Then when the image changes you just use the new setImage(..) method.

  • JScrollPane focus lost - graphics messed up.

    Hello,
    Before I start explaining my problem I would like to apologize for
    the fact that I couldn't find a solution for my problem on the forum although there are many topics covering problems with this
    widget .
    I have a JEditorPane inside a JScrollPane, the editor pane contains
    som text, usually multiple lines so that the scrollbars are activated.
    But when I transfer the focus onto another component, for example,
    a button the bottom of the scrollpane gets messed up (it's like the scrollpane isn't repainted) and I can't scroll anymore.
    Anyone knows how this can be solved ?
    Thanks in advance.

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It’s unlikely to solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
    The purpose of the test is to determine whether the problem is caused by third-party software that loads automatically at startup or login. 
    Disconnect all wired peripherals except those needed for the test, and remove all aftermarket expansion cards. Boot in safe mode* and log in to the account with the problem. The instructions provided by Apple are as follows:
    Shut down your computer, wait 30 seconds, and then hold down the shift key while pressing the power button.
    When you see the gray Apple logo, release the shift key.
    If you are prompted to log in, type your password, and then hold down the shift key again as you click  Log in.
    *Note: If FileVault is enabled under OS X 10.7 or later, or if a firmware password is set, or if the boot volume is a software RAID, you can’t boot in safe mode. Safe mode is much slower to boot and run than normal, and some things won’t work at all, including wireless networking on certain Macs. The next normal boot may also be somewhat slow.
    The login screen appears even if you usually log in automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin. Test while in safe mode. Sound won't work, but there should be no graphics artifacts. After testing, reboot as usual (i.e., not in safe mode) and verify that you still have the problem. Post the results of the test.

  • JScrollPane doesn't work with Graphics

    Does JScrollPane work easily with Graphics?
    The GUI I am trying to create will have an indeterminate number of coloured blocks, so I need scroll bars to allow the full number of blocks to be seen. My test code, with just a few of blocks, is below.
    I have a class which contains the paintComponent(Graphics) method, and an object of that is put inside a JScrollPane and the whole lot is added to a JFrame. I have tried playing around with various sizes, and with various combinations of JFrame and JPanel, but the scrollbars don't allow me to scroll to see the full blocks. In the JScrollPane constructor I have declared them to be always visible so that I can see them, but they don't do anything.
    Should my ColorPanel class implement Scrollable? I'd like to avoid that, as it seems unnecessarily complicated. Has anybody got any ideas? Many thanks.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics.*;
    import javax.swing.*;
    class ScrollTest extends JFrame
    {     private Toolkit tk = Toolkit.getDefaultToolkit();
         private Dimension d = tk.getScreenSize();
         int screenHeight = d.height;
         int screenWidth = d.width;
         public ScrollTest()
         {     super("Test");
              addWindowListener(new WindowAdapter()
                   {     public void windowClosing(WindowEvent e)
                        {     System.exit(0);
              setSize(screenWidth, screenHeight);
              Container contentPane = getContentPane();
              ColorPanel cp = new ColorPanel();
              cp.setPreferredSize(new Dimension(400, 400));
              JScrollPane scroller = new JScrollPane(cp,
                        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
              JViewport vp = scroller.getViewport();
              vp.setViewSize(new Dimension(350, 350));
              vp.add(cp);
              contentPane.add(scroller, "Center");
         public static void main(String[] args)
         {     ScrollTest st = new ScrollTest();
              st.pack();
              st.show();
    class ColorPanel extends JPanel
         public void paintComponent(Graphics g)
         {     super.paintComponent(g);
              g.setColor(Color.red);
              g.fillRect(50, 250, 50, 500);
              g.fillRect(250, 50, 500, 50);
              g.setColor(Color.green);
              g.fillRect(50, 750, 50, 50);
              g.fillRect(750, 50, 50, 50);
    }

    I am having an almost identical problem . I am also building panels with custom graphics of indeterminate numbers of colored blocks, and so need scrollbars - or SOME way of being able to view everything, but have not been able to get it to work. i tried setting the preferred size of the panel, and setting the sizes of the scrollpane and its container as well..
    this problem has been plaguing me for a while - enclosing a panel with custon graphics in a scrollpane and getting the scrollbars to show up and function correctly. any help will be greatly appreciated!!

  • Painting graphics in a JScrollPane

    Hi All,
    I'm writing a graph-drawing application. In my GUI I have a large area for drawing the graphs; this area (drawPanel) is a custom subclass of JPanel.
    So that the graphics persist when the screen is refreshed, I've overridden the paintComponent() method in my drawPanel and put all the drawing code in there (consisting of g.drawLine, g.drawString, etc). I can then call drawPanel.update() when I update the graph, to make sure the graphics appear. Works fine.
    However, if I try to put the drawPanel into a JScrollPane, for example like this:
    JScrollPane scroller = new JScrollPane(drawPanel);
    and add that to the GUI, the program works fine and the scrollbars appear, but nothing at all is drawn in the drawing area.
    I bet this is something really simple, but can anyone point me in the right direction? I've searched the forums, but to no avail. I would post some code, but I'm not sure of the relevant area.
    Many thanks,
    Mike.

    Thanks.
    Although this example works, it's unfortunately too dissimilar to my own program to work out the difference. I'm doing more or less the same thing on a bigger scale - drawing in the paintComponent() method of the JPanel object, and then adding a JScrollPane with the JPanel as the client. I've tried setting preferred size for both (as that was the only obvious difference), but no luck.
    I always thought that the viewport of a scroll pane was supposed to be "transparent" in the sense that the client of the JScrollPane "showed through", and indeed it seems to work for small examples, but I don't know what I'm doing wrong.
    Any other ideas?
    Thanks in advance,
    Mike.

  • Adding java 2d graphices to a JScrollPane

    How can I use the graphics 2d in a Java Applet JScrollPane?
    here is me code. I have a a ImageOps.java file that does a bunch of Java 2d, but I need it to fit into my applet's JScrollPane. Can anyone help me?
    package louis.tutorial;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JApplet;
    import java.awt.Dimension;
    import javax.swing.JInternalFrame;
    import javax.swing.JButton;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Rectangle;
    import java.awt.Point;
    import javax.swing.JComboBox;
    import javax.swing.JList;
    import javax.swing.JTextArea;
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JLabel;
    import javax.swing.JMenuItem;
    import javax.swing.ImageIcon;
    import javax.swing.KeyStroke;
    import java.awt.FileDialog;
    import java.awt.Toolkit;
    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import javax.swing.JScrollPane;
    * Place class description here
    * @version 1.0
    * @author llakser
    public class ImageApplet extends JApplet {
    private JPanel jContentPane = null;
    private JPanel jpLower = null;
    private JButton JBFirst = null;
    private JButton JBPrevious = null;
    private JButton JBNext = null;
    private JButton JBLast = null;
    private JButton JBZoonIn = null;
    private JButton JBZoomOut = null;
    private JButton JBAnimate = null;
    private JButton JBPause = null;
    private JButton JBAutoRepeat = null;
    private JComboBox jcbFrameDelay = null;
    private BufferedImage image; // the rasterized image
    private Image[] numbers = new Image[10];
    private Thread animate;
    private MediaTracker tracker;
    private int frame = 0;
    private ImageIcon[] icon = null;
    private JFrame f = new JFrame("ImageOps");
    * This is the xxx default constructor
    public ImageApplet() {
         super();
    * This method initializes this
    * @return void
    public void init() {
         this.setSize(600, 600);
         this.setContentPane(getJContentPane());
         this.setName("");
         this.setMinimumSize(new Dimension(600, 600));
         this.setPreferredSize(new Dimension(600, 600));
    // f.addWindowListener(new WindowAdapter() {
    // public void windowClosing(WindowEvent e) {System.exit(0);}
    ImageOps applet = new ImageOps();
    //getJContentPane().add("Center", f);
    getJContentPane().add("Center", applet);
    //applet.add(f);
    applet.init();
    f.pack();
    f.setSize(new Dimension(550,550));
    f.setVisible(true);
    public void load() {
         tracker = new MediaTracker(this);
         for (int i = 1; i < 10; i++) {
         numbers[i] = getImage (getCodeBase (), i+".jpeg");//getImage(getDocumentBase(), "Res/" + i + ".gif");
         //Image img;
         //ico[i] = new ImageIcon(getCodeBase (), i+".jpeg");
         /* if (fInBrowser)
         img = getImage (getCodeBase (), "1.jpeg");
         else
         img = Toolkit.getDefaultToolkit ().getImage (
         "1.jpeg");*/
         tracker.addImage(numbers, i);
         try {
         tracker.waitForAll();
         } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         int i = 1;
         // for (int i = 1; i < 10; i++)
              icon[0] = new ImageIcon ("C:/images2/1.jpeg");//getImage(getDocumentBase(), "Res/" + i + ".gif");
              //icon[0] = new ImageIcon("C:/images2/1.jpg");
              //System.out.println("Made it after icon " + ico.toString());
              //ImageIcon ii = new ImageIcon("1.jpg");
              //jLabel = new JLabel(icon);
              //jLabel.setText("JLabel");
              //jLabel.setIcon(icon[0]);
         Graphics g = null;
              //g.drawImage(numbers[1],0,0,1500,1400,jScrollPane);
         //ImageIcon ii = new ImageIcon(numbers[2]);
         //this.jScrollPane.add(new JLabel(ii));// = new JScrollPane(new JLabel(ii));
         // System.out.println("Gee I made it..");
              //scroll.paintComponents(g);
              //paintComponents(g);
    public void paintComponents(Graphics g) {
    super.paintComponents(g);
    // Retrieve the graphics context; this object is used to paint shapes
    Graphics2D g2d = (Graphics2D)g;
    // Draw an oval that fills the window
    int width = this.getBounds().width;
    int height = this.getBounds().height;
    Icon icon = new ImageIcon("C:/images2/1.jpg");
    //jLabel.setIcon(icon);
    //g2d.drawImage(0,0,1500,1400);
    g2d.drawOval(10,20, 300,400);
    // and Draw a diagonal line that fills MyPanel
    g2d.drawLine(0,0,width,height);
    * This method initializes jContentPane
    * @return javax.swing.JPanel
    private JPanel getJContentPane() {
         if (jContentPane == null) {
         jContentPane = new JPanel();
         jContentPane.setLayout(new BorderLayout());
         jContentPane.setPreferredSize(new Dimension(768, 576));
         jContentPane.setMinimumSize(new Dimension(768, 576));
         jContentPane.setName("");
         jContentPane.add(getJpLower(), BorderLayout.SOUTH);
         return jContentPane;
    * This method initializes jpLower     
    * @return javax.swing.JPanel     
    private JPanel getJpLower() {
    if (jpLower == null) {
         try {
         jpLower = new JPanel();
         jpLower.setLayout(null);
         jpLower.setPreferredSize(new Dimension(500, 100));
         jpLower.setMinimumSize(new Dimension(500, 160));
         jpLower.add(getJBFirst(), null);
         jpLower.add(getJBPrevious(), null);
         jpLower.add(getJBNext(), null);
         jpLower.add(getJBLast(), null);
         jpLower.add(getJBZoonIn(), null);
         jpLower.add(getJBZoomOut(), null);
         jpLower.add(getJBAnimate(), null);
         jpLower.add(getJBPause(), null);
         jpLower.add(getJBAutoRepeat(), null);
         jpLower.add(getJcbFrameDelay(), null);
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return jpLower;
    * This method initializes JBFirst     
    * @return javax.swing.JButton     
    private JButton getJBFirst() {
    if (JBFirst == null) {
         try {
         JBFirst = new JButton();
         JBFirst.setText("First");
         JBFirst.setLocation(new Point(7, 7));
         JBFirst.setSize(new Dimension(59, 26));
         JBFirst.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent e) {
              System.out.println("First Button Clicked()");
         load();
              // TODO Auto-generated Event stub actionPerformed()
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBFirst;
    * This method initializes JBPrevious     
    * @return javax.swing.JButton     
    private JButton getJBPrevious() {
    if (JBPrevious == null) {
         try {
         JBPrevious = new JButton();
         JBPrevious.setText("Previous");
         JBPrevious.setLocation(new Point(69, 7));
         JBPrevious.setSize(new Dimension(94, 26));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBPrevious;
    * This method initializes JBNext     
    * @return javax.swing.JButton     
    private JButton getJBNext() {
    if (JBNext == null) {
         try {
         JBNext = new JButton();
         JBNext.setText("Next");
         JBNext.setLocation(new Point(166, 7));
         JBNext.setSize(new Dimension(66, 26));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBNext;
    * This method initializes JBLast     
    * @return javax.swing.JButton     
    private JButton getJBLast() {
    if (JBLast == null) {
         try {
         JBLast = new JButton();
         JBLast.setText("Last");
         JBLast.setLocation(new Point(235, 7));
         JBLast.setSize(new Dimension(59, 26));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBLast;
    * This method initializes JBZoonIn     
    * @return javax.swing.JButton     
    private JButton getJBZoonIn() {
    if (JBZoonIn == null) {
         try {
         JBZoonIn = new JButton();
         JBZoonIn.setText("Zoom In");
         JBZoonIn.setLocation(new Point(408, 7));
         JBZoonIn.setPreferredSize(new Dimension(90, 26));
         JBZoonIn.setSize(new Dimension(90, 26));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBZoonIn;
    * This method initializes JBZoomOut     
    * @return javax.swing.JButton     
    private JButton getJBZoomOut() {
    if (JBZoomOut == null) {
         try {
         JBZoomOut = new JButton();
         JBZoomOut.setBounds(new Rectangle(503, 7, 90, 26));
         JBZoomOut.setPreferredSize(new Dimension(90, 26));
         JBZoomOut.setText("Zoom Out");
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBZoomOut;
    * This method initializes JBAnimate     
    * @return javax.swing.JButton     
    private JButton getJBAnimate() {
    if (JBAnimate == null) {
         try {
         JBAnimate = new JButton();
         JBAnimate.setText("Animate");
         JBAnimate.setSize(new Dimension(90, 26));
         JBAnimate.setPreferredSize(new Dimension(90, 26));
         JBAnimate.setLocation(new Point(408, 36));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBAnimate;
    * This method initializes JBPause     
    * @return javax.swing.JButton     
    private JButton getJBPause() {
    if (JBPause == null) {
         try {
         JBPause = new JButton();
         JBPause.setPreferredSize(new Dimension(90, 26));
         JBPause.setLocation(new Point(503, 36));
         JBPause.setSize(new Dimension(90, 26));
         JBPause.setText("Pause");
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBPause;
    * This method initializes JBAutoRepeat     
    * @return javax.swing.JButton     
    private JButton getJBAutoRepeat() {
    if (JBAutoRepeat == null) {
         try {
         JBAutoRepeat = new JButton();
         JBAutoRepeat.setBounds(new Rectangle(446, 65, 106, 26));
         JBAutoRepeat.setText("Auto Repeat");
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return JBAutoRepeat;
    * This method initializes jcbFrameDelay     
    * @return javax.swing.JComboBox     
    private JComboBox getJcbFrameDelay() {
    if (jcbFrameDelay == null) {
         try {
         jcbFrameDelay = new JComboBox();
         jcbFrameDelay.setSize(new Dimension(156, 25));
         jcbFrameDelay.setName("Frame Delay");
         jcbFrameDelay.setLocation(new Point(7, 35));
         } catch (java.lang.Throwable e) {
         // TODO: Something
    return jcbFrameDelay;
    } // @jve:decl-index=0:visual-constraint="10,10"

    I think you've got it:
    public class ImageComponent extends JPanel { //or JComponent
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            //do your rendering here
    }The fact that ImageComponent sits inside JScrollPane is accidental, right?
    It should be able to function on its own, without a JScrollPane present, just as JTextArea can.
    So I'm not sure what your question is. Can you post a small (<1 page)
    example program demonstrating your problem.
    For example, here are a very few lines demonstrating what I am trying to say:
    import java.awt.*;
    import javax.swing.*;
    class CustomExample extends JPanel {
        public CustomExample() {
            setPreferredSize(new Dimension(800,800));
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillOval(0, 0, getWidth(), getHeight());
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    JFrame f = new JFrame("CustomExample");
                    f.getContentPane().add(new JScrollPane(new CustomExample()));
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setSize(400,400);
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }Now you post your problem in a program that is just as short.

  • Odd graphic in mail headers

    My sister is seeing an odd image when examining the headers of a received email. When her mouse rolls over an area (I guess where address book info links are) then this huge circle appears. Its a powder blue circle with a small, white, downward pointed triangle at the 3 o'clock position. Also the different header fields become widely spaces vertically...
    Any ideas?

    I don't fully understand what are you talking about, but I have the feeling that your sister has been playing too much with fonts lately:
    Font Management in Mac OS X Tiger and Panther

Maybe you are looking for

  • HWF UI work list not loading in BPM worklist page (on clustered env)

    hello - Please reply directly as I am not on this alias. My SOA project (BPEL+HWF) is successfully deployed on the clustered env successfully. My BPEL process runs fine. But my HWF_UI taskflow fails to load on the browser (IE and Firefox, same error)

  • HD erasure to prevent dropped frames.

    I know I am not alone on this dropped frames thing. I have received a lot of great info and areas to consider. Here is my little situation. I had a HD replaced on my PB G4. I have 2 GB of memmory... I reinstalled all my apps... but my new project is

  • IPhoto 08 slow to FINISH loading in Leopard

    Installed Leopard clean, copied my iphoto library to DVD before the upgrade, then back. All updates installed. I start iPhoto, it loads in less than 5 seconds whether in event or photo view. I see pictures immediately. After that, iPhoto locks up for

  • Can't figure out how to parse JSON list of lists into a table

    JSON returned (from google analytics) has an entry like: "rows": [         "(none)",         "148"         "email",         "2"         "organic",         "183"         "referral",         "140" In Power Query the column heading is "List" and each ro

  • Podcast posting problems - multiple sites

    I have 2 website, one for family and one for dj mixes that I upload. I have created both and have been able to publish both, but when I hit subscribe to one of the mixes it says invalid. When I look, it is using the family website as the link and of