//package dialog;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;
/*
* FileChooserDemo.java is a 1.4 application that uses these files:
* images/Open16.gif
* images/Save16.gif
*/
public class FileChooserDialog
{
public static void OpenDialog()
{
JFileChooser fc = new JFileChooser();
JDialog.setDefaultLookAndFeelDecorated(true);
fc.setFileFilter( new FileFilter() {
public boolean accept( File f ) {
return f.isDirectory() ||
f.getName().toLowerCase().endsWith(".txt");
}
public String getDescription() {
return ".txt files only!";
}
} );
int returnValue = fc.showOpenDialog( null );
if ( returnValue == JFileChooser.APPROVE_OPTION )
{
File f = fc.getSelectedFile();
String datei = f.getName();
// einlesenVomFile.
}
}//Ende Methode
public static void SaveDialog()
{
JFileChooser fc = new JFileChooser();
JDialog.setDefaultLookAndFeelDecorated(true);
fc.setFileFilter( new FileFilter() {
public boolean accept( File f ) {
return f.isDirectory() ||
f.getName().toLowerCase().endsWith(".txt");
}
public String getDescription() {
return ".txt files only!";
}
} );
int returnValue = fc.showSaveDialog( null );
if ( returnValue == JFileChooser.APPROVE_OPTION )
{
File file = fc.getSelectedFile();
//DateiSchreiben schreiber = new DateiSchreiben();
//schreiber.dateiZeilenweiseSchreiben(file.getAbsolutePath(),daten);
}
} }
-------------------------------------------------------------------------------------------------------------------------------
import dialog.FileChooserDialog;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
public class tabbedpane extends JPanel {
String datei = "Beispiel.txt"; //Da muss der Filename aus FileChooserDialog übergeben werden!
String[] counter = {"A,B,C","D,E,F","G,H,I","J,K,L","M,N,O","P,Q,R,S","T,U,V","W,X,Y,Z"};
JList [] swingliste = new JList[counter.length];
JList selectedListe = null;
String selectedName = null;
int selectedIndex = 0;
/*
void update(){
// nun alle Karteikarten updaten
for(int i = 0; i < counter.length; i++){
((JListData)swingliste[i].getModel()).update(counter[i]);
}
}
void nameLoeschen(){
}
void nameEinfuegen(String nameNeu){
}
void nameAendern(String nameNeu){
}
String getSelectedName(){
return selectedName;
}
*/
public tabbedpane() {
super(new GridLayout(1, 1));
JPanel[] panel = new JPanel[counter.length];
ImageIcon icon = createImageIcon("/middle.gif");
JTabbedPane tabpane;
tabpane = new JTabbedPane();
for(int i = 0; i < counter.length; i++){
panel[i] = makeTextPanel(counter[i], i);
tabpane.addTab(counter[i], icon, panel[i], "tool tip");
}
//Add the tabbed pane to this panel.
add(tabpane);
//Uncomment the following line to use scrolling tabs.
tabpane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
/*
/**
* String text : ist der Text der Karteikarte, also "ABC"
*
*/
protected JPanel makeTextPanel(String text, int index) {
JPanel panel = new JPanel(false);
panel.setLayout(new GridLayout(1, 1));
// Listbox mit AbstractListModel ------------------------
JListData dataModel = new JListData(this, text);
JList liste = new JList(dataModel);
// ------------------------------------------------------
swingliste[index] =liste; // Referenz auf Liste merken
// -------------------------------------------------------
liste.setLayoutOrientation(JList.VERTICAL);
liste.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
liste.setSelectedIndex(0);
liste.setVisibleRowCount(-1);
JScrollPane scrollPane = new JScrollPane(liste);
scrollPane.setPreferredSize(new Dimension(450, 260));
liste.addListSelectionListener(new ListSelectionAdapter(liste));
panel.add(scrollPane);
return panel;
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = tabbedpane.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
class ListSelectionAdapter implements ListSelectionListener{
JList meineListe;
public ListSelectionAdapter(JList liste){
meineListe = liste;
}
public void valueChanged( ListSelectionEvent ev ) {
System.out.println("Test: valueChanged");
if(ev.getValueIsAdjusting() == false){
// Name merken
selectedName = (String)meineListe.getSelectedValue();
System.out.println("selected Item= " + selectedName);
// Index merken
selectedIndex = meineListe.getSelectedIndex();
System.out.println("selected Index= " + selectedIndex);
// Liste merken
selectedListe = meineListe;
}
}
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
_______________________________________________________________________
//Hier die JList
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
class JListData extends AbstractListModel{
private Vector dlist; // Datenspeicher
private tabbedpane fenster;
public JListData(tabbedpane f, String text){
fenster = f;
dlist = new Vector();
makeData(text);
}
/*
* String text : ist der Text der Karteikarte,
* also "ABC" oder nur "A"
*
*/
private Vector makeData(String text){
// einlesen der Daten -------------
Vector data = einlesenVomFile(text);
// Sortierung ---------------------
Object[] arrayNames = data.toArray();
Arrays.sort(arrayNames);
// und sortiert in Datenspeicher ablegen
for(int i = 0; i < arrayNames.length; i++){
dlist.addElement( (String) arrayNames[i]);
}
// sortierten Datenspeicher zurückgeben
return dlist;
}
public void update(String text){
// alte Werte löschen
removeAllElements();
// Werte neu
makeData(text);
fireContentsChanged(this, 0, dlist.size());
}
public int getSize(){
return dlist.size();
}
public Object getElementAt(int index){
return dlist.elementAt(index);
}
public void removeAllElements(){
dlist.removeAllElements();
fireIntervalRemoved(this, 0, dlist.size());
}
public void removeElementAt(int index){
}
public void addElement(String s){
System.out.println("added " + s);
dlist.addElement(s);
fireIntervalAdded(this, dlist.size()-1, dlist.size());
}
public void changeElement(int index, String s){
}
private Vector einlesenVomFile(String text){
BufferedReader in;
String
zeile,dateiname = fenster.datei;
Vector data = new Vector(); // Speicher anlegen
try {
// Stream öffnen
in = new BufferedReader(new FileReader( new File(dateiname) ));
// Stream zeilenweise lesen
while( (zeile = in.readLine()) != null ){
for(int j = 0; j < text.length();j++){
// nur Zeilen übernehmen, die der karteikarte entsprechen
if( zeile.substring(0,1).equals(text.substring(j,j+1)))
data.addElement(zeile);
}
}
in.close(); // Stream schließen
}
catch(IOException exp){
System.out.println("Dateifehler beim Lesen" + exp.getMessage());
}
// Rückgabe des Vectors unsortiert
return data;
}
}// end of class JListData
________________________________________________________________________