browse button importing values

i have created a browse button. A browser opens up when i click the brose button. i have 3 inputs. on my gui,, i have 3 options in file open. ok cancel and browse with a text box. if i type in the txt file i would like to import and click ok to it it works fine. but when i click browse. the browse screen opens up i select the file click ok but no values appear. any ideas as to why. the browse coding used is.

example. copy this code and save

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

import java.util.*;

public class browser extends JFrame implements ActionListener, Serializable

{

private JTextField input;

private JTextField no;

private JTextField output;

private JButton browse;

private JButton file;

private int inputs, nos, outputs;

public browser()

{

input = new JTextField("");

no = new JTextField("");

output = new JTextField("");

browse = new JButton("Browse");

browse.addActionListener(this);

file = new JButton("Read from file");

file.addActionListener(this);

/** Add buttons to panel */

JPanel buttonsPanel = new JPanel(new FlowLayout());

buttonsPanel.add(browse);

buttonsPanel.add(file);

JPanel functionsPanel = new JPanel(new GridLayout(3,2,5,5));

functionsPanel.add(input);

functionsPanel.add(no);

functionsPanel.add(output);

/** puts on pane */

Container contentPane = this.getContentPane();

contentPane.setLayout(new GridLayout(5,3,2,5));

contentPane.add(buttonsPanel);

contentPane.add(functionsPanel);

this.pack();

this.setVisible(true);

}

public void actionPerformed(ActionEvent evt)

{

if(evt.getSource() == browse)

{

final JFileChooser browse = new JFileChooser();

int returnVal = browse.showOpenDialog(browse);

}

else if(evt.getSource() == file)

{

try

{

String inputValue = JOptionPane.showInputDialog("Please enter file");

BufferedReader d = new BufferedReader(new FileReader(inputValue));

input.setText(d.readLine());

no.setText(d.readLine());

output.setText(d.readLine());

d.close();

inputs = 0;

StringTokenizer str = new StringTokenizer(input.getText(), " ");

while (str.hasMoreTokens())

{

inputs = Integer.parseInt(str.nextToken()) + inputs;

}

nos = 0;

StringTokenizer st = new StringTokenizer(no.getText(), " ");

while (st.hasMoreTokens())

{

nos = Integer.parseInt(st.nextToken()) + nos;

}

outputs = 0;

StringTokenizer s = new StringTokenizer(output.getText(), " ");

while (s.hasMoreTokens())

{

outputs = Integer.parseInt(s.nextToken()) + outputs;

}

}

catch(Exception e){}

}

}

/** Constructs */

public static void main (String args[])

{

/** Instance GUI */

JFrame frame = new browser();

frame.show();

}

}

and in a txt file enter this

486 464 646 4

49 74 9749 7

48 48 484

/* Line 1 = inputs

* Line 2 = nos

* Line 3 = Outputs */

save this then run java file. can import throug read file but not browsing any ideas and also haveing problems with txt filter. thank you

[3443 byte] By [thothoface321a] at [2007-10-3 11:03:57]
# 1
Please repost the code inside [code][/code] tags, and restate your question clearly. It's hard to tell exactly what your problem is.
hunter9000a at 2007-7-15 13:26:22 > top of Java-index,Java Essentials,Java Programming...
# 2

sorry, ok the actual problem is that there are two buttons on my gui, read value and browse

the read value gives the user a text box to enter the file location, ie, c:/input.txt and i click ok, the values get imported into the 3 fields on the gui

however, wen i use the browse button i can retrieve the file, but wen i click ok nothing happens, no data is imported from the exact same file. input.txt can be seen on my previous post. thank you

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

import java.util.*;

public class browser extends JFrame implements ActionListener, Serializable

