jlist printing problem

i have a jlist with about 4 items in it, and the 3rd one is selected.

but the size the jlist is in is only big enough for one item in the list to display, this is why i have a jscrollpane round it,

this works on the display, you can see the selected item in the list,

BUT when i try to print it, i cannot find a way to show the selected item in the jlist,

i have tried printing the jscrollpane and it prints a gray box, and

the jlist which gives the top index in the jlist,

but how do i get it to print the selected item in the jlist?

many thanks for any help.

[612 byte] By [motogpwadea] at [2007-11-27 9:46:11]
# 1

> i have a jlist with about 4 items in it, and the 3rd

> one is selected.

> but the size the jlist is in is only big enough for

> one item in the list to display, this is why i have a

> jscrollpane round it,

>

> this works on the display, you can see the selected

> item in the list,

...

> but how do i get it to print the selected item in the

> jlist?

package print;

/*

* PrintJListItemDemo.java

*/

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.awt.print.*;

import javax.swing.*;

public class PrintJListItemDemo extends JFrame {

private JButton btPrint;

private JList list;

public PrintJListItemDemo() {

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(150, 80);

setLocationRelativeTo(null);

list = new JList();

btPrint = new JButton();

list.setModel(new AbstractListModel() {

String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

public int getSize() { return strings.length; }

public Object getElementAt(int i) { return strings[i]; }

});

list.setSelectedIndex(3);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

list.scrollRectToVisible(list.getCellBounds(3, 3));

}

});

getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);

btPrint.setText("Print...");

btPrint.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

btPrintActionPerformed(evt);

}

});

getContentPane().add(btPrint, BorderLayout.NORTH);

}

private void btPrintActionPerformed(ActionEvent evt) {

PrinterJob printJob = PrinterJob.getPrinterJob();

PageFormat pageFormat = new PageFormat();

pageFormat.setOrientation(PageFormat.LANDSCAPE);

printJob.setPrintable(new ListItem(list), pageFormat);

if (printJob.printDialog())

try {

printJob.print();

} catch(PrinterException pe) {

System.out.println("Error printing: " + pe);

}

}

public static void main(String args[]) {

new PrintJListItemDemo().setVisible(true);

}

class ListItem extends JComponent implements Printable{

private JLabel component;

public ListItem(JList list){

component = (JLabel) list.getCellRenderer().getListCellRendererComponent(list,

list.getSelectedValue(), list.getSelectedIndex(), true, true);

int w = component.getPreferredSize().width;

int h = component.getPreferredSize().height;

component.setBounds(0, 0, w, h);

}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {

if (pageIndex > 0) {

return(NO_SUCH_PAGE);

} else {

int x = (int)pageFormat.getImageableX() + 1;

int y = (int)pageFormat.getImageableY() + 1;

g.translate(x, y);

component.print(g);

return(PAGE_EXISTS);

}

}

}

}

Message was edited by:

Andre_Uhres

Message was edited by:

Andre_Uhres

Message was edited by:

Andre_Uhres

Andre_Uhresa at 2007-7-12 23:56:09 > top of Java-index,Desktop,Core GUI APIs...