J
jimgardener
hi
I created a gui app in which I have two jfilechooser widgets ,one to
select a file and other to select a different directory.I want the
selected filename and selected directory name to be displayed in a
textarea.If no selection is made ,i want to display an error message
instead.
I coded like this
class MyView extends JFrame {
private JFileChooser filechooser;
private JFileChooser dirchooser;
private JTextArea resultfield;
...//and many jpanels to contain these widgets above
//and ok,quit buttons
public MyView(MyModel model){
super("top frame");
model=model;
createAndAddAllWidgets();
}
public void createAndAddAllWidgets(){
...
filechooser=new JFileChooser("Select imagefile");
...
dirchooser=new JFileChooser();
dirchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
...
//similarly adds textarea and needed ok, quit buttons
}
public String getSelectedFile(){
//?
}
public String getSelectedFolder(){
//?
}
public void displayMessage(String msg){
resultfield.setText(msg);
}
public void addOKButtonListener(ActionListener okl){
okbtn.addActionListener(okl);
}
public void addQuitButtonListener(ActionListener qbl){
quitbtn.addActionListener(qbl);
}
}//end of MyView class
class MyController{
private MyModel model;
private MyView view;
public MyController(MyModel m,MyView v ){
model=m;
view=v;
view.addOKButtonListener(new OKButtonListener());
view.addQuitButtonListener(new QuitButtonListener());
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String fileselection=view.getSelectedFile();
String folderselection=view.getSelectedFolder();
String result=model.processSelections(fileselection,
folderselection);
view.displayResult(result);
}
}//end inner class
class QuitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
view.dispose();
}
}//end inner class
}//end of MyController class
class MyModel{
public String processSelections(String filename,String foldername){
String result="you selected file:"+filename+" you selected
folder:"+foldername;
return result;
}
}//end of MyModel class
What I couldn't figure out was how to code the logic in MyView's
getSelectedFile(),getSelectedFolder() methods..
I can get the selected file name string as ,
filechooser.getSelectedFile().getPath() and
the selected folder name as
dirchooser.getSelectedFile().getPath()
But,how should I deal with the situation when no file is selected or
no folder is selected?Should I return an empty string from the method ?
How can I display an error message if both happen at the same time?
I need to display the result from MyModel's processSelections() if
selections are made.I am not sure how I should do this.Should I define
a new Exception for these empty selection cases? or should I create
StringBuffer and append messages into it, and use it when I call
MyView's displayMessage() ?
If anyone can help me here ,it would be nice.
thanks,
jim
I created a gui app in which I have two jfilechooser widgets ,one to
select a file and other to select a different directory.I want the
selected filename and selected directory name to be displayed in a
textarea.If no selection is made ,i want to display an error message
instead.
I coded like this
class MyView extends JFrame {
private JFileChooser filechooser;
private JFileChooser dirchooser;
private JTextArea resultfield;
...//and many jpanels to contain these widgets above
//and ok,quit buttons
public MyView(MyModel model){
super("top frame");
model=model;
createAndAddAllWidgets();
}
public void createAndAddAllWidgets(){
...
filechooser=new JFileChooser("Select imagefile");
...
dirchooser=new JFileChooser();
dirchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
...
//similarly adds textarea and needed ok, quit buttons
}
public String getSelectedFile(){
//?
}
public String getSelectedFolder(){
//?
}
public void displayMessage(String msg){
resultfield.setText(msg);
}
public void addOKButtonListener(ActionListener okl){
okbtn.addActionListener(okl);
}
public void addQuitButtonListener(ActionListener qbl){
quitbtn.addActionListener(qbl);
}
}//end of MyView class
class MyController{
private MyModel model;
private MyView view;
public MyController(MyModel m,MyView v ){
model=m;
view=v;
view.addOKButtonListener(new OKButtonListener());
view.addQuitButtonListener(new QuitButtonListener());
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String fileselection=view.getSelectedFile();
String folderselection=view.getSelectedFolder();
String result=model.processSelections(fileselection,
folderselection);
view.displayResult(result);
}
}//end inner class
class QuitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
view.dispose();
}
}//end inner class
}//end of MyController class
class MyModel{
public String processSelections(String filename,String foldername){
String result="you selected file:"+filename+" you selected
folder:"+foldername;
return result;
}
}//end of MyModel class
What I couldn't figure out was how to code the logic in MyView's
getSelectedFile(),getSelectedFolder() methods..
I can get the selected file name string as ,
filechooser.getSelectedFile().getPath() and
the selected folder name as
dirchooser.getSelectedFile().getPath()
But,how should I deal with the situation when no file is selected or
no folder is selected?Should I return an empty string from the method ?
How can I display an error message if both happen at the same time?
I need to display the result from MyModel's processSelections() if
selections are made.I am not sure how I should do this.Should I define
a new Exception for these empty selection cases? or should I create
StringBuffer and append messages into it, and use it when I call
MyView's displayMessage() ?
If anyone can help me here ,it would be nice.
thanks,
jim