aspose file tools*
The moose likes Java in General and the fly likes Browse and Load a Text File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Browse and Load a Text File" Watch "Browse and Load a Text File" New topic
Author

Browse and Load a Text File

sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
hi, i have made a GUI and i am having problems with browsing for a text file and loading it on to the GUI. i can get a browse dialogue box to appear, but thats all i can do.

any help will be much appreciated.

thnak you.

here is the code for my GUI:-

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JTextField.*;
import javax.swing.JTextArea.*;
import java.awt.print.*;
import java.io.*;

class JFrameDemo extends JFrame {

private JTextField FUText, inputText, resultText;
JFrame jWindow;

public JFrameDemo()
{
super("Halstead Metrics");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = this.getContentPane(); //we add components to the contentPane
contentPane.setLayout(new GridLayout(0,1,3,3));

JPanel topPanel = new JPanel(new GridLayout(0,2,2,2));
JPanel middlePanel = new JPanel(new GridLayout(1,1,5,5));
JPanel bottomPanel = new JPanel(new GridLayout(0,2,5,5));
JPanel endPanel = new JPanel(new GridLayout(0,1,5,5));

//JPanel topPanel = new JPanel();
JLabel FULabel = new JLabel("File or URL", JLabel.CENTER);
topPanel.add(FULabel);
FULabel.setToolTipText("Enter File or URL");
FULabel.setVisible(true);

FUText = new JTextField(10);
FUText.setToolTipText("Enter File or URL");
FUText.setText("Enter File or URL");
FUText.setEditable(true);
topPanel.add(FUText);



JButton browse = new JButton("Browse");
topPanel.add(browse);

browse.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser browse = new JFileChooser();
browse.showOpenDialog(jWindow);
}
}
);

JButton load = new JButton("Load Script");
topPanel.add(load);




contentPane.add(topPanel); //add the panel to the frame

final JTextArea inputText = new JTextArea();
JScrollPane scrollingArea = new JScrollPane();
scrollingArea.add(new JTextArea(5,5));
inputText.setEditable(true);
middlePanel.add(inputText);

//JScrollPane jsp = new JScrollPane();
//jsp.add(new JTextArea(5,5));


JButton execute = new JButton("Execute");
bottomPanel.add(execute);



JButton clear = new JButton("Clear Screen");
clear.setToolTipText("Click to Cancel Request");
bottomPanel.add(clear);

clear.addActionListener ( new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
FUText.setText(null);
inputText.setText(null);
resultText.setText(null);
}
}
);

JButton refresh = new JButton("Refresh");
bottomPanel.add(refresh);

refresh.addActionListener ( new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
resultText.setText(null);
}
}
);

contentPane.add(middlePanel); //add the panel to the frame

//JPanel bottomPanel = new JPanel();
final JTextArea resultText = new JTextArea();
resultText.setEditable(false);
endPanel.add(resultText);



JButton exit = new JButton("EXIT");
bottomPanel.add(exit);

exit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);

//creates menu bar.
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu menuFile = new JMenu ("File");
menuBar.add(menuFile);

JMenuItem fSave = new JMenuItem("Save");
menuFile.add(fSave);

JMenuItem fPrint = new JMenuItem("Print");
menuFile.add(fPrint);

fPrint.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
//printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
}
catch(PrinterException pe)
{
System.out.println("Error printing: " + pe);
}
}
}
);

JMenuItem fExit = new JMenuItem("Exit");
menuFile.add(fExit);

fExit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);

contentPane.add(bottomPanel); //add the panel to the frame
contentPane.add(endPanel);

//--------Other javax.swing components-------------

this.pack();
this.setVisible(true);
}

/* Test */
public static void main (String[] arg) {
JFrameDemo jf = new JFrameDemo();
}
}
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,

what is your *exact* problem? Compiling problems? Or do you expect your program to behave differently? What does your program and what should it do?

Stefan
sukh singh
Greenhorn

Joined: Nov 30, 2004
Posts: 13
i am trying to browse for any text file and load the context of the text file in my input text area.
the browse button is supposed to browse for any text file and bring up the loaction i.e, H:/mydocuments/code.txt in the FUText field and when the load button is pressed the context of the chosen browsed file should appear in the input text area.
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,

the class JFileChooser provides a method getSelectedFile() which return a File-object. You can read the content of this file (BufferedReader and FileReader) into a StringBuffer and set the content of the textfield to the content of the StringBuffer.

Stefan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Browse and Load a Text File
 
Similar Threads
Swing/Applet question
i like this better
Event listener not responding
Having trouble adding a top panel to my GUI
Changing Layout of a Panel in Border Layout