Abdh-Allah
Abdh-Allah

Reputation: 3

Scrollpane gets disappeared when frame is maximized

My prob is this....

JFrame ActualFrame = new JFrame("Actual frame");
JScrollPane pane = new JScrollPane(new JTable(data, columns));
ActualFrame.add(pane);
ActualFrame.add(PrintPreviewBtn);

PrintPreviewBtn.addActionListener(new ActionListener() {            
 public void actionPerformed(ActionEvent e) {
   JFrame PreviewFrame = new JFrame("Preview");
   PreviewFrame.add(pane, BorderLayout.CENTER);
}

When i run the program everything seems to b fine but when i press print preview button the preview frame shows off and when i maximize or resize the ActualFrame the table gets disappeared....

NOTE: I m adding the pane to the preview frame to show as if it is a preview of the table displayed.....is ter any other method for print preview

Upvotes: 0

Views: 344

Answers (3)

Vishal K
Vishal K

Reputation: 13066

The Reason for disappearance of JScrollPane from the ActualFrame is that: You are adding the same instance of pane in PreviewFrame. So the actual container of the pane is now PreviewFrame instead of ActualFrame. When you maximize or resize the ActualFrame,it repaint its child components. Since the pane now no longer belongs to the ActualFrame it does not show the pane now.

The best way to avoid this situation is to create a seperate JTabel instance first, instead of passing anonymous JTable class object within the constructor of JScrollPane while creating the object pane . Get the TableModel and TableColumnModel of that JTable instance. In previewFrame add a new instance of JScrollPane that will contain the new instance of JTable with same TableModel and TableColumnModel objects. The Code would look something like this:

final JTable table = new JTable(data,columns);
JScrollPane pane = new JScrollPane(table);
ActualFrame.add(pane);
ActualFrame.add(PrintPreviewBtn);

PrintPreviewBtn.addActionListener(new ActionListener() {            
 public void actionPerformed(ActionEvent e) {
   JFrame previewFrame = new JFrame("Preview");
   javax.swing.table.TableModel tabModel = table.getModel();
   javax.swing.table.TableColumnModel colModel = table.getColumnModel();
   JScrollPane previewPane = new JScrollPane(new JTable(tabModel,colModel));
   previewFrame.getContentPane().add(previewPane, BorderLayout.CENTER);
   previewFrame.pack();previewFrame.setVisible(true);
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

A component can only belong to a single parent. When you add it to the PreviewFrame, it is been removed, automatically, from the ActualFrame.

Instead of using the previous panel, create a new JTable, using the model from the previous one.

Update

Printing tables is a little more complicated, as includes the headers, and the columns need to be resized to meet the requirements of the available space.

Take a look at the Printing Tables tutorial for some examples

Upvotes: 1

Petr
Petr

Reputation: 6279

For printing there is the java.awt.PrinterJob class. To show the standart print preview you should call:

PrinterJob job = PrinterJob.getPrinterJob();
job.printDialog();

Upvotes: 1

Related Questions