Link to home
Create AccountLog in
Avatar of perdoname_
perdoname_

asked on

sorting jlist

Hello Experts,

I want to make use of a program i found from a sun tutorial but unfortunately it returns a couple of errors.
The program actually implements a jlist which let the possibility to the user to add more elements to the list , or delete, and present the elements in their ascending , descending and unsorted form.
The errors i got, belong to the first class "Sorting".

orting.java:11: incompatible types
found   : SortingModels
required: Sorting
            sortedModel = new SortingModels(unsortedModel);
                              ^
Sorting.java:54: setModel(javax.swing.ListModel) in javax.swing.JList cannot be applied to (Sorting)
            list.setModel(sortedModel);
                    ^
Sorting.java:118: package org.jdesktop.layout does not exist
            org.jdesktop.layout.GroupLayout panelSortLayout = new org.jdesktop.layout.GroupLayout(panelSort);
                                   ^
Sorting.java:118: package org.jdesktop.layout does not exist
            org.jdesktop.layout.GroupLayout panelSortLayout = new org.jdesktop.layout.GroupLayout(panelSort);
                                                                                         ^
Sorting.java:132: package org.jdesktop.layout does not exist
                                                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
                                                                                                    ^
Sorting.java:128: package org.jdesktop.layout does not exist
                                    .add(panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                                                            ^
Sorting.java:124: package org.jdesktop.layout does not exist
                        panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                                       ^
Sorting.java:146: package org.jdesktop.layout does not exist
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                                    ^
Sorting.java:142: package org.jdesktop.layout does not exist
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                                    ^
Sorting.java:136: package org.jdesktop.layout does not exist
                        panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                                       ^
Sorting.java:154: package org.jdesktop.layout does not exist
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
                                   ^
Sorting.java:154: package org.jdesktop.layout does not exist
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
                                                                                ^
Sorting.java:182: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                                                                                                        ^
Sorting.java:182: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                                                                                                                                                                     ^
Sorting.java:182: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                                                                                                                                                                                                                   ^
Sorting.java:180: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                                                        ^
Sorting.java:180: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                                                                                                              ^
Sorting.java:180: package org.jdesktop.layout does not exist
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                                                                                                                                                            ^
Sorting.java:178: package org.jdesktop.layout does not exist
                                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                                                                                                   ^
Sorting.java:176: package org.jdesktop.layout does not exist
                                                            .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                   ^
Sorting.java:176: package org.jdesktop.layout does not exist
                                                            .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                                                                   ^
Sorting.java:176: package org.jdesktop.layout does not exist
                                                            .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                                                                                                                 ^
Sorting.java:174: package org.jdesktop.layout does not exist
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                                                                   ^
Sorting.java:172: package org.jdesktop.layout does not exist
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                                                    ^
Sorting.java:170: package org.jdesktop.layout does not exist
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                                                        ^
Sorting.java:170: package org.jdesktop.layout does not exist
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                                                                                                             ^
Sorting.java:170: package org.jdesktop.layout does not exist
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                                                                                                                                                           ^
Sorting.java:168: package org.jdesktop.layout does not exist
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, txtAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                                                                        ^
Sorting.java:168: package org.jdesktop.layout does not exist
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, txtAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                                                                                                                              ^
Sorting.java:166: package org.jdesktop.layout does not exist
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                                                                   ^
Sorting.java:160: package org.jdesktop.layout does not exist
                        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                              ^
Sorting.java:210: package org.jdesktop.layout does not exist
                                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                                                                    ^
Sorting.java:206: package org.jdesktop.layout does not exist
                                                            .add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                     ^
Sorting.java:206: package org.jdesktop.layout does not exist
                                                            .add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                                                                     ^
Sorting.java:206: package org.jdesktop.layout does not exist
                                                            .add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                                                                                                                                                                   ^
Sorting.java:204: package org.jdesktop.layout does not exist
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                                                                                                   ^
Sorting.java:202: package org.jdesktop.layout does not exist
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                                                                    ^
Sorting.java:200: package org.jdesktop.layout does not exist
                                                .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                                                                   ^
Sorting.java:200: package org.jdesktop.layout does not exist
                                                .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                                                                                                                   ^
Sorting.java:200: package org.jdesktop.layout does not exist
                                                .add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                                                                                                                                                                 ^
Sorting.java:198: package org.jdesktop.layout does not exist
                                                .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                                    ^
Sorting.java:198: package org.jdesktop.layout does not exist
                                                .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                                                                                  ^
Sorting.java:196: package org.jdesktop.layout does not exist
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                                                   ^
Sorting.java:190: package org.jdesktop.layout does not exist
                        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                                              ^
Sorting.java:232: cannot find symbol
symbol  : method toUnsortedModelIndex(int)
location: class Sorting
                  int unsortedIndex = sortedModel.toUnsortedModelIndex(sortedIndex);
                                                       ^
Sorting.java:262: cannot find symbol
symbol  : variable SortOrder
location: class Sorting
            sortedModel.setSortOrder(SortOrder.UNORDERED);
                                         ^
Sorting.java:268: cannot find symbol
symbol  : variable SortOrder
location: class Sorting
            sortedModel.setSortOrder(SortOrder.DESCENDING);
                                         ^
Sorting.java:273: cannot find symbol
symbol  : variable SortOrder
location: class Sorting
            sortedModel.setSortOrder(SortOrder.ASCENDING);
                                         ^
Sorting.java:283: cannot find symbol
symbol  : method toUnsortedModelIndices(int[])
location: class Sorting
            int[] unsortedSelection = sortedModel.toUnsortedModelIndices(sortedSelection);
                   




If anyone knows how can this code can be workable it'd be grateful !

Thanks in advance
import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
 
public class Sorting extends javax.swing.JFrame {
 
	public Sorting() {
		super("GUI!");
		unsortedModel = new DefaultListModel();
		sortedModel = new SortingModels(unsortedModel);
		initComponents();
 
	}
 
	private void initComponents() {
		bgSortOrder = new javax.swing.ButtonGroup();
		txtAddEntry = new javax.swing.JTextField();
		btnAddEntry = new javax.swing.JButton();
		scrollPane = new javax.swing.JScrollPane();
		list = new javax.swing.JList();
		btnDeleteSelection = new javax.swing.JButton();
		panelSort = new javax.swing.JPanel();
		rbAscendingSort = new javax.swing.JRadioButton();
		rbDescendingSort = new javax.swing.JRadioButton();
		rbUnordered = new javax.swing.JRadioButton();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		txtAddEntry.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				txtAddEntryKeyPressed(evt);
			}
 
		});
 
 
 
		btnAddEntry.setText("Add");
 
		btnAddEntry.addActionListener(new java.awt.event.ActionListener() {
 
			public void actionPerformed(java.awt.event.ActionEvent evt) {
 
				btnAddEntryActionPerformed(evt);
 
			}
 
		});
 
 
 
		scrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("JList with SortedListModel"));
		scrollPane.setMinimumSize(new java.awt.Dimension(0, 0));
		scrollPane.setPreferredSize(new java.awt.Dimension(250, 150));
		list.setModel(sortedModel);
 
		list.addKeyListener(new java.awt.event.KeyAdapter() {
 
			public void keyPressed(java.awt.event.KeyEvent evt) {
 
				listKeyPressed(evt);
 
			}
 
		});
 
		list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				listValueChanged(evt);
			}
 
		});
 
		scrollPane.setViewportView(list);
		btnDeleteSelection.setText("Delete");
		btnDeleteSelection.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDeleteSelectionActionPerformed(evt);
			}
 
		});
 
 
 
		panelSort.setBorder(javax.swing.BorderFactory.createTitledBorder("Sort Order"));
		bgSortOrder.add(rbAscendingSort);
		rbAscendingSort.setSelected(true);
		rbAscendingSort.setText("Ascending");
		rbAscendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbAscendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbAscendingSort.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbAscendingSortActionPerformed(evt);
			}
		});
 
		bgSortOrder.add(rbDescendingSort);
		rbDescendingSort.setText("Descending");
		rbDescendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbDescendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbDescendingSort.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbDescendingSortActionPerformed(evt);
			}
 
		});
 
		bgSortOrder.add(rbUnordered);
		rbUnordered.setText("Unordered");
		rbUnordered.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbUnordered.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbUnordered.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbUnorderedActionPerformed(evt);
			}
 
		});
 
		org.jdesktop.layout.GroupLayout panelSortLayout = new org.jdesktop.layout.GroupLayout(panelSort);
 
		panelSort.setLayout(panelSortLayout);
 
		panelSortLayout.setHorizontalGroup(
 
				panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(panelSortLayout.createSequentialGroup()
 
						.add(panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(rbAscendingSort)
								.add(rbDescendingSort)
								.add(rbUnordered))
								.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
 
		panelSortLayout.setVerticalGroup(
 
				panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(panelSortLayout.createSequentialGroup()
 
						.add(rbAscendingSort)
 
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
						.add(rbDescendingSort)
 
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
						.add(rbUnordered))
 
		);
 
 
 
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
 
		getContentPane().setLayout(layout);
 
		layout.setHorizontalGroup(
 
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(layout.createSequentialGroup()
 
						.addContainerGap()
 
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
 
								.add(org.jdesktop.layout.GroupLayout.LEADING, txtAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
 
								.add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
										.add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
 
										.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
 
												.add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
												.add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
 
												.addContainerGap())
 
		);
 
		layout.setVerticalGroup(
 
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(layout.createSequentialGroup()
 
						.addContainerGap()
 
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
								.add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
								.add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
 
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
 
										.add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
 
										.add(btnAddEntry))
 
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
										.add(btnDeleteSelection)
 
										.addContainerGap())
 
		);
 
		pack();
 
	}// </editor-fold>//GEN-END:initComponents
 
 
 
	private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
 
		int sortedFirstIndex = evt.getFirstIndex();
 
		int sortedLastIndex = evt.getLastIndex();
 
		for (int sortedIndex = sortedFirstIndex; sortedIndex <= sortedLastIndex; sortedIndex++) {
 
			int unsortedIndex = sortedModel.toUnsortedModelIndex(sortedIndex);
 
			// do something with unsorted model index
 
			System.out.printf("listValueChanged index: %d (sorted) %d (unsorted)\n", 
 
					sortedIndex, unsortedIndex);
 
		}
 
	}
	private void listKeyPressed(java.awt.event.KeyEvent evt) {
 
		int key = evt.getKeyCode();
 
		if (key == evt.VK_DELETE) {
 
			int option = JOptionPane.showConfirmDialog(this, "Delete selected elements?");
 
			if (option == JOptionPane.YES_OPTION) {
 
				btnDeleteSelectionActionPerformed(null);
 
			}
 
		}
 
	}
 
	private void rbUnorderedActionPerformed(java.awt.event.ActionEvent evt) {
		sortedModel.setSortOrder(SortOrder.UNORDERED);
 
	}
 
	private void rbDescendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDescendingSortActionPerformed
 
		sortedModel.setSortOrder(SortOrder.DESCENDING);
 
	}
	private void rbAscendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAscendingSortActionPerformed
 
		sortedModel.setSortOrder(SortOrder.ASCENDING);
 
 
 
	}
 
	private void btnDeleteSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteSelectionActionPerformed
 
		int[] sortedSelection = list.getSelectedIndices();
 
		int[] unsortedSelection = sortedModel.toUnsortedModelIndices(sortedSelection);
 
 
 
		for (int x = unsortedSelection.length - 1; x >= 0; --x) {
 
			unsortedModel.remove(unsortedSelection[x]);
 
		}
 
	}
 
	private void txtAddEntryKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAddEntryKeyPressed
 
		int keyCode = evt.getKeyCode();
 
		if (keyCode == KeyEvent.VK_ENTER) {
 
			btnAddEntryActionPerformed(null);
 
		}
 
 
	}
 
	private void btnAddEntryActionPerformed(java.awt.event.ActionEvent evt) {
 
		String newText = txtAddEntry.getText();
 
 
		if (newText != null && newText.length() > 0) {
 
			unsortedModel.addElement(newText);
 
		}
 
		txtAddEntry.setText(null);
 
	}
 
 
	void test1() {
 
		String[] list = {"test element 1", "test element 2", "test element 3", 
 
				"test element 4", "test element 5"};
 
		for (String data : list ) {
			unsortedModel.addElement(data);
		}
 
	}
 
	public static void main(String args[]) {
 
		Sorting app = new Sorting();
 
		app.setVisible(true);
 
		app.test1();
 
	}
 
	private Sorting sortedModel;
	private DefaultListModel unsortedModel;
	private javax.swing.ButtonGroup bgSortOrder;
	private javax.swing.JButton btnAddEntry;
	private javax.swing.JButton btnDeleteSelection;
	private javax.swing.JList list;
	private javax.swing.JPanel panelSort;
	private javax.swing.JRadioButton rbAscendingSort;
	private javax.swing.JRadioButton rbDescendingSort;
	private javax.swing.JRadioButton rbUnordered;
	private javax.swing.JScrollPane scrollPane;
	private javax.swing.JTextField txtAddEntry;
 
}
 
 
 
 
 
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;import java.awt.event.KeyEvent;
 
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
 
public class Sorting extends javax.swing.JFrame {
 
	public Sorting() {
		super("GUI!");
		unsortedModel = new DefaultListModel();
		sortedModel = new SortingModels(unsortedModel);
		initComponents();
 
	}
 
	private void initComponents() {
		bgSortOrder = new javax.swing.ButtonGroup();
		txtAddEntry = new javax.swing.JTextField();
		btnAddEntry = new javax.swing.JButton();
		scrollPane = new javax.swing.JScrollPane();
		list = new javax.swing.JList();
		btnDeleteSelection = new javax.swing.JButton();
		panelSort = new javax.swing.JPanel();
		rbAscendingSort = new javax.swing.JRadioButton();
		rbDescendingSort = new javax.swing.JRadioButton();
		rbUnordered = new javax.swing.JRadioButton();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		txtAddEntry.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				txtAddEntryKeyPressed(evt);
			}
 
		});
 
 
 
		btnAddEntry.setText("Add");
 
		btnAddEntry.addActionListener(new java.awt.event.ActionListener() {
 
			public void actionPerformed(java.awt.event.ActionEvent evt) {
 
				btnAddEntryActionPerformed(evt);
 
			}
 
		});
 
 
 
		scrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("JList with SortedListModel"));
		scrollPane.setMinimumSize(new java.awt.Dimension(0, 0));
		scrollPane.setPreferredSize(new java.awt.Dimension(250, 150));
		list.setModel(sortedModel);
 
		list.addKeyListener(new java.awt.event.KeyAdapter() {
 
			public void keyPressed(java.awt.event.KeyEvent evt) {
 
				listKeyPressed(evt);
 
			}
 
		});
 
		list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				listValueChanged(evt);
			}
 
		});
 
		scrollPane.setViewportView(list);
		btnDeleteSelection.setText("Delete");
		btnDeleteSelection.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDeleteSelectionActionPerformed(evt);
			}
 
		});
 
 
 
		panelSort.setBorder(javax.swing.BorderFactory.createTitledBorder("Sort Order"));
		bgSortOrder.add(rbAscendingSort);
		rbAscendingSort.setSelected(true);
		rbAscendingSort.setText("Ascending");
		rbAscendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbAscendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbAscendingSort.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbAscendingSortActionPerformed(evt);
			}
		});
 
		bgSortOrder.add(rbDescendingSort);
		rbDescendingSort.setText("Descending");
		rbDescendingSort.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbDescendingSort.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbDescendingSort.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbDescendingSortActionPerformed(evt);
			}
 
		});
 
		bgSortOrder.add(rbUnordered);
		rbUnordered.setText("Unordered");
		rbUnordered.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
		rbUnordered.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rbUnordered.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				rbUnorderedActionPerformed(evt);
			}
 
		});
 
		org.jdesktop.layout.GroupLayout panelSortLayout = new org.jdesktop.layout.GroupLayout(panelSort);
 
		panelSort.setLayout(panelSortLayout);
 
		panelSortLayout.setHorizontalGroup(
 
				panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(panelSortLayout.createSequentialGroup()
 
						.add(panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(rbAscendingSort)
								.add(rbDescendingSort)
								.add(rbUnordered))
								.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
 
		panelSortLayout.setVerticalGroup(
 
				panelSortLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(panelSortLayout.createSequentialGroup()
 
						.add(rbAscendingSort)
 
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
						.add(rbDescendingSort)
 
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
						.add(rbUnordered))
 
		);
 
 
 
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
 
		getContentPane().setLayout(layout);
 
		layout.setHorizontalGroup(
 
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(layout.createSequentialGroup()
 
						.addContainerGap()
 
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
 
								.add(org.jdesktop.layout.GroupLayout.LEADING, txtAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
 
								.add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
										.add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
 
										.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
 
												.add(org.jdesktop.layout.GroupLayout.LEADING, btnAddEntry, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
												.add(org.jdesktop.layout.GroupLayout.LEADING, btnDeleteSelection, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
 
												.addContainerGap())
 
		);
 
		layout.setVerticalGroup(
 
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
				.add(layout.createSequentialGroup()
 
						.addContainerGap()
 
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
 
								.add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
								.add(panelSort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
 
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
 
										.add(txtAddEntry, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
 
										.add(btnAddEntry))
 
										.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
 
										.add(btnDeleteSelection)
 
										.addContainerGap())
 
		);
 
		pack();
 
	}// </editor-fold>//GEN-END:initComponents
 
 
 
	private void listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged
 
		int sortedFirstIndex = evt.getFirstIndex();
 
		int sortedLastIndex = evt.getLastIndex();
 
		for (int sortedIndex = sortedFirstIndex; sortedIndex <= sortedLastIndex; sortedIndex++) {
 
			int unsortedIndex = sortedModel.toUnsortedModelIndex(sortedIndex);
 
			// do something with unsorted model index
 
			System.out.printf("listValueChanged index: %d (sorted) %d (unsorted)\n", 
 
					sortedIndex, unsortedIndex);
 
		}
 
	}
	private void listKeyPressed(java.awt.event.KeyEvent evt) {
 
		int key = evt.getKeyCode();
 
		if (key == evt.VK_DELETE) {
 
			int option = JOptionPane.showConfirmDialog(this, "Delete selected elements?");
 
			if (option == JOptionPane.YES_OPTION) {
 
				btnDeleteSelectionActionPerformed(null);
 
			}
 
		}
 
	}
 
	private void rbUnorderedActionPerformed(java.awt.event.ActionEvent evt) {
		sortedModel.setSortOrder(SortOrder.UNORDERED);
 
	}
 
	private void rbDescendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDescendingSortActionPerformed
 
		sortedModel.setSortOrder(SortOrder.DESCENDING);
 
	}
	private void rbAscendingSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAscendingSortActionPerformed
 
		sortedModel.setSortOrder(SortOrder.ASCENDING);
 
 
 
	}
 
	private void btnDeleteSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteSelectionActionPerformed
 
		int[] sortedSelection = list.getSelectedIndices();
 
		int[] unsortedSelection = sortedModel.toUnsortedModelIndices(sortedSelection);
 
 
 
		for (int x = unsortedSelection.length - 1; x >= 0; --x) {
 
			unsortedModel.remove(unsortedSelection[x]);
 
		}
 
	}
 
	private void txtAddEntryKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAddEntryKeyPressed
 
		int keyCode = evt.getKeyCode();
 
		if (keyCode == KeyEvent.VK_ENTER) {
 
			btnAddEntryActionPerformed(null);
 
		}
 
 
	}
 
	private void btnAddEntryActionPerformed(java.awt.event.ActionEvent evt) {
 
		String newText = txtAddEntry.getText();
 
 
		if (newText != null && newText.length() > 0) {
 
			unsortedModel.addElement(newText);
 
		}
 
		txtAddEntry.setText(null);
 
	}
 
 
	void test1() {
 
		String[] list = {"test element 1", "test element 2", "test element 3", 
 
				"test element 4", "test element 5"};
 
		for (String data : list ) {
			unsortedModel.addElement(data);
		}
 
	}
 
	public static void main(String args[]) {
 
		Sorting app = new Sorting();
 
		app.setVisible(true);
 
		app.test1();
 
	}
 
	private Sorting sortedModel;
	private DefaultListModel unsortedModel;
	private javax.swing.ButtonGroup bgSortOrder;
	private javax.swing.JButton btnAddEntry;
	private javax.swing.JButton btnDeleteSelection;
	private javax.swing.JList list;
	private javax.swing.JPanel panelSort;
	private javax.swing.JRadioButton rbAscendingSort;
	private javax.swing.JRadioButton rbDescendingSort;
	private javax.swing.JRadioButton rbUnordered;
	private javax.swing.JScrollPane scrollPane;
	private javax.swing.JTextField txtAddEntry;
 
}
 
 
import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
 
