Hallo Leute,
ich habe folgendes Problem und zwar habe ich eine GUI gebastelt, die anschließend als Client eingesetzt werden soll. Ich habe zwei Buttons (Auswählen und Auswählen 2) mit denen ich den FileChooser öffne und eine Datei Auswählen kann. Bei "Auswählen" wird der Dateiname einfach nur in eine TextArea geschrieben und dort angezeit und bei "Auswählen 2" kann ich mehrere Dateinamen in eine JList schreiben, die dann untereinander aufgelistet werden.
Jetzt habe ich aber den Wunsch Einträge aus der Liste zu löschen und alle Inhalte aufeinmal aus der Liste zu werfen (einschließlich aus der JTextArea von "Auswählen")
Hier ist mein bisheriger Quellcode:
[HIGHLIGHT="Java"]import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import javax.swing.text.JTextComponent;
public class TVorklassifikator extends JFrame implements ActionListener
{
private JList nameList;
private DefaultListModel nameListModel;
//private Vector<String> nameListModel;
private JTextComponent file1;
public TVorklassifikator()
{
super("TVorklassifikator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints constraints = new GridBagConstraints();
setLayout(new GridBagLayout());
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 2;
constraints.insets = new Insets(0, 2, 0, 2);
Label header = new Label("TVorklassifikator");
header.setFont(new Font("Arial",Font.BOLD, 16));
add(header, constraints);
constraints.gridheight = 4;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JButton choose1 = new JButton("Auswählen");
choose1.addActionListener(this);
add(choose1, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JLabel labelFile = new JLabel("Datei 1:");
add(labelFile, constraints);
constraints.gridheight = 8;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.insets = new Insets(5, 5, 5, 5);
file1 = new JTextField(" ");
add(file1, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
JButton choose2 = new JButton("Auswählen 2");
choose2.addActionListener(this);
add(choose2, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
JLabel labelFile2 = new JLabel("Datei 2:");
add(labelFile2, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
nameListModel = new DefaultListModel();
nameList = new JList(nameListModel);
//nameListModel = new Vector<String>();
JScrollPane fileName = new JScrollPane(nameList);
add(fileName, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5,5,5,5);
JButton delete = new JButton("Eintrag entfernen");
delete.addActionListener(this);
add(delete,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.SOUTH;
constraints.insets = new Insets(5, 10, 5, 10);
JButton end = new JButton("Ende");
end.addActionListener(this);
add(end, constraints);
setSize(330, 450);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
System.out.println("cmd: " + cmd);
if(cmd.equals("Auswählen"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setFileFilter(new MyFileFilter());
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f = fc.getSelectedFile();
file1.setText(f.getName());
}
}
if(cmd.equals("Auswählen 2"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setFileFilter(new MyFileFilter());
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f2 = fc.getSelectedFile();
nameListModel.addElement(f2.getName());
// nameListModel.add(f2.getName());
// nameList.setListData(nameListModel);
}
}
if(cmd.equals("Ende"))
{
int n = JOptionPane.showConfirmDialog(
this, "Anwedung wirklich beenden?", "Ende",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(n == JOptionPane.YES_OPTION)
System.exit(0);
}
if(cmd.equals("Eintrag entfernen"))
{
}
}
public class MyFileFilter extends FileFilter
{
public boolean accept(File file)
{
if(file.isDirectory())
return true;
String name = file.getName();
if(name.endsWith(".xml"))
return true;
else
return false;
}
public String getDescription()
{
return "Text file(*.xml)";
}
}
public static void main(String[] args)
{
new TVorklassifikator();
}
}[/HIGHLIGHT]
Hoffentlich könnte ihr mir den nötigen Ansatz hier reinposten, dass wäre echt super ich weiß nämlich echt gerade nicht was sache ist.
Ich bedanke mich schonmal im voraus
ich habe folgendes Problem und zwar habe ich eine GUI gebastelt, die anschließend als Client eingesetzt werden soll. Ich habe zwei Buttons (Auswählen und Auswählen 2) mit denen ich den FileChooser öffne und eine Datei Auswählen kann. Bei "Auswählen" wird der Dateiname einfach nur in eine TextArea geschrieben und dort angezeit und bei "Auswählen 2" kann ich mehrere Dateinamen in eine JList schreiben, die dann untereinander aufgelistet werden.
Jetzt habe ich aber den Wunsch Einträge aus der Liste zu löschen und alle Inhalte aufeinmal aus der Liste zu werfen (einschließlich aus der JTextArea von "Auswählen")
Hier ist mein bisheriger Quellcode:
[HIGHLIGHT="Java"]import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import javax.swing.text.JTextComponent;
public class TVorklassifikator extends JFrame implements ActionListener
{
private JList nameList;
private DefaultListModel nameListModel;
//private Vector<String> nameListModel;
private JTextComponent file1;
public TVorklassifikator()
{
super("TVorklassifikator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints constraints = new GridBagConstraints();
setLayout(new GridBagLayout());
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 2;
constraints.insets = new Insets(0, 2, 0, 2);
Label header = new Label("TVorklassifikator");
header.setFont(new Font("Arial",Font.BOLD, 16));
add(header, constraints);
constraints.gridheight = 4;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JButton choose1 = new JButton("Auswählen");
choose1.addActionListener(this);
add(choose1, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JLabel labelFile = new JLabel("Datei 1:");
add(labelFile, constraints);
constraints.gridheight = 8;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.insets = new Insets(5, 5, 5, 5);
file1 = new JTextField(" ");
add(file1, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
JButton choose2 = new JButton("Auswählen 2");
choose2.addActionListener(this);
add(choose2, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
JLabel labelFile2 = new JLabel("Datei 2:");
add(labelFile2, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
nameListModel = new DefaultListModel();
nameList = new JList(nameListModel);
//nameListModel = new Vector<String>();
JScrollPane fileName = new JScrollPane(nameList);
add(fileName, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5,5,5,5);
JButton delete = new JButton("Eintrag entfernen");
delete.addActionListener(this);
add(delete,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.SOUTH;
constraints.insets = new Insets(5, 10, 5, 10);
JButton end = new JButton("Ende");
end.addActionListener(this);
add(end, constraints);
setSize(330, 450);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
System.out.println("cmd: " + cmd);
if(cmd.equals("Auswählen"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setFileFilter(new MyFileFilter());
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f = fc.getSelectedFile();
file1.setText(f.getName());
}
}
if(cmd.equals("Auswählen 2"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setFileFilter(new MyFileFilter());
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f2 = fc.getSelectedFile();
nameListModel.addElement(f2.getName());
// nameListModel.add(f2.getName());
// nameList.setListData(nameListModel);
}
}
if(cmd.equals("Ende"))
{
int n = JOptionPane.showConfirmDialog(
this, "Anwedung wirklich beenden?", "Ende",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(n == JOptionPane.YES_OPTION)
System.exit(0);
}
if(cmd.equals("Eintrag entfernen"))
{
}
}
public class MyFileFilter extends FileFilter
{
public boolean accept(File file)
{
if(file.isDirectory())
return true;
String name = file.getName();
if(name.endsWith(".xml"))
return true;
else
return false;
}
public String getDescription()
{
return "Text file(*.xml)";
}
}
public static void main(String[] args)
{
new TVorklassifikator();
}
}[/HIGHLIGHT]
Hoffentlich könnte ihr mir den nötigen Ansatz hier reinposten, dass wäre echt super ich weiß nämlich echt gerade nicht was sache ist.
Ich bedanke mich schonmal im voraus