Inserting String in JTextArea at specific location

Is there any function in Java where one can insert Strings at a specified JTextArea column number.

for example,

String str = "This is a sample text"

String => column Number

This => at 0

is => at 10

a => at 14

sample => at 20

text => at 25


Please do have a look at this example, which uses JTextComponent's viewToModel() method.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaExample extends JFrame
    private JTextArea tarea =  new JTextArea(10, 10);
    private JTextField tfield = new JTextField(10);
    private Object[] possibleValues = { "First", "Second", "Third" };

    private void createAndDisplayGUI()
        tarea.setText("Hello there\n");
        tarea.append("Hello student://");
        JScrollPane scroll = new JScrollPane(tarea);

        tfield.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent ae)
                tarea.append(tfield.getText() + "\n");

        tarea.addMouseListener(new MouseAdapter()
            public void mouseClicked(MouseEvent me)
                int x = me.getX();
                int y = me.getY();
                System.out.println("X : " + x);
                System.out.println("Y : " + y);
                int startOffset = tarea.viewToModel(new Point(x, y));
                System.out.println("Start Offset : " + startOffset);
                String text = tarea.getText();
                String firstPart = text.substring(0, startOffset);
                String secondPart = text.substring(startOffset, text.length());

                Object selectedValue = JOptionPane.showInputDialog(null,
                                                  "Choose one", "Input",
                                                  JOptionPane.INFORMATION_MESSAGE, null,
                                                  possibleValues, possibleValues[0]);

                if (selectedValue != null)  
                    String newText = firstPart + " " 
                                               + (String) selectedValue
                                               + " "
                                               + secondPart;

        getContentPane().add(scroll, BorderLayout.CENTER);
        getContentPane().add(tfield, BorderLayout.PAGE_END);

    public static void main(String... args)
        SwingUtilities.invokeLater(new Runnable()
            public void run()
                new TextAreaExample().createAndDisplayGUI();

Need Your Help

Pointer towards making images in UIImageView appear full screen when clicked on

objective-c uiimageview fullscreen

I have a UIImageView in my application. I want to make it so that when you click on the UIImageView, the photo appears full screen as it does when you click on a photo in the Photos app.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.