This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Browse and Load a Text File  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!