Results 1 to 10 of 10
  1. #1
    ruchi is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default how to get filecontents in jtextarea

    hello guys
    i made a frame file textframe.java in which i have a menubar and toolbar and in menu bar i have a file option which has new,open,save and saveas menuitem.
    what i want to dis is i want to open filecontents by selecting file from filechooser and want to display it's contents on jtextarea that is on the another file named as textpanel.java
    so can anybody here helpme out?????

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ruchi View Post
    hello guys
    i made a frame file textframe.java in which i have a menubar and toolbar and in menu bar i have a file option which has new,open,save and saveas menuitem.
    what i want to dis is i want to open filecontents by selecting file from filechooser and want to display it's contents on jtextarea that is on the another file named as textpanel.java
    so can anybody here helpme out?????
    I do it like this:

    Java Code:
    BufferedReader br= // reader of you file ...
    // ta is your JTextArea
    ta.setText(""); // clear the current text
    ta.getUI().getEditorKit(ta).read(br, ta.getDocument(), 0);
    ta.setCaretPosition(0);
    kind regards,

    Jos
    Last edited by JosAH; 07-09-2010 at 01:16 PM.

  3. #3
    ruchi is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    i did not get you
    so here are my files
    this is textframe class
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package texteditor;

    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.io.File;



    /**
    *
    * @author Ratika
    */


    class textFrame extends JFrame implements ActionListener {

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu helpMenu;
    private JMenuItem aboutItem;
    private JMenuItem newItem;
    private JMenuItem openItem;
    private JMenuItem exitItem;
    private JMenuItem saveItem;
    private JMenuItem saveasItem;


    public textFrame() {

    setSize(600, 500);
    setLocationByPlatform(true);


    menuBar = new JMenuBar();
    fileMenu = new JMenu("File");
    newItem = new JMenuItem("New...");
    openItem = new JMenuItem("Open..");
    saveItem = new JMenuItem("Save");
    saveasItem = new JMenuItem("Save As");
    exitItem = new JMenuItem("Exit");
    newItem.setMnemonic( KeyEvent.VK_N );
    openItem.setMnemonic( KeyEvent.VK_O);
    saveItem.setMnemonic(KeyEvent.VK_S);

    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MA SK);
    KeyStroke keystrok = KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK);
    KeyStroke keystro = KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK);
    openItem.setAccelerator(keystro);
    newItem.setAccelerator(keystroke);
    saveItem.setAccelerator(keystrok);
    fileMenu.add(newItem);

    fileMenu.add(openItem);
    fileMenu.add(saveItem);
    fileMenu.add(saveasItem);
    fileMenu.add(exitItem);

    helpMenu = new JMenu("Help");
    aboutItem = new JMenuItem("About");
    helpMenu.add(aboutItem);
    menuBar.add(fileMenu);
    menuBar.add(helpMenu);
    setJMenuBar(menuBar);

    openItem.addActionListener(this);
    exitItem.addActionListener(this);
    aboutItem.addActionListener(this);

    add(new ptextpanel());
    JButton new1 = new JButton( new ImageIcon("c:\\actions\\new.png"));
    JButton open1 = new JButton( new ImageIcon("c:\\actions\\open.png"));
    JButton save1= new JButton( new ImageIcon("c:\\actions\\save.png"));
    JToolBar jtoolbar = new JToolBar();
    jtoolbar.add(new1);
    jtoolbar.add(open1);
    jtoolbar.add(save1);
    jtoolbar.setRollover(true);
    new1.setToolTipText("new");
    open1.setToolTipText("open");
    save1.setToolTipText("save");
    open1.addActionListener(this);

    add(jtoolbar,BorderLayout.NORTH);
    jtoolbar.setFloatable(false);
    pack();
    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitItem)
    {
    System.exit(0);
    }

    if( e.getSource()==openItem)
    {
    filechooser fchooser= new filechooser();

    }


    else{
    JOptionPane.showMessageDialog((Component)null,"hel lo");

    }

    }

    }


    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    textFrame frame = new textFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    }


    and this my ptextpanel class

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /*
    * ptextpanel.java
    *
    * Created on Jul 4, 2010, 11:49:24 AM
    */

    package texteditor;

    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    /**
    *
    * @author Ratika
    */
    public class ptextpanel extends javax.swing.JPanel {

    /** Creates new form ptextpanel */
    public ptextpanel() {
    initComponents();
    }
    @SuppressWarnings("unchecked")

    private void initComponents() {

    JTextArea text = new JTextArea(50, 60);
    text.setEditable(true);
    JScrollPane jscroll=new JScrollPane(text);
    jscroll.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jscroll.setPreferredSize(new Dimension(250, 250));
    text.setLineWrap(true);
    add(text);
    }
    }


    nw i want to display my results on jtextarea that is on ptextpanel.java file

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Please use code tags when posting your code.

    jtextarea that is on ptextpanel.java
    You need to make a reference to that text area available to the code that has the data from the file. In JosAH's post, that would be the variable: ta
    You will need to move the definition of the variable: text out of the constructor to make it a member variable.

  5. #5
    ruchi is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    @Norm
    could you please write that code for me ..i think it would be easier for me to get what you are saying

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Sorry, we expect you to write the code. We'll help you get it to work.

  7. #7
    ruchi is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    but i m not getting that you are saying...and i m a newbie in java....

  8. #8
    ruchi is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    i write this code on textframe.java

    if( e.getSource()==openItem)
    {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(null);
    File fileobj = chooser.getSelectedFile();
    if(result== JFileChooser.APPROVE_OPTION)
    {
    \\ what to write here so that i can get the file contents on jtextarea that is on ptextpanel.java file
    }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    To get a reference to a member in another class, you must first have a reference to that class. For example for class A to get at the membr1 member of class B:

    in class A:
    B b = new B(); // create class B object and set b to point to it
    then
    b.membr1
    gives you access to the membr1 member of class B.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ruchi View Post
    i did not get you
    My code suggestion assumed a reference to a 'ta' JTextArea; if you don't have such a reference don't dump all your code at me, I'm not going to read it; you have to take care that you have a reference to that JTextArea somehow. Got it?

    kind regards,

    Jos

Similar Threads

  1. help me with JTextArea
    By javanew in forum New To Java
    Replies: 3
    Last Post: 05-04-2010, 08:51 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  3. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 07:27 PM
  4. About JTEXTAREA
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 03-19-2009, 06:53 AM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 01:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •