0

I have a button on my program, to Print the JTable with the Data that is displayed in it (it's filled with data that the user searches from the database). I want to print 2 lines on the header: one with the name of the selected item in the combobox (this is working) and other one with the name of the student (this one is not working). Here is my code:

    MessageFormat header = new MessageFormat("Ficha Pedagógica - "+jComboBox1.getSelectedItem());

    MessageFormat footer = new MessageFormat("Página {0,number,integer}");

    try{
        jTable2.print(JTable.PrintMode.NORMAL, header, null);
    }
    catch(java.awt.print.PrinterException e){
        System.out.println("error");
    }

When I tried to do this with the header, it didn't break the line, instead, it showed "/nAl..." in front of the first line:

MessageFormat header = new MessageFormat("Ficha Pedagógica - "+jComboBox1.getSelectedItem()+"\nNome do Aluno - "+jTextField1.getText());

Also, when I tried using 2 headers, I couldn't do it because the print function doesn't allow it. So, how can I have 2 header lines?

Vadio
  • 5
  • 2
  • 6
  • I don't see any attempt to break the line in your code. Why do you think it should have broken it? – RealSkeptic Oct 27 '14 at 18:32
  • I forgot to add it here, because it was not working before, so I took it out of my code. But I tried it again, and it didn't work anyway. Any ideas of why the line break doesn't work with the header? – Vadio Oct 27 '14 at 18:39

1 Answers1

0

Unfortunately, I don't think there is a way to do that. jTable is calling upon AWT functions to draw the header and the footer. It uses FontMetrics.getStringBounds() to measure how much space the header (and footer) will take, and then uses Graphics.drawString() to draw it. Unfortunately, these two methods do not take newlines into account.

You may be able to do this by getting the Printable object from the JTable and wrapping it with your own implementation. But this is not trivial.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • So there's no simple way of doing this? I see...well, I really need to know how to implement it, so, how could I do it? Or where could I learn how to do it? Keep in mind that I'm a beginner, so, knowing what I'm doing every step would be good. – Vadio Oct 27 '14 at 19:43
  • Take a look at [this answer](http://stackoverflow.com/a/12097309/4125191). In my opinion, it's not suitable for beginner level, but your opinion may differ. – RealSkeptic Oct 27 '14 at 19:53
  • I'll take a look at it. Thanks! If I can't follow something, I'll ask here. – Vadio Oct 27 '14 at 19:54
  • I was trying to implement it, but I get a lot of errors. Cannot Find Symbol: private final JTableHeader header; private final TableColumnModel colModel; Rectangle2D hRect = null; Rectangle2D fRect = null; AffineTransform oldTrans; Is this caused because I left something without importing? I don't know what causes this "Cannot find symbol" error... – Vadio Oct 27 '14 at 20:45
  • You need to open a new question about this, explain the background, show the code that you implemented, and what errors you get. You can check the documentation for each of the classes to see whether you imported it correctly, and try to eliminate as many errors as possible before posting your question. – RealSkeptic Oct 27 '14 at 20:49
  • Oooh, I see. I'll try finding it by myself first, and checking a little more, if I have no luck, I'll post another question. Thanks for the help! – Vadio Oct 27 '14 at 20:56