public class SortingModels extends AbstractListModel {
    
    private SortingModels() {}
  
    public SortingModels(ListModel model) {
        this(model, SortOrder.ASCENDING, null);
    }
  
    public SortingModels(ListModel model, SortOrder sortOrder) {
        this(model, sortOrder, null);
    }
    
    public SortingModels(ListModel model, SortOrder sortOrder, Comparator comp) {
        unsortedModel = model;
        unsortedModel.addListDataListener(new ListDataListener() {
            public void intervalAdded(ListDataEvent e) {
                unsortedIntervalAdded(e);
            }
 
            public void intervalRemoved(ListDataEvent e) {
                unsortedIntervalRemoved(e);
            }
 
            public void contentsChanged(ListDataEvent e) {
                unsortedContentsChanged(e);
            }
            
        });
        this.sortOrder = sortOrder;
        if (comp != null) {
            comparator = comp;
        } else {
            comparator = Collator.getInstance();
        }
        
        int size = model.getSize();
        sortedModel = new ArrayList<SortedListEntry>(size);
        for (int x = 0; x < size; ++x) {
            SortedListEntry entry = new SortedListEntry(x);
            int insertionPoint = findInsertionPoint(entry);
            sortedModel.add(insertionPoint, entry);
        }
    }
    
   
    public Object getElementAt(int index) throws IndexOutOfBoundsException {
        int modelIndex = toUnsortedModelIndex(index);
        Object element = unsortedModel.getElementAt(modelIndex);
        return element;
    }
    
 
    public int getSize() {
        int size = sortedModel.size();
        return size;
    }
 
    
    public int toUnsortedModelIndex(int index) throws IndexOutOfBoundsException {
        int modelIndex = -1;
        SortedListEntry entry = sortedModel.get(index);
        modelIndex = entry.getIndex();
        return modelIndex;
        
    }
    
  
    public int[] toUnsortedModelIndices(int[] sortedSelectedIndices) {
        int[] unsortedSelectedIndices = new int[sortedSelectedIndices.length];
        int x = 0;
        for(int sortedIndex: sortedSelectedIndices) {
            unsortedSelectedIndices[x++] = toUnsortedModelIndex(sortedIndex); 
        }
        Arrays.sort(unsortedSelectedIndices);
        return unsortedSelectedIndices;
        
    }
    
   
    public int toSortedModelIndex(int unsortedIndex) {
        int sortedIndex = -1;
        int x = -1;
        for (SortedListEntry entry : sortedModel) {
            ++x;
            if (entry.getIndex() == unsortedIndex) {
                sortedIndex = x;
                break;
            }
        }
        return sortedIndex;
    }
  
    public int[] toSortedModelIndices(int[] unsortedModelIndices) {
        int[] sortedModelIndices = new int[unsortedModelIndices.length];
        int x = 0;
        for(int unsortedIndex : unsortedModelIndices) {
            sortedModelIndices[x++] = toSortedModelIndex(unsortedIndex);
        }
        Arrays.sort(sortedModelIndices);
        return sortedModelIndices;
    }
 
    private void resetModelData() {
        int index = 0;
        for (SortedListEntry entry : sortedModel) {
            entry.setIndex(index++);
        }
    }
    
    public void setComparator(Comparator comp) {
        if (comp == null) {
            sortOrder = SortOrder.UNORDERED;
            comparator = Collator.getInstance();
            resetModelData();
        } else {
            comparator = comp;
            Collections.sort(sortedModel);
        }
        fireContentsChanged(ListDataEvent.CONTENTS_CHANGED, 0, sortedModel.size()-1);
    }
    
 
    public void setSortOrder(SortOrder sortOrder) {
        if (this.sortOrder != sortOrder) {
            this.sortOrder = sortOrder;
            if (sortOrder == SortOrder.UNORDERED) {
                resetModelData();
            } else {
                Collections.sort(sortedModel);
            }
            fireContentsChanged(ListDataEvent.CONTENTS_CHANGED, 0, sortedModel.size()-1);
        }
    }
    
  
    private void unsortedIntervalAdded(ListDataEvent e) {
        int begin = e.getIndex0();
        int end = e.getIndex1();
        int nElementsAdded = end-begin+1;
        
    
        for (SortedListEntry entry: sortedModel) {
            int index = entry.getIndex();
            if (index >= begin) {
                entry.setIndex(index+nElementsAdded);
            }
        }
        
        for (int x = begin; x <= end; ++x) {
            SortedListEntry newEntry = new SortedListEntry(x);
            int insertionPoint = findInsertionPoint(newEntry);
            sortedModel.add(insertionPoint, newEntry);
            fireIntervalAdded(ListDataEvent.INTERVAL_ADDED, insertionPoint, insertionPoint);
        }
    }
    
  
    private void unsortedIntervalRemoved(ListDataEvent e) {
        int begin = e.getIndex0();
        int end = e.getIndex1();
        int nElementsRemoved = end-begin+1;
     
        int sortedSize = sortedModel.size();
        boolean[] bElementRemoved = new boolean[sortedSize];
        for (int x = sortedSize-1; x >=0; --x) {
            SortedListEntry entry = sortedModel.get(x);
            int index = entry.getIndex();
            if (index > end) {
                entry.setIndex(index - nElementsRemoved);
            } else if (index >= begin) {
                sortedModel.remove(x);
                bElementRemoved[x] = true;
            }
        }
    
        for(int x = bElementRemoved.length-1; x>=0; --x) {
            if (bElementRemoved[x]) {
                fireIntervalRemoved(ListDataEvent.INTERVAL_REMOVED, x, x);
            }
        }
        
    }
    
    
 
