Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:19:13+00:00 2026-05-27T23:19:13+00:00

I have a JFrame containing a JScrollPane containing a JPanel (with only graphics) and

  • 0

I have a JFrame containing a JScrollPane containing a JPanel (with only graphics) and a JTabbedPanel.
On the JScrollPane, here is the paint method :

public void paint(Graphics g) {
    g2d = (Graphics2D) g;
    super.paint(g2d);
    if (geneLines.size() != 0 ) {
        g2d.drawString("5'",linePadding, topMargin+4); 
        for(int i=0; i<geneLines.size();i++) {
            drawDNALine(i);
            drawLineLabels(i);
            for(int j=0; j<geneLines.get(i).size();j++) {
                Gene gene = geneLines.get(i).get(j);
                drawGene(i, gene.getMid(), initColor(gene.getFunctionnality()), gene.getFunctionnality() >= 2 ? (float)0.5:(float)1, gene);
            }
        }
        g2d.drawString("3'", 510, (geneLines.size()-1)*linePadding+topMargin+4);
    }
    setPreferredSize(new Dimension(getWidth()-20,(geneLines.size()-1)*linePadding+topMargin-GlobalVars.squareY/2+topMargin));
    revalidate();
}

And here is the drawGene method :

private void drawGene(int l, int mid, Color c, float w, Gene g) {
    int posX = Math.round(((((float)mid/(float)length)-l)*400+100-GlobalVars.squareX/2));
    int posY = l*linePadding+topMargin-GlobalVars.squareY/2;
    g2d.setColor(c);
    g2d.fillRect(posX,posY,(int) (GlobalVars.squareX*w),GlobalVars.squareY);
    g2d.setColor(Color.black);
    g2d.drawRect(posX,posY,(int) (GlobalVars.squareX*w),GlobalVars.squareY);
    AffineTransform atBefore = g2d.getTransform(); 
    Rectangle2D r = g2d.getFontMetrics().getStringBounds(g.getName(), g2d);
    AffineTransform trans = AffineTransform.getTranslateInstance(posX+5, posY-40);
    trans.concatenate(AffineTransform.getRotateInstance(-Math.PI/2));
    trans.concatenate(AffineTransform.getTranslateInstance(-r.getCenterX(), -r.getCenterY()));
    g2d.setTransform(trans);
    g2d.drawString(g.getName(), 0, 0);
    g2d.setTransform(atBefore);
}

So, here is my goal and my problem. I have to make a picture showing the gene distribution on lines. Each gene is a square (or rectangle), and I have to write the name of the gene vertically above the square/rectangle. I also write some information like the length of the line, etc.
By this way, when I resize the frame, all is ok (well repainted), same if I reduce the frame. But, if the number of line is too large, a scrollbar appear (that’s why I use JScrollPane). And when I move this scrollbar, all vertical text disappear. I don’t understand why, because the text drawing is in the same method than the rectangle drawing (and they are well repainted while scrolling).

I have no idea about where this is coming from… I’ve tried some manual repaints, but I have no result.

I’m using Eclipse on Windows, and I’m available if you need more information, of course. Thank’s very much for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:19:14+00:00Added an answer on May 27, 2026 at 11:19 pm

    You have to use a simple JScrollPane with a JPanel doing the drawing and resizing. If you give the JPanel a border and other background colour, you will see whether everything behaves as desired.


    An other answer

    Painting should be fast, and not call setPreferredSize and revalidate.
    For the moment you could do:

    if (...size changes) {
        SwingUtilities.invokeLater(new Runnable()) {
            @Overriden
            public void run() {
                ...setPreferredSize and revalidate
            }
        }
    }
    

    Then this?

    final double NINETY_DEGREES = Math.PI/2;
    Rectangle2D r = g2d.getFontMetrics().getStringBounds(g.getName(), g2d);
    g2d.translate(posX+5, posY-40);
    g2d.rotate(-NINETY_DEGREES);
    g2d.translate(-r.getCenterX(), -r.getCenterY());
    g2d.drawString(g.getName(), 0, 0);
    g2d.translate(r.getCenterX(), r.getCenterY());
    g2d.rotate(NINETY_DEGREES);
    g2d.translate(-(posX+5), -(posY-40));
    

    There is no difference, but maybe the painting takes long and then this just might help.


    Last resort

    Make a JPanel with a BoxLayout in the vertical direction. Add for every line a JPanel that does the drawing for just that line. (Or a JList)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame containing three JPanel. The first JPanel contains a JTextField and
I have JFrame , and I added my JPanel class with paintComponent() method. For
I am using swing to create my GUI. J have a JFrame containing one
I have a JFrame that contains a display JPanel with JTextField and a control
Java Newbie here. I have a JFrame that I added to my netbeans project,
I have a JFrame with an associated JPanel which fill the screen, both having
I have created a JFrame subclass containing 9 JPanels using a GridLayout (3x3). I'm
I have a swing app. with a jframe with lots of internal frames containing
I have a JFrame and JPanel full of Jsomethings with an actionlistener. When the
So I have a JFrame in which inside of it I have a JScrollPane

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.