0

first im newbie and not good in english
is it possible to put system.out.print in frame? let's say if i want to see all my output print in textfield or something like that. i can put output print in textfield but it just settext one process. i want all log of my output in textfield, not just one output.
sorry if it dummy question,thanks for your answer before
i have code and i want to put all my output in text2(textfield)

   this is my whole code:



import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jpos.iso.BaseChannel;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOPackager;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOServer;
import org.jpos.iso.ISOSource;
import org.jpos.iso.ServerChannel;
import org.jpos.iso.channel.ASCIIChannel;
import org.jpos.iso.packager.GenericPackager; 
import jpos.JPosServer;

public class server extends javax.swing.JFrame {


   public server() {
    initComponents();
}


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    mulai = new javax.swing.JButton();
    text = new javax.swing.JScrollPane();
    text1 = new javax.swing.JTextArea();
    jScrollPane1 = new javax.swing.JScrollPane();
    JTextPane = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    mulai.setText("Star Server");
    mulai.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            mulaiActionPerformed(evt);
        }
      });
      getContentPane().add(mulai, new org.netbeans.lib.awtextra.AbsoluteConstraints(129, 66, -1, -1));

      text1.setColumns(20);
      text1.setRows(5);
      text.setViewportView(text1);

      getContentPane().add(text, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 96, 350, 130));

      jScrollPane1.setViewportView(JTextPane);

      getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 250, 330, 170));

    pack();
}// </editor-fold>




  private void log(String msg) {
    JTextPane guiConsole = new JTextPane();
  Document doc = guiConsole.getDocument();
  try {
      doc.insertString(doc.getLength(), msg + "\r\n", null);
   } catch (BadLocationException e) {}
  }



  private void setText2Text(String msg) {
  String toAppend = text1.getText();
  toAppend = toAppend + "/n" + msg;
  text1.setText(toAppend);
  }

private void mulaiActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

}






   public boolean process1(ISOSource isoSrc, ISOMsg isoMsg) {
   try {
         log("Server menerima koneksi dari ["+((BaseChannel)isoSrc).getSocket().getInetAddress().getHostAddress()+"]");

        if (isoMsg.getMTI().equalsIgnoreCase("1800")) {
                acceptNetworkMsg(isoSrc, isoMsg);
        }
    } catch (IOException ex) {
        Logger.getLogger(JPosServer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ISOException ex) {
        Logger.getLogger(JPosServer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

    private void acceptNetworkMsg(ISOSource isoSrc, ISOMsg isoMsg) throws ISOException, IOException {

      log("Accepting Network Management Request");

    ISOMsg reply = (ISOMsg) isoMsg.clone();
    reply.setMTI("1810");
    reply.set(39, "00");
    isoSrc.send(reply);
} 


/**
 * @param args the command line arguments
 */
public static void main(String args[]) throws ISOException {

    String hostname = "localhost";
    int portNumber = 1234;

    // membuat sebuah packager
    ISOPackager packager = new GenericPackager("src/jpos/iso93ascii.xml");
    // membuat channel
    ServerChannel channel = new ASCIIChannel(hostname, portNumber, packager);
    // membuat server
    ISOServer server = new ISOServer(portNumber, channel, null);
    server.addISORequestListener(new JPosServer());
    new Thread(server).start();

    System.out.println("Server siap menerima koneksi pada port [" + portNumber+"]");


    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new server().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JTextPane JTextPane;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton mulai;
private javax.swing.JScrollPane text;
private javax.swing.JTextArea text1;
// End of variables declaration

}

thanks for getting in my problem yogendra,i really appreciate it

2

3 Answers 3

0

before each text2.setText(), you can get the text first, then append the new string in the original string.

String toAppend = text2.getText();
toAppend = toAppend + "/n" + "Your new string message here!";
text2.setText(toAppend);

Updated:

private void setText2Text(String msg) {
    String toAppend = text2.getText();
    toAppend = toAppend + "/n" + msg;
    text2.setText(toAppend);
}

Then in your code, replace

text2.setText("Your message here") 

to

setText2Text("Your message here")
2
  • Did you add the code to all three methods? Or to reduce duplicate code, you can write your own setTextMsg(String msg) method
    – Don
    Commented Nov 27, 2012 at 18:21
  • i add code to all three method. then i reduce it as you mention before and it still dosn't work Commented Nov 27, 2012 at 18:41
0

To simulate a console on your GUI, you may need a JTextPane because JTextField can display only one line of text. You need to create your own way of "printing out" to the text pane (You can't use System.out.println to print to GUI).

Code Sample:

Assuming you have this as the target:

JTextPane guiConsole = new JTextPane();

You can create a function like this:

private void log(String msg) {
    Document doc = guiConsole.getDocument();
    try {
        doc.insertString(doc.getLength(), msg + "\r\n", null);
    } catch (BadLocationException e) {}
}

Now, wherever you would have used System.out.println("Hello"); to print to the console, just call log("Hello"); to print the same text to your text pane.

7
  • thanks for answer david,but it not show in textpane.where i declare text pane parameter assuming variable name textpane is txtpane1? Commented Nov 27, 2012 at 20:06
  • @poundPound Is your textpane showing? Did you ever call the log method after displaying the gui?
    – davidXYZ
    Commented Nov 27, 2012 at 20:27
  • You did it wrongly. The JTextPane guiConsole = new JTextPane(); should not be inside the log method. It should be declared as a class field. Preferably at the beginning right after you declared jScrollPane1 = new javax.swing.JScrollPane();. Also, don't forget to add it to your frame, like this getContentPane().add(guiConsole).
    – davidXYZ
    Commented Nov 27, 2012 at 20:32
  • Delete the line that says JTextPane = new javax.swing.JTextPane();. It's a compile-time error.
    – davidXYZ
    Commented Nov 27, 2012 at 20:35
  • i can't delete it,it generated by netbbeans Commented Nov 27, 2012 at 20:46
0

Keep updating the text with the appended text using setText method itself. Use getText to retrieve the existing text and append the new text with the existing text separated by new line.

Use System.lineSeparator() to append the new line in below:

  String text = text2.getText();
  text= text + System.lineSeparator() + "New line text";//<--put the text here
  text2.setText(text);  //<--update the text field value with modified text 

EDIT:

You are initializing the server inside the thread. Try instantiating it outside and use the same to set the text:

 final server myServer = new server();
 /* Create and display the form */
 java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        myServer.setVisible(true);
    }
  });

  JTextArea text1 = myServer.getText1().
  String text = text1.getText();
  text= text + System.lineSeparator() + "New line text";//<--put the text here
  text1.setText(text);  //<--update the text field value with modified text 

Also define a getter method for text1 in you class as:

  public JTextArea getText1(){
     return this.text1;
  }
10
  • @poundPound: Oops. I was having some typos. Corrected now(updated the answer). Check and let me know, if still doesn't work. Commented Nov 27, 2012 at 18:57
  • @poundPound I don't see any static context. Can you please point exactly where it is? Any way, it should be working. Is there any issue? Commented Nov 27, 2012 at 19:06
  • public static void main(String args[]) throws ISOException { the first line is the static method and it say you can't reference static method to non static method Commented Nov 27, 2012 at 19:39
  • @poundPound Which statement in main method gives that error? I know the fix(should be simple), just struggling in getting your issue. Commented Nov 27, 2012 at 19:44
  • thankyou so much for reply yogendra.statement give that error is text2.setText("Server siap menerima koneksi pada port [" + portNumber+"]"); Commented Nov 27, 2012 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.