Java printing full scroll not only visible part

I have a class to print a component at a time. What I want to print I am going to JPanel. So I have a JPanel (inside a jframe to display it on screen) with 4 JScrollPanes (in sync). In each of these scrollpanes, I have a JList. What I want to print is the JLists next to each other as they are on the screen. However, if the lists contain so many items that scrolls can scroll, then it will only print the visible part where I want it to print all the lists. I tried resizing the JPanel first, then JScrollPanes, and finally JLists, but it doesn't work.

This is the code I am using to resize:

    int x,y,width,height;

    //jPanel1 setBounds
    x = jPanel1.getBounds().x;
    y = jPanel1.getBounds().y;
    width = jPanel1.getBounds().width;
    height = lm1.getSize() * jList1.getFixedCellHeight() + 20;
    jPanel1.setBounds(x, y, width, height);

    //jList1 setBounds
    x = jList1.getBounds().x;
    y = jList1.getBounds().y;
    width = jList1.getBounds().width;
    height = lm1.getSize() * jList1.getFixedCellHeight();
    jScrollPane1.setBounds(x, y, width, height);
    jList1.setBounds(x, y, width, height);
    jList1.setVisibleRowCount(lm1.getSize());

    //jList2 setBounds
    x = jList2.getBounds().x;
    y = jList2.getBounds().y;
    width = jList2.getBounds().width;
    height = lm2.getSize() * jList2.getFixedCellHeight();
    jScrollPane2.setBounds(x, y, width, height);
    jList2.setBounds(x, y, width, height);
    jList2.setVisibleRowCount(lm2.getSize());

    //jList3 setBounds
    x = jList3.getBounds().x;
    y = jList3.getBounds().y;
    width = jList3.getBounds().width;
    height = lm3.getSize() * jList3.getFixedCellHeight();
    jScrollPane3.setBounds(x, y, width, height);
    jList3.setBounds(x, y, width, height);
    jList3.setVisibleRowCount(lm3.getSize());

    //jList4 setBounds
    x = jList4.getBounds().x;
    y = jList4.getBounds().y;
    width = jList4.getBounds().width;
    height = lm4.getSize() * jList4.getFixedCellHeight();
    jScrollPane4.setBounds(x, y, width, height);
    jList4.setBounds(x, y, width, height);
    jList4.setVisibleRowCount(lm4.getSize());

    PrintUtil.printComponent(jPanel1);

      

EDIT:

This works partially. I can get lists converted to images and it works great, but when I want to add it to the JPanel so that one component prints out the layout it gets pretty weird! The last addition of the lists image always starts with (0,0), which it shouldn't since I set its borders.

The code I made:

    BufferedImage bi1 = componentToImage(jList1, false);
    BufferedImage bi2 = componentToImage(jList2, false);
    BufferedImage bi3 = componentToImage(jList3, false);
    BufferedImage bi4 = componentToImage(jList4, false);

    ImagePanel ip1 = new ImagePanel(bi1);
    ip1.setBounds(20, 20, bi1.getWidth(), bi1.getHeight());

    ImagePanel ip2 = new ImagePanel(bi2);
    ip2.setBounds(30 + bi1.getWidth(), 20, bi2.getWidth(), bi2.getHeight());

    ImagePanel ip3 = new ImagePanel(bi3);
    ip3.setBounds(40 + bi1.getWidth() + bi2.getWidth(), 20, bi3.getWidth(), bi3.getHeight());

    ImagePanel ip4 = new ImagePanel(bi4);
    ip4.setBounds(50 + bi1.getWidth() + bi2.getWidth() + bi3.getWidth(), 20, bi4.getWidth(), bi4.getHeight());

    JLabel jl1 = new JLabel();
    JLabel jl2 = new JLabel();
    JLabel jl3 = new JLabel();
    JLabel jl4 = new JLabel();

    jl1.setBounds(20, 0, bi1.getWidth(), bi1.getHeight());
    jl2.setBounds(30 + bi1.getWidth(), 0, bi2.getWidth(), bi2.getHeight());
    jl3.setBounds(40 + bi1.getWidth() + bi2.getWidth(), 0, bi3.getWidth(), bi3.getHeight());
    jl4.setBounds(50 + bi1.getWidth() + bi2.getWidth() + bi3.getWidth(), 0, bi4.getWidth(), bi4.getHeight());

    jl1.setText(jLabel1.getText());
    jl2.setText(jLabel2.getText());
    jl3.setText(jLabel3.getText());
    jl4.setText(jLabel4.getText());

    JPanel jp = new JPanel();
    jp.add(jl1);
    jp.add(ip1);
    jp.add(jl2);
    jp.add(ip2);
    jp.add(jl3);
    jp.add(ip3);
    jp.add(jl4);
    jp.add(ip4);
    jp.setVisible(true);

      

+3


source to share





All Articles