Results 1 to 10 of 10
- 07-09-2010, 12:05 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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?????
- 07-09-2010, 12:37 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Last edited by JosAH; 07-09-2010 at 01:16 PM.
- 07-09-2010, 12:42 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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
- 07-09-2010, 12:47 PM #4
Please use code tags when posting your code.
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: tajtextarea that is on ptextpanel.java
You will need to move the definition of the variable: text out of the constructor to make it a member variable.
- 07-09-2010, 12:51 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
@Norm
could you please write that code for me ..i think it would be easier for me to get what you are saying
- 07-09-2010, 01:03 PM #6
Sorry, we expect you to write the code. We'll help you get it to work.
- 07-09-2010, 01:07 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
but i m not getting that you are saying...and i m a newbie in java....
- 07-09-2010, 01:14 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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
}
}
- 07-09-2010, 01:15 PM #9
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.
- 07-09-2010, 01:25 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
help me with JTextArea
By javanew in forum New To JavaReplies: 3Last Post: 05-04-2010, 08:51 PM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Tab key in JTextArea
By KristoZ in forum New To JavaReplies: 1Last Post: 09-25-2009, 07:27 PM -
About JTEXTAREA
By makpandian in forum AWT / SwingReplies: 4Last Post: 03-19-2009, 06:53 AM -
JTextArea
By saytri in forum New To JavaReplies: 0Last Post: 01-13-2008, 01:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks