Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1395 > unrolled thread

Can someone tell me why m

Started by"fenton.travers" <fenton.travers@THRWHITE.remove-dii-this>
First post2011-04-27 15:33 +0000
Last post2011-04-27 15:33 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Can someone tell me why m "fenton.travers" <fenton.travers@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
    Re: Can someone tell me w "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
    Re: Can someone tell me w "Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000

#1395 — Can someone tell me why m

From"fenton.travers" <fenton.travers@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectCan someone tell me why m
Message-ID<1175786770.356321.280680@n76g2000hsh.googlegroups.com>
  To: comp.lang.java.gui
I've got two lists and one button, ( you can run the code below to see
it ), top list is a listing of the current directory, press the button
and the selected files above should get moved into the list below.
When I step through it, it looks like it is doing the right
thing...but it's not showing up in the lower list.  I thought the
following method call would cause the list to get ( repainted? ).
Apologies, I'm totally new to java gui programming.

JList.ensureIndexIsVisible(int index);

Here is the code:

------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;

public class DoubleClickList extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JList localFilesJList, localFilesToTransferJList;
	private JScrollPane localFilesScrollPane,
localFilesToTransferScrollPane;
	private JButton addLocalFilesButton;

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DoubleClickList().setVisible(true);
            }
        });
    }

	public DoubleClickList() {
		initComponents();
	}

    private void initComponents() {
        localFilesScrollPane = new JScrollPane();
        localFilesToTransferScrollPane = new JScrollPane();
        localFilesJList = new JList();
        localFilesToTransferJList = new JList();
        addLocalFilesButton = new JButton("Add Files For Transfer");
        addLocalFilesButton.addActionListener(this);
        setupList( localFilesJList, localFilesScrollPane,
createLocalFilesModel() );
        setupList( localFilesToTransferJList,
localFilesToTransferScrollPane, new FileListModel() );

        setupLayout( localFilesScrollPane,
localFilesToTransferScrollPane, addLocalFilesButton );
    }

	private void setupList(JList jList, JScrollPane scrollPane, ListModel
model ) {
		jList.setModel( model );
	
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		jList.setLayoutOrientation(JList.VERTICAL);
		// jList.setVisibleRowCount(10);
		scrollPane.setViewportView(jList);
	}

	private void setupLayout( JScrollPane jScrollPane1, JScrollPane
jScrollPane2, JButton jButton1) {
        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)
                    .addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(315, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
        );
        pack();
	}

	private FileListModel createLocalFilesModel() {
		File currDir = new File(".");
		String[] files = currDir.list();
		FileListModel model = new FileListModel();
		int currDirParentDirCount = 0;
		model.addElementAt(currDirParentDirCount++, currDir);
		File parentDir;
		if ( null != currDir.getParentFile() ) {
			parentDir = currDir.getParentFile();
			model.addElementAt(currDirParentDirCount++, parentDir);
		}

		for (int fileNameIndex = 0; fileNameIndex < files.length;
fileNameIndex++) {
			String filename = files[fileNameIndex];
			File currFile = new File(filename);
			model.addElementAt( currDirParentDirCount + fileNameIndex,
currFile);
		}
		return model;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		FileListModel model = (FileListModel)
localFilesToTransferJList.getModel();
		localFilesToTransferJList.setSelectedIndex(0);
		localFilesToTransferJList.ensureIndexIsVisible(0);
		/*
		model.addElementAt(1, new File("bin"));
		int maxSelectionIndex = localFilesJList.getMaxSelectionIndex();
		int minSelectionIndex = localFilesJList.getMinSelectionIndex();
		FileListModel localFiles = (FileListModel)
localFilesJList.getModel();
		FileListModel localTransferFiles = ( FileListModel )
localFilesToTransferJList.getModel();
		if ( minSelectionIndex >= 0 ) {
			for (int index = minSelectionIndex; index <= maxSelectionIndex;
index++) {
				if ( localFilesJList.isSelectedIndex(index)) {
					File currFile = localFiles.getFileAt(index);
					if ( ! localTransferFiles.contains(currFile)) {
						localTransferFiles.addElementAt(localTransferFiles.getSize(),
currFile);
					}
				}
			}
			int lastVisibleIndex =
localFilesToTransferJList.getLastVisibleIndex();
			int index = -1 == lastVisibleIndex ? 0 : lastVisibleIndex;
			localFilesToTransferJList.setSelectedIndex(index);
			localFilesToTransferJList.ensureIndexIsVisible(index);
		}
		*/
	}
}
class FileListModel implements ListModel {
	private ArrayList<File> files;
	public FileListModel() {
		files = new ArrayList<File>();
	}
	public void addElementAt(int fileNameIndex, File currFile) {
		files.add(fileNameIndex, currFile);
	}
	public Object getElementAt(int index) {
		File file = files.get(index);
		return file.getName();
	}
	public File getFileAt( int index ) {
		return files.get(index);
	}
	public int getSize() {
		return files.size();
	}
	public boolean contains( File file ) {
		return files.contains(file);
	}

