Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really wanted to display text, which simulates like as if someone is typing, you can use <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html" rel="nofollow">javax.swing.Timer</a>, for this purpose and simply use a <code>JLabel</code> to display the text on the <code>JPanel</code> instead of painting it on the <code>JPanel</code>, since while painting, you have to worry about the Font Metrics and the placement of the said character at a given location, which can be cumbersome. Though if you intend to do something different from what I presented here, please reply likewise.</p> <p>Here is one example code for your help :</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TypingLetters { private String text; private JLabel letterLabel; private int counter = 0; private Timer timer; private ActionListener timerAction = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (counter &lt;= text.length()) { letterLabel.setText(text.substring(0, counter)); counter++; } else timer.stop(); } }; public TypingLetters() { text = "A long text that I want to" + " appear as being TYPED :-)"; } private void displayGUI() { JFrame frame = new JFrame("Typing Letters Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); letterLabel = new JLabel(); contentPane.add(letterLabel); frame.setContentPane(contentPane); frame.setSize(500, 200); frame.setLocationByPlatform(true); frame.setVisible(true); timer = new Timer(250, timerAction); timer.start(); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TypingLetters().displayGUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload