-1

im working on a project where i am drawing an image on a JPanel and then putting that panel inside a JScrollPane. The panel itself will vary in size sometimes being smaller than the screen, sometimes larger. When it is larger the scrollpane doesn't seem to have any scroll bars appearing. Im sure im messing something up that is simple but i can't seem to figure it out.

Any help would be greatly appreaciated.

public void createGI(theTicket[] ticketArray){
    JFrame ticketReportFrame = new JFrame();
    ticketReportFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JPanel ticketReport = new drawTurnoverReport(ticketArray , notes, tapes, turnover);
    ticketReport.setBackground(Color.WHITE);
    ticketReport.setLayout(new BorderLayout());
    JScrollPane ticketReportHolder = new JScrollPane(ticketReport);
    ticketReportFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    ticketReportFrame.pack();
    ticketReportHolder.setWheelScrollingEnabled(true);
    ticketReportFrame.add(ticketReportHolder);
    ticketReportFrame.setVisible(true);
    ticketReportFrame.setResizable(false);
}

this is just a small portion of drawTurnoverReport to try and help show what i am doing.

public class drawTurnoverReport extends JPanel{

   public drawTurnoverReport(theTicket[] tickets, String[] n, String[] t, String[] to){
        ticArray = tickets;
        notes = n;
        tapes = t;
        turnover = to;
        formalDate = mt.formalDate;
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent( g );
        Graphics2D g2 = (Graphics2D) g;



        //set fonts
        Font tahomaB = new Font("tahoma", Font.BOLD, 15);
        Font tahomaP = new Font("tahoma", Font.PLAIN, 15);

        //draw heading
        g2.setColor(Color.BLACK);
        g2.setFont(tahomaB);
        Rectangle2D rec = g2.getFontMetrics().getStringBounds("Turnover / Activity Log", g2);
        int stringW = (int) rec.getWidth();
        int prevStringH = (int) rec.getHeight();
        g2.drawString("Turnover / Activity Log", x-(stringW / 2) , 50);

        g2.setFont(tahomaP);
        rec = g2.getFontMetrics().getStringBounds("Date: " + formalDate, g2);
        stringW = (int) rec.getWidth();
        verticalPlace = 50 + prevStringH;
        g2.drawString("Date: " + formalDate, x-(stringW / 2) , verticalPlace);
        prevStringH = (int) rec.getHeight();
        verticalPlace += prevStringH;

        g2.setFont(tahomaB);
        rec = g2.getFontMetrics().getStringBounds("Service Manager Tickets", g2);
        stringW = (int) rec.getWidth();
        g2.drawString("Service Manager Tickets", x-(stringW / 2) , verticalPlace);
        verticalPlace +=2;
        g2.drawLine(((int)rec.getMinX()+ x)-(stringW / 2 +2), verticalPlace, 
                ((int)rec.getMaxX()+x)-(stringW / 2), verticalPlace);
        verticalPlace += 40;

        //draw table headers
        verticalUnderHeader = verticalPlace;
        g2.drawLine(50, verticalPlace, w-50, verticalPlace);
        rec = g2.getFontMetrics().getStringBounds(ticArray[0].getTicketNum(), g2);
        stringW = (int) rec.getWidth();
        rec = g2.getFontMetrics().getStringBounds("Ticket #", g2);
        temp = (int) rec.getWidth();
        g2.drawString("Ticket #", ((stringW+65+50)/2-(temp/2)), verticalPlace+17);
        horzPlace = stringW + 65;
        CNCxA = horzPlace;
        rec = g2.getFontMetrics().getStringBounds("Customer Name", g2);
        stringW = (int) rec.getWidth();
        widthCNC = (horzPlace + stringW + 30)-horzPlace;
        g2.drawString("Customer Name", horzPlace + 15, verticalPlace+17);
        horzPlace = horzPlace + stringW + 30;
        CNCxB = horzPlace;
        temp = w-horzPlace+30;
        DCx = temp;
        widthDC = temp - horzPlace;
        rec = g2.getFontMetrics().getStringBounds("Description", g2);
        stringW = (int) rec.getWidth();
        g2.drawString("Description", (horzPlace+temp)/2-(stringW/2), verticalPlace+17);
        rec = g2.getFontMetrics().getStringBounds("Status", g2);
        stringW = (int) rec.getWidth();
        g2.drawString("Status", (temp+(w-50))/2-(stringW/2), verticalPlace+17);
        verticalPlace += 20;
        g2.drawLine(50, verticalPlace, w-50, verticalPlace);
        verticalPlaceTemp = verticalPlace + 20;


    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
mig
  • 142
  • 7
  • How do you know for a **fact** that the ticketReport JPanel gets larger than its JScrollPane container? Do you have a listener added to the JPanel, such as a ComponentListener, that reports any and all changes in sizes of the JPanel to prove that your assumptions are correct? Do this and I'll bet you'll be in for a surprise. Note that your posted code contains nothing that will help us figure out what is wrong, suggesting the error lies elsewhere, and my guess is that your JPanel is not getting as big as you assume it is in fact getting. – Hovercraft Full Of Eels Mar 12 '14 at 04:13
  • @hovercraft well i don't know for a fact, that is why i am coming here for help. I appreciate your comment but no need to be so harsh. What i stated in my description is that the Jpanel gets larger than the screen, not larger than the ScrollPane itself. Are you suggesting that i try and set a size for the scrollpane? and what other code would you like to see, i could put the whole drawTurnoverReport class up but i didn't think people would need the 300+ lines of code that i am just drawing a picture with. – mig Mar 12 '14 at 04:21
  • How is the size of `drawTurnoverReport` determined? – MadProgrammer Mar 12 '14 at 04:25
  • Please explain my "harshness". I am expressing my opinion on your error, nothing more nor less, which I thought is what you came here for. I think you are reading emotion in my post that does not exist, something you should consider trying not to do in the future. What I think you should do is to test your code by adding a ComponentListener to the panel. Again see for yourself what's going on. Again, you have stated that the JPanel gets larger at some points in your program, and I"m asking you to debug your program by testing if this is in fact so. – Hovercraft Full Of Eels Mar 12 '14 at 04:25
  • @MadProgrammer i don't ever actually set a size, im drawing at particular x,y locations and eventually those locations are at a point that it surpasses the screen size – mig Mar 12 '14 at 04:26
  • So you're not changing the preferred size of the component in any way? – MadProgrammer Mar 12 '14 at 04:27
  • `"...what other code would you like to see, i could put the whole drawTurnoverReport class up but i didn't think people would need the 300+ lines of code that i am just drawing a picture with."` -- best would be to create and post a [minimal example program](http://stackoverflow.com/help/mcve). Please read the link before creating. – Hovercraft Full Of Eels Mar 12 '14 at 04:28
  • @MadProgrammer no, i have not set the preferred size of the panel because when i tried that the scroll pane worked for that size but the picture eventually surpassed that, due to not knowing the size before the picture is drawn. – mig Mar 12 '14 at 04:32
  • Again please clarify. You state that `"The panel itself will vary in size sometimes being smaller than the screen, sometimes larger."` -- but then you state in comment that you don't ever set its size or preferred size. – Hovercraft Full Of Eels Mar 12 '14 at 04:33
  • 1
    You can override `getPreferredSize` and return the know size of the image (or a default size until it is know), just make sure you are calling `revalidate` once you know the size of the image... – MadProgrammer Mar 12 '14 at 04:34
  • @HovercraftFullOfEels i wish i could, the program itself is a report, it is pulling info from a file and doesn't reach past the screen until the whole report is made so it involves the whole class – mig Mar 12 '14 at 04:34
  • If @MadProgrammer's decent answer doesn't help you (1+ to it!), then again consider first testing your code, as I suggest, and then creating and posting your minimal example program as per the link in my comment above. – Hovercraft Full Of Eels Mar 12 '14 at 04:36

1 Answers1

2

From the sounds of things, the drawTurnoverReport component isn't providing enough size hint information to the scroll pane.

JScrollPane will use the components preferredSize to determine if it needs to display scroll bars or not.

Ensure you are overriding the getPreferredSize method and return an appropriate size of the component.

For example:

This assumes that you are performing some kind of custom painting and not using something like JLabel to render the image, which would then suggest you have a layout issue somewhere...but without the source for drawTurnoverReport, it's impossible to know...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • im sorry to ask this, im just new to java, im trying though. i updated my question part of the drawTurnoverReport class to try and help clarify. I assume that you meant to override the getPreferredSize method in that same class i am just unsure on how to do it. I looked at the links you put in but those both use preset images and i can't determine how to make that work for my case. – mig Mar 12 '14 at 04:58
  • Here's an idea. Based on the information from the constructor, you should be able to either, generate a `BufferedImage` which can be painted to the component, or calculate the required width and height you would need to display it... – MadProgrammer Mar 12 '14 at 05:05
  • thanks for the help, with all the responses i got my wheels started turning and i threw one line at the bottom of my paintComponent method. `this.setPreferredSize(new Dimension(w-10,verticalPlace));` the verticalPlace is what i use throughout the method to keep where i am at vertically and the w is just the screen size width – mig Mar 12 '14 at 05:24