Keine Ausgabe von JList bei drücken von Button auf die Konsole

ElDonRon

Mitglied
Hallo,

hab mir gerade ein Beispiel aus "Handbuch der Java Programmierung raus gesucht" um ein wenig mit JList herum zu experimentieren.

Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sap.mw.jco.*;

public class ViewerJList
extends JFrame
implements Constants, ActionListener
{
  //JTextArea resultField = new JTextArea();
  String[] resultTableColumn = {"Project Nummer", "Project Definition", "Description"};
  String[][] resultTableRow = new String[1000][3];
  JTable resultTable = new JTable(resultTableRow, resultTableColumn);
  JScrollPane resultTableScrollpane = new JScrollPane(resultTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  
  //Daten fuer die Liste in JList
  static final String[] DATA = {
    "PROJECT_DEFINITION", "DESCRIPTION", "MASK_ID", "RESPONSIBLE_NO", "APPLICANT_NO", "COMP_CODE", "BUS_AREA", "CONTROLLING_AREA", "PROFIT_CTR", "PROJECT_CURRENCY", "PROJECT_CURRENCY_ISO", "NETWORK_ASSIGNMENT", "START",
    "FINISH", "PLANT", "CALENDAR", "PLAN_BASIC", "PLAN_FCST", "TIME_UNIT", "TIME_UNIT_ISO", "NETWORK_PROFILE", "PROJECT_PROFILE", "PROJECT_STOCK", "OBJECTCLASS", "STATISTICAL", "TAXJURCODE", "PS_ZSCHM", "PS_SCHDPRF", "PSFM_PRF",
    "BP_PPROFIL"
  };
  
  JList liste;

  public ViewerJList(String name)
  {
    super(name);
    
    buildGui();    
    buildMenuBar();
  }
  
  
  public String buildGui()
  {
    JTextField usernameTextField, maxrowsTextField;
    JPasswordField passwordField;
    JList liste;
    JButton connectButton, getListButton, selectButton;
    JLabel usernameLabel, passwordLabel, statusLabel, indicatorLabel, maxrowsLabel;

    getContentPane().setLayout(null);

    //The username label
    usernameLabel = new JLabel("Username");
    usernameLabel.setLocation(12,12);
    usernameLabel.setSize(80,20);
    getContentPane().add(usernameLabel);
    
    //The password label
    usernameLabel = new JLabel("Password");
    usernameLabel.setLocation(12,40);
    usernameLabel.setSize(80,20);
    getContentPane().add(usernameLabel);
    
    //The status label
    statusLabel = new JLabel("Status");
    statusLabel.setLocation(12,68);
    statusLabel.setSize(80,20);
    getContentPane().add(statusLabel);
    
    //The indicator label
    indicatorLabel = new JLabel("Disconnected");
    indicatorLabel.setLocation(120,68);
    indicatorLabel.setSize(80,20);
    indicatorLabel.setForeground(Color.red);
    getContentPane().add(indicatorLabel);
    
    //The username field
    usernameTextField = new JTextField();
    usernameTextField.setLocation(120,12);
    usernameTextField.setSize(100,20);
    getContentPane().add(usernameTextField);
    
    //The password field
    passwordField = new JPasswordField();
    passwordField.setLocation(120,40);
    passwordField.setSize(100,20);
    getContentPane().add(passwordField);
    
    //The result table
    resultTableScrollpane.setLocation(282,12);
    resultTableScrollpane.setSize(600,430);
    getContentPane().add(resultTableScrollpane);

    //The connect button
    connectButton = new JButton("Connect");
    connectButton.setMnemonic('C');
    connectButton.setLocation(12, 96);
    connectButton.setSize(100,30);
    getContentPane().add(connectButton);
    
    //The getList button
    getListButton = new JButton("getList");
    getListButton.setMnemonic('G');
    getListButton.setLocation(12, 230);
    getListButton.setSize(100,30);
    getContentPane().add(getListButton);

    //JList
    liste = new JList(DATA);
    liste.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    liste.setLocation(12, 280);
    liste.setSize(200,70);
    JScrollPane listeScrollPane = new JScrollPane(liste, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    getContentPane().add(liste);
	

    //Select Button in JList
    selectButton = new JButton("Select");
    selectButton.addActionListener(this);
    selectButton.setLocation(12, 350);
    selectButton.setSize(100,30);
    getContentPane().add(selectButton);

    
    //The maxrows label
    maxrowsLabel = new JLabel("MAXROWS");
    maxrowsLabel.setLocation(12,200);
    maxrowsLabel.setSize(80,20);
    //maxrowsLabel.setForeground(Color.red);
    getContentPane().add(maxrowsLabel);
    
    //The maxrows field
    maxrowsTextField = new JTextField("20");
    maxrowsTextField.setLocation(100,200);
    maxrowsTextField.setSize(100,20);
    getContentPane().add(maxrowsTextField);
    return maxrowsTextField.getText();
  }
  
  
  
  public void buildMenuBar()
  {
    // Create menubar and menues
    JMenuBar menueBar = new JMenuBar();
    setJMenuBar(menueBar);
    JMenu fileMenu = new JMenu("File");
    JMenu sapMenu = new JMenu("SAP");
    JMenu helpMenu = new JMenu("Help");
    menueBar.add(fileMenu);
    menueBar.add(sapMenu);
    menueBar.add(helpMenu); 
    
    // Add actions to Menu
    JMenuItem connectAction = new JMenuItem("Connect");
    JMenuItem disconnectAction = new JMenuItem("Disconnect");
    JMenuItem exitAction = new JMenuItem("Exit");
    JMenuItem versionAction = new JMenuItem("Version");

    // Create shortcuts Alt+Key
    connectAction.setMnemonic('C');
    connectAction.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.ALT_DOWN_MASK));
    disconnectAction.setMnemonic('D');
    disconnectAction.setAccelerator(KeyStroke.getKeyStroke('D', InputEvent.ALT_DOWN_MASK));
    exitAction.setMnemonic('E');
    exitAction.setAccelerator(KeyStroke.getKeyStroke('E', InputEvent.ALT_DOWN_MASK));

    // Build menu
    fileMenu.add(exitAction);
    sapMenu.add(connectAction);
    sapMenu.addSeparator();
    sapMenu.add(disconnectAction);
    helpMenu.add(versionAction);
  }
  
  public void actionPerformed(ActionEvent Event)
  {
  	String cmd = Event.getActionCommand();
  	if (cmd.equals("Ausgabe")) {
  		System.out.println("-");
  		ListModel lm = liste.getModel();
  		int[] sel = liste.getSelectedIndices();
  		for (int i = 0; i < sel.length; i++) {
  			String value = (String)lm.getElementAt(sel[i]);
  			System.out.println(" " + value);
  		}
  	}
  }
}

Problem ist, dass ich beim drücken keine Ausgabe der selektierten Optionen aus JList auf die Konsole erhalte.

Ich hoffe man kann mir weiter helfen. Danke schon einmal vorab!
 
Wie wär's mal zu Anfang mit ein bisschen weniger Code? ;-)
Ein JButton und eine JList sollten ausreichen, um diesen Umgang mit einer JList zu experimentieren.

Dein Problem ist, dass du zwei Variablen
Code:
liste
hast. Und im ActionListener auf die Variable zugreifst, die nicht initialisiert ist. Die lokale Variable im Konstruktor ist überflüssig.
Fliegt da keine Exception?
 
Naja, ich will direkt etwas damit ausprobieren, von daher hab ich es in den Code mit eingebaut 😉

Zeile 38 war gemeint oder?

Hab ich raus genommen, leider ohne Effekt.
Exception schmeisst er ebenfalls keine.

Edit:

Nur damit klar ist, was ich vorhabe. Ich will JList eigentlich als Filter benutzen. D.H. die Werte, welche ich bei JList auswähle, sollen am Ende in der Tabelle, als Spalten angezeigt werden. Hierfür sollen die anderen Spalten auf Spaltengröße 0 gesetzt werden.

Aber vielleicht, gibt es ja auch einen anderen besseren Weg 🙂
 
Zuletzt bearbeitet:
Wieso kommen in letzter Zeit so viele daher die meinen das NullLayout benutzen zu müssen? Wer bringt euch diesen Blödsinn bitte bei???
Warum dein ActionListener nicht funktioniert: Du prüfst auf actionCommand="Ausgabe". Dein "selectButton" hat aber den Text "Select" und da du nirgends den ActionCommand anders gesetzt hast ist der auch "Select".
 
Und wenn du schon unbedingt das NullLayout verwenden willst kannst du Positionieren und Größe Bestimmen auch in einem Schritt machen.
Nämlich anstatt von
Java:
usernameLabel.setLocation(12,40);
usernameLabel.setSize(80,20);
kannst du
Java:
usernameLabel.setBounds(12, 40, 80, 20);
schreiben.

Mein Tipp: Benutz FormLayout von JGoodies (MigLayout is auch OK, find ich aber etwas umständlicher)
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben