1

I am printing some records of invoice in a jTextArea just like below:

 +----------------------------------------+      
 |            V.K Autos Larkana           |       
 |      Near Rehmaniya Masjid Larkana     |      
 |     Police Shopping Center Larkana     |      
 |    Cell: 0334-3269198, 0333-3910951    |      
 |                                        |      
 |            CUSTOMER INVOICE            |      
 +--------------------+-------------------+      
 |INFO                |CUSTOMER           |      
 +--------------------+-------------------+      
 |DATE: 2016/01/28    |Mohammad Rafi Abro |      
 |TIME: 15:31:15      |Bill No:10         |      
 +--------------------+-------------------+      
 |             SELLING DETAILS            |      
 +----------------+-----+-------+---------+      
 |ITEM            |  QTY|   Rate|    Total|      
 +----------------+-----+-------+---------+      
 |nuts            |   12|     25|      300|      
 |70 CC           |   12|   7000|    84000|      
 |125 CC          |   13|  95000|  1235000|      
 |12              |   25|   2500|    62500|      
 +----------------+-----+-------+---------+      
 |                     Sub Total|  1381800|      
 +------------------------------+---------+      
 |  ------Thank you for your visit------  |      
 +----------------------------------------+      
 | ******** Soulution by AbroSoft ******* |      
 | * abrosoft@outlook.com # 03337584273 * |      
 +----------------------------------------+  

the text inside the frame are String, taken from different variables or jTextField, I want to bold some text in it like Company name should be in bold, address should be in italic or titles should be in bold like below:

V.K Autos Larkana
Near Rehmaniya Masjid Larkana
Police Shopping Center Larkana
Cell: 0334-3269198, 0333-3910951

code I am writing is below:

String company = ""
            + "V.K Autos Larkana\n"
            + "Near Rehmaniya Masjid Larkana\n"
            + "Police Shopping Center Larkana\n"
            + "Cell: 0334-3269198, 0333-3910951\n"
            + " \n"
            + "CUSTOMER INVOICE"
            + " ";

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    List<String> t1Headers = Arrays.asList("INFO", "CUSTOMER");
    List<List<String>> t1Rows = Arrays.asList(
            Arrays.asList("DATE: " + date + "", "" + txt_cnam.getText()),
            Arrays.asList("TIME: " + sdf.format(cal.getTime()) + "", "Bill No:" + billno + "")
    );
    String t2Desc = "SELLING DETAILS";
    List<String> t2Headers = Arrays.asList("ITEM", "QTY", "Rate", "Total");

    List<List<String>> t2Rows = new ArrayList<List<String>>();

    for (int i = 0; i < tbl_sale.getRowCount(); i++) {
        String pid = tbl_sale.getValueAt(i, 0).toString();
        String item = tbl_sale.getValueAt(i, 1).toString();
        String quant = tbl_sale.getValueAt(i, 2).toString();
        String rate = tbl_sale.getValueAt(i, 3).toString();
        String rs = tbl_sale.getValueAt(i, 4).toString();
        ArrayList<String> temp = new ArrayList<String>();
        temp.add(item);
        temp.add(quant);
        temp.add(rate);
        temp.add(rs);
        t2Rows.add(temp);
    }
    List<Integer> t2ColWidths = Arrays.asList(20, 4, 7, 12);
    String t3Desc = "Sub Total";
    List<String> t3Headers = Arrays.asList("", "", "", "");
    List<List<String>> t3Rows = Arrays.asList(
            Arrays.asList("WR-DVD", "122.00", "7", "854.00")
    );

    String summary = ""
            + "Sub Total\n";

    String summaryVal = ""
            + txt_total.getText() + "\n";
    String sign1 = ""
            + "---------Thank you for your visit---------\n";
    String sign2 = ""
            + "-We value your visit-\n";
    String advertise = "********** Soulution by AbroSoft *********\n*** abrosoft@outlook.com # 03337584273 ***";
    try {
        Board b = new Board(48);
        b.setInitialBlock(new Block(b, 46, 6, company).allowGrid(true).setBlockAlign(Block.BLOCK_CENTRE).setDataAlign(Block.DATA_CENTER));
        b.appendTableTo(0, Board.APPEND_BELOW, new Table(b, 48, t1Headers, t1Rows));
        b.getBlock(3).setBelowBlock(new Block(b, 46, 1, t2Desc).setDataAlign(Block.DATA_CENTER));
        b.appendTableTo(5, Board.APPEND_BELOW, new Table(b, 48, t2Headers, t2Rows, t2ColWidths));
        Block summaryBlock = new Block(b, 33, 1, summary).setDataAlign(Block.DATA_TOP_RIGHT);
        b.getBlock(10).setBelowBlock(summaryBlock);
        Block summaryValBlock = new Block(b, 12, 1, summaryVal).setDataAlign(Block.DATA_TOP_RIGHT);
        summaryBlock.setRightBlock(summaryValBlock);
        Block sign1Block = new Block(b, 46, 1, sign1).setDataAlign(Block.DATA_CENTER);
        b.getBlock(14).setBelowBlock(sign1Block);
        Block sign2Block = new Block(b, 46, 2, advertise).setDataAlign(Block.DATA_CENTER);
        b.getBlock(16).setBelowBlock(sign2Block);   
        jTextArea1.setText(b.invalidate().build().getPreview()); 

note: This is the receipt printer format I have followed by this link

Community
  • 1
  • 1
mrabro
  • 1,097
  • 4
  • 13
  • 27
  • 3
    already answered : http://stackoverflow.com/questions/2713863/how-to-display-bold-text-in-only-parts-of-jtextarea – chenchuk Feb 14 '16 at 20:31
  • no, I have gone through that, thats not my question, I am appending different strings to my `jTextArea` and I want to apply different font styles to different strings and append them in jTextArea – mrabro Feb 14 '16 at 20:34
  • 2
    No, `JTextArea` is simply not capable of doing this, you will need to use a `JTextPane` or a `JEditorPane`, otherwise you'll need to do it by hand (custom painting) or maybe instead, use something like JasperReports, which is designed to do this kind of thing – MadProgrammer Feb 14 '16 at 20:52
  • @RafiAbro FYI, chenchuk's duplicate link is the answer you are looking for - `JTextArea` deals only with PLAIN text, it's not capable of dealing with styes or markups. – MadProgrammer Feb 14 '16 at 20:53
  • but when i use `jEditorPane` or `jTextPane` then there is no any alignment in my text, like center or bottom left/right etc.. :( – mrabro Feb 14 '16 at 20:54
  • then i'll have to recode every thing for text alignment using html tags on each string, – mrabro Feb 14 '16 at 21:00
  • @RafiAbro You're saying that RTF and HTML don't have alignment capabilities? I think you not seeing the forest for the trees – MadProgrammer Feb 14 '16 at 21:37
  • I mean that what i hv code to maintain my alignments will not work on JtextEditor.. for that ill have to recode it – mrabro Feb 14 '16 at 21:39
  • *"..for that ill have to recode it "* So ..get on with it. Wasting time here complaining about it, won't get the job done. – Andrew Thompson Feb 15 '16 at 08:40
  • I am on it, as soon as I re coded, I'll post that here, don't worry about that – mrabro Feb 15 '16 at 08:42

0 Answers0