I have a class to print a component at a time. The things I want to print I, I group in JPanel. I therefore have a JPanel (inside a jframe to display it on the screen) with 4 JScrollPanes (syncronized ones). In each of these scrollpanes I have a JList. The thing I want to print out is the JLists beside each other as they are on the screen. However if the lists contains so many elements that the scrollpanes are able to scroll then it will only print the visible part of it where I want it to print the entire lists. I have tried to resize first the JPanel then the JScrollPanes and at last the JLists but it does not work.
This is code I use for resizing:
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:
It works partially. I can get the lists converted to images and it works great but when I want to add it to a JPanel to have one component to print out the layout becomes pretty wierd! The last lists image add will always start at (0,0) which it should not as I have set the bounds of it.
The code I have 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);