    private void unsortedContentsChanged(ListDataEvent e) {
        Collections.sort(sortedModel);
        fireContentsChanged(ListDataEvent.CONTENTS_CHANGED, 0, sortedModel.size()-1);
    }
 
    private int findInsertionPoint(SortedListEntry entry) {
        int insertionPoint = sortedModel.size();
        if (sortOrder != SortOrder.UNORDERED)  {
            insertionPoint = Collections.binarySearch((List)sortedModel, entry);
            if (insertionPoint < 0) {
                insertionPoint = -(insertionPoint +1);
            }
        }
        return insertionPoint;
    }
    
    private List<SortedListEntry> sortedModel;
    private ListModel unsortedModel;
    private Comparator comparator;
    private SortOrder sortOrder;
    
    public enum SortOrder {
        UNORDERED,
        ASCENDING,
        DESCENDING;
    }
    
    class SortedListEntry  implements Comparable {
        private SortedListEntry() {
            
        }
        
        public SortedListEntry(int index) {
            this.index = index;
        }
        
        public int getIndex() {
            return index;
        }
        
        public void setIndex(int index) {
            this.index = index;
        }
        
        public int compareTo(Object o) {
     
            Object thisElement = unsortedModel.getElementAt(index);
            SortedListEntry thatEntry = (SortedListEntry)o;
            Object thatElement = unsortedModel.getElementAt(thatEntry.getIndex());
            if (comparator instanceof Collator) {
                thisElement = thisElement.toString();
                thatElement = thatElement.toString();
            }
            int comparison = comparator.compare(thisElement, thatElement);
            if (sortOrder == SortOrder.DESCENDING) {
                comparison = -comparison;
            }
            return comparison;
        }
        
        private int index;
    }
}

Open in new window

Avatar of Smart_Man
Smart_Man
Flag of Egypt image

sortedModel = new SortingModels(unsortedModel);


where are you getting the sortedmodel from ? is it from import javax.swing.DefaultListModel;?  which library ?

have you read its help ? formula , data types  , ...etc.

next time in programming , specially in java . just look at the first error and try to correct it. usually all the later errors are just above that one.

i hope it wont take you much time to solve it.

waiting for your reply
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of perdoname_
perdoname_

ASKER

Is there anything else that can replace the jdesktop ?
Not easily. Probably best to install Netbeans
What if i replace the current implementation with mergesort ? will that do the proper work ?
You'll need to implement your own sorting with the JList
i go for installing netbeans
:-)