0

I'm making a shipping label program, where the address that has been inputted is previewed on JLabel. The problem is JLabel isn't supporting multiline text. How to make it multiline?

I've tried HTML method but it didn't work.

Anyway I'm working on Netbeans 8.0.2

private void buttonexitActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int result = JOptionPane.showConfirmDialog(null, "Exit now?", "You're about to exit", JOptionPane.YES_NO_OPTION);
    if(result == JOptionPane.YES_OPTION){
        System.exit(0);
    }
    else if(result == JOptionPane.NO_OPTION) {
        this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
    }
}                                                                                    

private void buttonpreviewActionPerformed(java.awt.event.ActionEvent evt) {                                              
    pname.setFont(new java.awt.Font("Times New Roman", 0, 12));
    pname.setForeground(Color.BLACK);
    pname.setText(String.valueOf(inputname.getText()));

    pnumber.setFont(new java.awt.Font("Times New Roman", 0, 12));
    pnumber.setForeground(Color.BLACK);
    pnumber.setText(String.valueOf(inputnumber.getText()));

    paddress.setFont(new java.awt.Font("Times New Roman", 0, 12));
    paddress.setForeground(Color.BLACK);
    padress.setText(String.valueOf(inputaddress.getText()));



    pcourier.setFont(new java.awt.Font("Times New Roman", 1, 12));
    pcourier.setForeground(Color.BLACK);
  pcourier.setText(String.valueOf(cbcourier.getModel().getSelectedItem()));
}                                             

private void buttonclearActionPerformed(java.awt.event.ActionEvent evt) {                                            
    inputname.setText("");
    inputnumber.setText("");
    inputaddress.setText("");
    cbcourier.setSelectedIndex(0);
}                                           

private void buttonprintActionPerformed(java.awt.event.ActionEvent evt) {                                            
    Toolkit tk = panelpreview.getToolkit();
    PrintJob pj = tk.getPrintJob(this, null, null);
    Graphics g = pj.getGraphics();
    panelpreview.print(g);
    g.dispose();
    pj.end();

I want the "paddress" (JLabel) can show the text the same as I input in "inputadress" (JTextField). Any suggestion?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lines
  • 13
  • 4

1 Answers1

0

I've found the answer using HTML, replace the /n with
.

 padress.setText("<html>" + String.valueOf(inputaddress.getText().replaceAll("<","&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br/>")) + "</html>");

Source: source

lines
  • 13
  • 4