Moin,
ich habe ein JTree initial, möchte nach Entfernen aller Items den Baum neu anlegen und anzeigen. Das Anlegen scheint zu funktionieren, hat jemand eine Idee, warum ich den geänderten JTree nicht angezeigt bekomme?
Der aufgerufene Code befindet sich hinter dem "Gruppieren"-Button (erst "Zufallsverteilung"ausführen) und der Methode groupPersons(...) im MyTableModel.
ich habe ein JTree initial, möchte nach Entfernen aller Items den Baum neu anlegen und anzeigen. Das Anlegen scheint zu funktionieren, hat jemand eine Idee, warum ich den geänderten JTree nicht angezeigt bekomme?
Der aufgerufene Code befindet sich hinter dem "Gruppieren"-Button (erst "Zufallsverteilung"ausführen) und der Methode groupPersons(...) im MyTableModel.
Code:
package jTreeTests;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* @author Harald
*
*/
public class MyTree extends JTree
{
/**
* Constructor
*
*/
public MyTree()
{
super();
}
/**
* Constructor
*
* @param root
*/
public MyTree(DefaultMutableTreeNode root)
{
super(root);
}
}
Code:
/*
* Created on 09.02.2005
*
*/
package jTreeTests;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* @author Harald
*
*/
public class MyTableModel extends DefaultTableModel
{
private String[] columnNames = new String[]
{"Rangnummer", "Name", "Vorname", "Zufallsnummer", "Gruppe"};
private Class[] classTypes =
{
Integer.class,
String.class,
String.class,
Integer.class,
Integer.class
};
private MyTree m_Tree;
private Jugendfreizeit m_gui;
/**
*
*/
public MyTableModel(MyTree myTree, Jugendfreizeit gui)
{
super();
m_Tree = myTree;
m_gui = gui;
}
public String getColumnName(int columnIndex)
{
return columnNames[columnIndex];
}
public int getColumnCount()
{
if (columnNames == null)
columnNames = new String[]
{"Anfangsnummer", "Name", "Vorname", "Zufallsnummer"};
return columnNames.length;
}
public int getRowCount()
{
int rows = getRows().size();
if (rows == 0)
{
addRow(new MyTableRow("Meiser", "Hans", 0));
addRow(new MyTableRow("Zwerg", "Eva", 1));
addRow(new MyTableRow("Mustermann", "Erika", 2));
addRow(new MyTableRow("Muster", "Frank", 3));
addRow(new MyTableRow("Meier", "Peter", 4));
addRow(new MyTableRow("Schmitz", "Petra", 5));
addRow(new MyTableRow("Schulz", "Nadine", 6));
}
return getRows().size();
}
public Class getColumnClass(int columnIndex)
{
return classTypes[ columnIndex ];
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return
((MyTableRow) getRows().get(rowIndex)).isCellEditable(columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex)
{
Object objRet = null;
try
{
objRet =
((MyTableRow) getRows().get(rowIndex)).getValueAt(columnIndex);
}
catch (Exception except)
{
}
return (objRet);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
{
((MyTableRow)getRows().get(rowIndex)).setValue(aValue, columnIndex);
}
/**
* Liefert eine Zeile zurück
* @param row der Index der Zeile
* @return die Zeile
*/
private MyTableRow getRow(int row)
{
return (MyTableRow) getRows().get(row);
}
/**
* Die Zeilen des Modells
*/
private List rows;
/**
* @return
*/
public List getRows()
{
if (rows == null)
{
rows = new ArrayList();
}
return rows;
}
/**
* Fügt eine neue Tabellenzeile hinzu
*
* @param newRow
*/
public void addRow(MyTableRow newRow)
{
getRows().add(newRow);
fireTableDataChanged();
}
/**
*
*/
public void randomizePersons()
{
HashSet set = new HashSet();
Random rnd = new Random();
int rangeStart = 0;
int rangeEnd = getRows().size();
for (int i = 0; set.size() < rangeEnd; i++)
{
MyTableRow tempRow = (MyTableRow)getRows().get( i );
Integer newInt = new Integer(nextInt(rangeStart, rangeEnd, rnd));
if ( !set.contains(newInt) && newInt.intValue() != 0)
{
tempRow.setZufallsNummer(newInt.intValue());
set.add(newInt);
System.out.println("Zufallsnummer lautet: " + newInt);
}
else
{
i--;
}
}
fireTableDataChanged();
}
private int nextInt(int rangeStart, int rangeEnd, Random random)
{
int iNextInt = random.nextInt() % (rangeEnd + 1);
if (iNextInt < 0)
iNextInt *= -1;
// int iNextInt = ((rangeStart - 1) + random.nextInt()) % (rangeEnd + 1);
return iNextInt;
}
/**
*
*/
public void groupPersons(int groupSize)
{
int iRowCount = getRows().size();
int iGroupSize = groupSize;
int iGroupsCount = (int)Math.ceil((double)iRowCount / (double)iGroupSize);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Gruppen");
for (int j = 0; j < iGroupsCount; j++)
{
root.add(new DefaultMutableTreeNode("Gruppe " + (j+1)));
}
m_Tree = new MyTree(root);
m_gui.updateTreeView(m_Tree);
for ( int i = 0; i < iRowCount; i++ )
{
MyTableRow currentRow = (MyTableRow)getRows().get( i );
int iRandomNumber = currentRow.getZufallsNummer();
int iCurrentGroup = (int)Math.ceil((double)iRandomNumber / (double)iGroupSize);
currentRow.setGruppenNummer( iCurrentGroup );
}
fireTableDataChanged();
}
}
Code:
/*
* Created on 09.02.2005
*
*/
package jTreeTests;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* @author Harald
*
*/
public class Jugendfreizeit extends JFrame
{
JScrollPane scrollPane = new JScrollPane();
JScrollPane scrTree = new JScrollPane();
JLabel jlGroupSize = new JLabel("Gruppengröße");
JPanel jpTable = new JPanel();
JPanel jpTree = new JPanel();
JTable mainTable;
MyTree groupTree = new MyTree();
JButton jbRandomize = new JButton("Zufallsverteilung");
JButton jbGroup = new JButton("Gruppieren");
JButton jbClose = new JButton("Beenden");
JButton jbAdd = new JButton("Hinzufügen");
JSpinner spinner;
SpinnerNumberModel spinnerModel;
GridBagLayout gblMain = new GridBagLayout();
MyTableModel myTableModel;
//--------------------------------------------------------
//--------------------------------------------------------
/** TODO: Tree, Sortierung */
//--------------------------------------------------------
//--------------------------------------------------------
/**
*
*/
public Jugendfreizeit()
{
super( "Jugendfreizeit"); // Set the title of the window.
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(640, 480);
try
{
initGUI();
}
catch(Exception e)
{
e.printStackTrace();
}
setVisible(true);
}
/**
*
*/
private void initGUI()
{
jbGroup.setEnabled(false);
myTableModel = new MyTableModel(groupTree, this);
mainTable = new SortedTable(myTableModel);
spinnerModel = new SpinnerNumberModel( 1, 1, myTableModel.getRowCount(), 1);
spinner = new JSpinner( spinnerModel );
spinner.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
int iRowCount = myTableModel.getRowCount();
int iCurrentSpinnerValue = ((Integer)spinner.getValue()).intValue();
if ( iCurrentSpinnerValue > iRowCount )
{
spinner.setValue(new Integer(iRowCount));
}
}
});
jbGroup.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
myTableModel.groupPersons(((Integer)spinner.getValue()).intValue());
}
});
jbRandomize.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
myTableModel.randomizePersons();
jbGroup.setEnabled(true);
}
});
jbClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
jbAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
myTableModel.addRow(new MyTableRow("", "", myTableModel.getRowCount()));
spinnerModel.setMaximum(new Integer(myTableModel.getRowCount()));
}
});
mainTable.setModel(myTableModel);
Container contentPane = getContentPane();
contentPane.setLayout(gblMain);
scrollPane.getViewport().add(mainTable);
jpTable.add(scrollPane);
scrTree.add(groupTree);
jpTree.add(scrTree);
contentPane.add(jpTable, new GridBagConstraints(0, 0, 3, 1, 1.0, 1.0
,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jpTree, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jbGroup, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jbRandomize, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jbAdd, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jbClose, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(jlGroupSize, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
contentPane.add(spinner, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
}
public static void main( String[] args )
{
Jugendfreizeit jugendfreizeit = new Jugendfreizeit();
}
public void updateTreeView(MyTree newTree)
{
groupTree = newTree;
repaint();
}
}