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();
}
}