{

private JTextField input;

private JTextField no;

private JTextField output;

private JButton browse;

private JButton file;

private int inputs, nos, outputs;

public browser()

{

input = new JTextField("");

no = new JTextField("");

output = new JTextField("");

browse = new JButton("Browse");

browse.addActionListener(this);

file = new JButton("Read from file");

file.addActionListener(this);

/** Add buttons to panel */

JPanel buttonsPanel = new JPanel(new FlowLayout());

buttonsPanel.add(browse);

buttonsPanel.add(file);

JPanel functionsPanel = new JPanel(new GridLayout(3,2,5,5));

functionsPanel.add(input);

functionsPanel.add(no);

functionsPanel.add(output);

/** puts on pane */

Container contentPane = this.getContentPane();

contentPane.setLayout(new GridLayout(5,3,2,5));

contentPane.add(buttonsPanel);

contentPane.add(functionsPanel);

this.pack();

this.setVisible(true);

}

public void actionPerformed(ActionEvent evt)

{

if(evt.getSource() == browse)

{

final JFileChooser browse = new JFileChooser();

int returnVal = browse.showOpenDialog(browse);

}

else if(evt.getSource() == file)

{

try

{

String inputValue = JOptionPane.showInputDialog("Please enter file");

BufferedReader d = new BufferedReader(new FileReader(inputValue));

input.setText(d.readLine());

no.setText(d.readLine());

output.setText(d.readLine());

d.close();

inputs = 0;

StringTokenizer str = new StringTokenizer(input.getText(), " ");

while (str.hasMoreTokens())

{

inputs = Integer.parseInt(str.nextToken()) + inputs;

}

nos = 0;

StringTokenizer st = new StringTokenizer(no.getText(), " ");

while (st.hasMoreTokens())

{

nos = Integer.parseInt(st.nextToken()) + nos;

}

outputs = 0;

StringTokenizer s = new StringTokenizer(output.getText(), " ");

while (s.hasMoreTokens())

{

outputs = Integer.parseInt(s.nextToken()) + outputs;

}

}

catch(Exception e){}

}

}

/** Constructs */

public static void main (String args[])

{

/** Instance GUI */

JFrame frame = new browser();

frame.show();

}

}

thothoface321a at 2007-7-15 13:26:22 > top of Java-index,Java Essentials,Java Programming...
# 3
any1 pls
thothoface321a at 2007-7-15 13:26:22 > top of Java-index,Java Essentials,Java Programming...
# 4

Take a look at this little example. It shows you how to use JFileChoosers, how to set the text in a JTextArea, how to add accelerators to JMenuItems and how to read a file in and display it in a JTextArea. It also shows the use of a KeyListener and a KeyAdapter. Some of the real fundamentals of what you are trying to do here.

In the future, be sure that you post swing-related questions in the swing forum.

Here you go:

import javax.swing.*;

import java.io.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import static javax.swing.JFileChooser.APPROVE_OPTION;

public class Browser extends JFrame {

private JTextArea jta;

private JScrollPane jsp;

private JMenuBar jmb;

private JMenu fileMenu;

private JMenuItem newItem;

private JMenuItem openItem;

private JMenuItem exitItem;

public Browser() {

jta = new JTextArea();

jsp = new JScrollPane(jta);

jmb = new JMenuBar();

fileMenu = new JMenu("File");

newItem = new JMenuItem("New");

openItem = new JMenuItem("Open");

openItem.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.CTRL_MASK));

exitItem = new JMenuItem("Exit");

fileMenu.add(newItem);

fileMenu.add(openItem);

fileMenu.addSeparator();

fileMenu.add(exitItem);

jmb.add(fileMenu);

this.getContentPane().add(jsp);

this.setJMenuBar(jmb);

this.pack();

this.setSize(500, 300);

this.setLocationRelativeTo(null);

this.setTitle("FileChooser Demo");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

newItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

newDocument();

}

});

openItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

openDocument();

}

});

exitItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

exitProg();

}

});

jta.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent evt) {

if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE)

exitProg();

}

});

}

private void newDocument() { jta.setText(""); }

private void openDocument() {

JFileChooser jfc = new JFileChooser();

jfc.setDialogTitle("Open a File");

int option = jfc.showOpenDialog(null);

if (option == APPROVE_OPTION) {

try {

FileInputStream fis = new FileInputStream(jfc.getSelectedFile());

InputStreamReader isr = new InputStreamReader(fis);

char[] text = new char[fis.available()]; // not a good idea.

isr.read(text, 0, fis.available()); // again, not a good idea.;

jta.setText(new String(text));

} catch (IOException e) { e.printStackTrace(); }

}

}

private void exitProg() {

System.exit(0);

}

public static void main(String[] argv) { new Browser().setVisible(true); }

}

Navy_Codera at 2007-7-15 13:26:22 > top of Java-index,Java Essentials,Java Programming...