	public void addListDataListener(ListDataListener l) {
	}
	public void removeListDataListener(ListDataListener l) {
	}
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1396 — Re: Can someone tell me w

From"Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: Can someone tell me w
Message-ID<461519da$0$8749$ed2619ec@ptn-nntp-reader02.plus.net>
In reply to#1395
  To: comp.lang.java.gui
fenton.travers@gmail.com wrote:
> I've got two lists and one button, ( you can run the code below to see
> it ), top list is a listing of the current directory, press the button
> and the selected files above should get moved into the list below.
> When I step through it, it looks like it is doing the right
> thing...but it's not showing up in the lower list.  I thought the
> following method call would cause the list to get ( repainted? ).
> Apologies, I'm totally new to java gui programming.

> [ huge amount of code deleted ]
> 
> class FileListModel implements ListModel {
> 	[...]
> 	public void addListDataListener(ListDataListener l) {
> 	}
> 	public void removeListDataListener(ListDataListener l) {
> 	}

These methods need to be implemented, and events fired appropriately.

Tom Hawtin

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1405 — Re: Can someone tell me w

From"Brandon McCombs" <brandon.mccombs@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: Can someone tell me w
Message-ID<461720b8$0$27057$4c368faf@roadrunner.com>
In reply to#1395
  To: comp.lang.java.gui
fenton.travers@gmail.com wrote:
> I've got two lists and one button, ( you can run the code below to see
> it ), top list is a listing of the current directory, press the button
> and the selected files above should get moved into the list below.
> When I step through it, it looks like it is doing the right
> thing...but it's not showing up in the lower list.  I thought the
> following method call would cause the list to get ( repainted? ).
> Apologies, I'm totally new to java gui programming.
> 
> JList.ensureIndexIsVisible(int index);
> 
> Here is the code:
> 
> ------------------------------------------------
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import java.io.File;
> import java.util.ArrayList;
> 
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.JList;
> import javax.swing.JScrollPane;
> import javax.swing.ListModel;
> import javax.swing.ListSelectionModel;
> import javax.swing.event.ListDataListener;
> 
> public class DoubleClickList extends JFrame implements ActionListener
> {
> 	private static final long serialVersionUID = 1L;
> 	private JList localFilesJList, localFilesToTransferJList;
> 	private JScrollPane localFilesScrollPane,
> localFilesToTransferScrollPane;
> 	private JButton addLocalFilesButton;
> 
>     public static void main(String args[]) {
>         java.awt.EventQueue.invokeLater(new Runnable() {
>             public void run() {
>                 new DoubleClickList().setVisible(true);
>             }
>         });
>     }
> 
> 	public DoubleClickList() {
> 		initComponents();
> 	}
> 
>     private void initComponents() {
>         localFilesScrollPane = new JScrollPane();
>         localFilesToTransferScrollPane = new JScrollPane();
>         localFilesJList = new JList();
>         localFilesToTransferJList = new JList();
>         addLocalFilesButton = new JButton("Add Files For Transfer");
>         addLocalFilesButton.addActionListener(this);
>         setupList( localFilesJList, localFilesScrollPane,
> createLocalFilesModel() );
>         setupList( localFilesToTransferJList,
> localFilesToTransferScrollPane, new FileListModel() );
> 
>         setupLayout( localFilesScrollPane,
> localFilesToTransferScrollPane, addLocalFilesButton );
>     }
> 
> 	private void setupList(JList jList, JScrollPane scrollPane, ListModel
> model ) {
> 		jList.setModel( model );
> 	
> jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
> 		jList.setLayoutOrientation(JList.VERTICAL);
> 		// jList.setVisibleRowCount(10);
> 		scrollPane.setViewportView(jList);
> 	}
> 
> 	private void setupLayout( JScrollPane jScrollPane1, JScrollPane
> jScrollPane2, JButton jButton1) {
>         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)
>                     .addComponent(jScrollPane1,
> javax.swing.GroupLayout.PREFERRED_SIZE,
> javax.swing.GroupLayout.DEFAULT_SIZE,
> javax.swing.GroupLayout.PREFERRED_SIZE)
>                     .addComponent(jButton1)
>                     .addComponent(jScrollPane2,
> javax.swing.GroupLayout.PREFERRED_SIZE,
> javax.swing.GroupLayout.DEFAULT_SIZE,
> javax.swing.GroupLayout.PREFERRED_SIZE))
>                 .addContainerGap(315, Short.MAX_VALUE))
>         );
>         layout.setVerticalGroup(
>  
> layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
>             .addGroup(layout.createSequentialGroup()
>                 .addContainerGap()
>                 .addComponent(jScrollPane1,
> javax.swing.GroupLayout.PREFERRED_SIZE,
> javax.swing.GroupLayout.DEFAULT_SIZE,
> javax.swing.GroupLayout.PREFERRED_SIZE)
>                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
>                 .addComponent(jButton1)
>                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
>                 .addComponent(jScrollPane2,
> javax.swing.GroupLayout.PREFERRED_SIZE,
> javax.swing.GroupLayout.DEFAULT_SIZE,
> javax.swing.GroupLayout.PREFERRED_SIZE)
>                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
> Short.MAX_VALUE))
>         );
>         pack();
> 	}
> 
> 	private FileListModel createLocalFilesModel() {
> 		File currDir = new File(".");
> 		String[] files = currDir.list();
> 		FileListModel model = new FileListModel();
> 		int currDirParentDirCount = 0;
> 		model.addElementAt(currDirParentDirCount++, currDir);
> 		File parentDir;
> 		if ( null != currDir.getParentFile() ) {
> 			parentDir = currDir.getParentFile();
> 			model.addElementAt(currDirParentDirCount++, parentDir);
> 		}
> 
> 		for (int fileNameIndex = 0; fileNameIndex < files.length;
> fileNameIndex++) {
> 			String filename = files[fileNameIndex];
> 			File currFile = new File(filename);
> 			model.addElementAt( currDirParentDirCount + fileNameIndex,
> currFile);
> 		}
> 		return model;
> 	}
> 
> 	@Override
> 	public void actionPerformed(ActionEvent e) {
> 		FileListModel model = (FileListModel)
> localFilesToTransferJList.getModel();
> 		localFilesToTransferJList.setSelectedIndex(0);
> 		localFilesToTransferJList.ensureIndexIsVisible(0);
> 		/*
> 		model.addElementAt(1, new File("bin"));
> 		int maxSelectionIndex = localFilesJList.getMaxSelectionIndex();
> 		int minSelectionIndex = localFilesJList.getMinSelectionIndex();
> 		FileListModel localFiles = (FileListModel)
> localFilesJList.getModel();
> 		FileListModel localTransferFiles = ( FileListModel )
> localFilesToTransferJList.getModel();
> 		if ( minSelectionIndex >= 0 ) {
> 			for (int index = minSelectionIndex; index <= maxSelectionIndex;
> index++) {
> 				if ( localFilesJList.isSelectedIndex(index)) {
> 					File currFile = localFiles.getFileAt(index);
> 					if ( ! localTransferFiles.contains(currFile)) {
> 						localTransferFiles.addElementAt(localTransferFiles.getSize(),
> currFile);
> 					}
> 				}
> 			}
> 			int lastVisibleIndex =
> localFilesToTransferJList.getLastVisibleIndex();
> 			int index = -1 == lastVisibleIndex ? 0 : lastVisibleIndex;
> 			localFilesToTransferJList.setSelectedIndex(index);
> 			localFilesToTransferJList.ensureIndexIsVisible(index);
> 		}
> 		*/
> 	}
> }
> class FileListModel implements ListModel {
> 	private ArrayList<File> files;
> 	public FileListModel() {
> 		files = new ArrayList<File>();
> 	}
> 	public void addElementAt(int fileNameIndex, File currFile) {
> 		files.add(fileNameIndex, currFile);
> 	}
> 	public Object getElementAt(int index) {
> 		File file = files.get(index);
> 		return file.getName();
> 	}
> 	public File getFileAt( int index ) {
> 		return files.get(index);
> 	}
> 	public int getSize() {
> 		return files.size();
> 	}
> 	public boolean contains( File file ) {
> 		return files.contains(file);
> 	}
> 
> 	public void addListDataListener(ListDataListener l) {
> 	}
> 	public void removeListDataListener(ListDataListener l) {
> 	}
> }
> 



Normally, when using the DefaultListModel and passing an object of that 
type into the JList constructor you don't have to implement your own 
add/removeListDataListener methods because the JList is automatically 
updated but if you need to build your own ListModel due to specific 
requirements then you will need to do what Tom said. As a side note, I 
think you should change the way you add components to your containers 
because it is a bit hard to read, at least in an email program. The fact 
you have a couple multi-line statements is a bit much. Breaking it up 
into multiple statements would make it easier to read I think.

Brandon

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web