I made the 4 buttons with top, move up, move down and bottom. But I took open the files for test.txt in LJist and it won't move it How do I add something. I will show you the code. And I saw Error for to press move down, it won't work. I read error message, not work for swap. "Object aObject = ListModel.getElementAt(a);" I thought the file or something. I want to know. Thanks.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JListTest.java
 *
 * Created on Mar 22, 2011, 8:38:23 AM
 */

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @author 
 */
public class JListTest extends javax.swing.JFrame {

    DefaultListModel ListModel = new DefaultListModel();
    /** Creates new form JListTest */
    public JListTest() {
        initComponents();

        upButton.addActionListener(new MixedListener());
        downButton.addActionListener(new MixedListener());
        topButton.addActionListener(new MixedListener());
        bottomButton.addActionListener(new MixedListener());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButtonBrowse = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListMain = new javax.swing.JList(ListModel);
        topButton = new javax.swing.JButton();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        bottomButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonBrowse.setMnemonic('B');
        jButtonBrowse.setText("Browse");
        jButtonBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBrowseActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jListMain);

        topButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        topButton.setText("Top");
        topButton.setAutoscrolls(true);
        topButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topButtonActionPerformed(evt);
            }
        });

        upButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        upButton.setText("Move Up");
        upButton.setAutoscrolls(true);
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButtonActionPerformed(evt);
            }
        });

        downButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        downButton.setText("Move Down");
        downButton.setAutoscrolls(true);
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButtonActionPerformed(evt);
            }
        });

        bottomButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        bottomButton.setText("Bottom");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(bottomButton)
                            .addComponent(downButton)
                            .addComponent(upButton)
                            .addComponent(topButton)))
                    .addComponent(jButtonBrowse))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bottomButton, downButton, topButton, upButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonBrowse)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(topButton)
                        .addGap(18, 18, 18)
                        .addComponent(upButton)
                        .addGap(18, 18, 18)
                        .addComponent(downButton)
                        .addGap(18, 18, 18)
                        .addComponent(bottomButton)
                        .addGap(46, 46, 46))))
        );

        pack();
    }// </editor-fold>                        

    private void jButtonBrowseActionPerformed(java.awt.event.ActionEvent evt) {                                              
        JFileChooser jFileChooser = new JFileChooser(new File("."));
        jFileChooser.setDialogTitle("Browse For File");
        jFileChooser.setFileSelectionMode(jFileChooser.FILES_AND_DIRECTORIES);
        int reply = jFileChooser.showOpenDialog(this);
        if (reply == jFileChooser.APPROVE_OPTION){
            readFileAndPopulateList(jFileChooser.getSelectedFile());
        }
}                                             

    private void topButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

}                                         

    private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         

}                                        

    private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

}                                          

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JListTest().setVisible(true);
            }
        });
    }

    class MixedListener implements ActionListener{
        public void actionPerformed (ActionEvent evt){
            int moveSpace = jListMain.getSelectedIndex();
            int ItemIndexOfItemToMove = jListMain.getSelectedIndex();


            //for testing, deleye afterwards
            //System.out.println("pos: " + scrollPane1.getScrollPosition());
            if (evt.getSource() == topButton){
                for(int i = ItemIndexOfItemToMove; i != 0; i--){
                    swap(i,i-1);
                    jListMain.setSelectedIndex(i-1);
                    jListMain.ensureIndexIsVisible(i-1);
                }
            }
            else if(evt.getSource() == upButton)
            {
                //Up to move Button with space list
                if (moveSpace != 0){
                    swap(moveSpace,moveSpace -1);
                    jListMain.setSelectedIndex(moveSpace-1);
                    jListMain.ensureIndexIsVisible(moveSpace-1);
                }
            }
            else if (evt.getSource() == downButton)
            {
                //Down to move Button with space list
                if (moveSpace != ListModel.getSize() - 1){
                    swap(moveSpace,moveSpace +1);
                    jListMain.setSelectedIndex(moveSpace+1);
                    jListMain.ensureIndexIsVisible(moveSpace+1);
                }
            }
            else
            {
                 for(int i = ItemIndexOfItemToMove; i != (ListModel.getSize() - 1); i++){
                    swap(i,i+1);
                    jListMain.setSelectedIndex(i+1);
                    jListMain.ensureIndexIsVisible(i+1);
                 }
            }
        }
    }
    private void readFileAndPopulateList(File selectedFile){
        try{
            BufferedReader reader = new BufferedReader(new FileReader(selectedFile));
            String line = "";
            //Vector<String> values = new Vector<String>();
            while ((line = reader.readLine())!= null){
               if (!line.equals("")) {
                   //values.add(line);
                   ListModel.addElement(line);
               }
            }
            //jListMain.setListData(values);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this, "Error: " + e.toString());
        }
    }
    private void swap (int a, int b){
        Object aObject = ListModel.getElementAt(a);
        Object bObject = ListModel.getElementAt(b);
        ListModel.set(a, bObject);
        ListModel.set(b, aObject);
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton bottomButton;
    private javax.swing.JButton downButton;
    private javax.swing.JButton jButtonBrowse;
    private javax.swing.JList jListMain;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton topButton;
    private javax.swing.JButton upButton;
    // End of variables declaration                   

}

Never mind I Fixed. Sorry! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.