• Post Reply Bookmark Topic Watch Topic
  • New Topic

open file and display content

 
panku gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



package lfrm;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import static java.util.Collections.list;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class NewJFrame extends javax.swing.JFrame implements ActionListener{
JFileChooser Chooser;
JList list;

public NewJFrame() {
initComponents();
}







@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jLayeredPane1 = new javax.swing.JLayeredPane();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("browse");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});

jScrollPane1.setViewportView(jList1);

javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
jLayeredPane1.setLayout(jLayeredPane1Layout);
jLayeredPane1Layout.setHorizontalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jButton1)
.addGap(85, 85, 85)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE))
);
jLayeredPane1Layout.setVerticalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jButton1))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(91, Short.MAX_VALUE))
);
jLayeredPane1.setLayer(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1)
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
JFileChooser chooser = new JFileChooser();
final DefaultListModel listModel = new DefaultListModel();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File directory = new File(chooser.getSelectedFile().toString());
File[] files = directory.listFiles();
listModel.clear();
for (int index = 0; index < files.length; index++)
{
//System.out.println(files[index].toString());
listModel.addElement(files[index].getName());
}
} else {
System.out.println("No Selection ");
}


getContentPane().setLayout(new FlowLayout());
final JList list = new JList(listModel);
getContentPane().add(new JScrollPane(list));

final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuFileNew = new JMenuItem( "New" );

JMenuItem menuFileOpen = new JMenuItem( "Open..." );
JMenuItem menuFileSave = new JMenuItem( "Save" );
JMenuItem menuFileSaveAs = new JMenuItem( "Save As..." );
JMenuItem menuFileExit = new JMenuItem( "Exit" );
popupMenu.add(menuFileNew);
popupMenu.add(menuFileOpen);
popupMenu.add(menuFileSave);
popupMenu.add(menuFileSaveAs);

enableEvents( AWTEvent.MOUSE_EVENT_MASK );
menuFileNew.addActionListener( this );
menuFileOpen.addActionListener( this );
menuFileSave.addActionListener( this );
menuFileSaveAs.addActionListener( this );
menuFileExit.addActionListener( this );

list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me) // if right mouse button clicked
&& !list.isSelectionEmpty() // and list selection is not empty
&& list.locationToIndex(me.getPoint()) // and clicked point is
== list.getSelectedIndex()) {
// inside selected item bounds
popupMenu.show(list, me.getX(), me.getY());

String s = (String) list.getSelectedValue();
System.out.println("Value Selected: " + s);


}
}
});

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});

pack();

}

public void actionPerformed(ActionEvent e) {


if(e.getActionCommand() == "New"){

System.out.println("new click");

}else if(e.getActionCommand() == "Save"){

System.out.println("Save click");

}else if(e.getActionCommand() == "Open..."){


System.out.println("Open click");


}


else if(e.getActionCommand() == "Save As..."){

System.out.println("Save As. click");

}
}


private void readFileToList(File file) {
DefaultListModel listModel = (DefaultListModel)list.getModel();

try {
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(file)));
String line;
while((line = br.readLine()) != null) {
listModel.addElement(line);
}
br.close();
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}

//GEN-LAST:event_jButton1MouseClicked

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);

}

});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables





}

Hi ,my problem in this code when file choose after that select file in jlist but when open , save , save as used context menu indivisual select file but not open. i dont know how to used condition or event for that please run this code and solve my problem..
Thanks.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last thread, which you haven't returned to, I told you about the code tags and also asked you to post a SSCCE for better help. This code still contains several unused imports, and probably a lot of code that is unrelated to the present question.

I say 'probably' because that's too much code to go through. Try making a SSCCE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!