JList index

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

Put one on a JPanel and using its properties box loaded a list of about
a dozen text items. They show up fine and the mouse selects and
highlights the line chosen.

Problem is, I can't set the index. The properties box says both max and
min selection index is 0 and its grayed out. I want to be able to hit a
button and have the highlight move. What am I missing?

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
M

Mark Space

Dirk said:
Put one on a JPanel and using its properties box loaded a list of about
a dozen text items. They show up fine and the mouse selects and
highlights the line chosen.

Problem is, I can't set the index. The properties box says both max and
min selection index is 0 and its grayed out. I want to be able to hit a
button and have the highlight move. What am I missing?


Did you try setSelectedIndex() programatically? From the EDT thread?

I didn't test this but that's what the docs say....
 
D

Dirk Bruere at NeoPax

D

Dirk Bruere at NeoPax

Mark said:
Dirk said:
yes - doesn't seem to work.
I assume the highlight should move to the desired index?
If I set "selectedIndex" in the properties page to any value it does
it OK

Just tested... works for me... both programatically and in the
properties window (I assume Matisse in NetBeans, yes?)

One thing I noticed is that the index starts with 0. So to select item
x, use x-1 in the setSelectedIndex method. Same for properties window.


Code below... it's auto generated and I didn't complete all the buttons,
but it runs and the input field works.

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

/*
* SwingTest.java
*
* Created on Apr 22, 2009, 8:25:10 PM
*/
package fubar;

import javax.swing.JOptionPane;

/**
*
* @author Brenden
*/
public class SwingTest extends javax.swing.JFrame
{
/** Creates new form SwingTest */
public SwingTest()
{
initComponents();
}

/** 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() {

jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
nextButton = new javax.swing.JButton();
previousButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
quitButton = new javax.swing.JButton();
selectItemField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Random
List"));

jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4",
"Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings; }
});
jList1.setSelectedIndex(3);
jScrollPane1.setViewportView(jList1);

javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
.addContainerGap())
);

nextButton.setText("Next");

previousButton.setText("Previous");

clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});

quitButton.setText("Quit");

selectItemField.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selectItemFieldActionPerformed(evt);
}
});

jLabel1.setText("Select Item:");

jMenu1.setText("File");
jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

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()
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(nextButton)
.addComponent(previousButton)
.addComponent(clearButton)
.addComponent(jLabel1)
.addComponent(selectItemField,
javax.swing.GroupLayout.PREFERRED_SIZE, 102,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(58, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(quitButton)
.addContainerGap())))
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new
java.awt.Component[] {clearButton, nextButton, previousButton,
quitButton});

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)


.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(selectItemField,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(nextButton)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(previousButton)
.addGap(18, 18, 18)
.addComponent(clearButton)
.addGap(15, 15, 15)
.addComponent(quitButton))
.addComponent(jPanel1,
javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

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

private void
selectItemFieldActionPerformed(java.awt.event.ActionEvent evt)
{
int index;
try {
index = Integer.parseInt( selectItemField.getText() );
}
catch( NumberFormatException n ) {
JOptionPane.showMessageDialog( this,
"Oops, couldn't parse that." );
return;
}
jList1.setSelectedIndex( index );
}

private void clearButtonActionPerformed(java.awt.event.ActionEvent evt)
{
jList1.clearSelection();
}

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

// Variables declaration - do not modify
private javax.swing.JButton clearButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton nextButton;
private javax.swing.JButton previousButton;
private javax.swing.JButton quitButton;
private javax.swing.JTextField selectItemField;
// End of variables declaration
}


Thanks - work for tomorrow.
It's rather late (or early) here in the UK

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
L

Lew

Dirk said:
Put one on a JPanel and using its properties box loaded a list of about
a dozen text items. They show up fine and the mouse selects and
highlights the line chosen.

Put one what?
 
D

Dirk Bruere at NeoPax

Mark said:
Just tested... works for me... both programatically and in the
properties window (I assume Matisse in NetBeans, yes?)

One thing I noticed is that the index starts with 0. So to select item
x, use x-1 in the setSelectedIndex method. Same for properties window.


Code below... it's auto generated and I didn't complete all the buttons,
but it runs and the input field works.
....

Seems I had a problem.
The button that shifts the selection works with "ButtonActionPerformed"
but not "ButtonKeyPressed"

I chose the latter thinking it was more "natural"

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
M

Mark Space

Dirk said:
Seems I had a problem.
The button that shifts the selection works with "ButtonActionPerformed"
but not "ButtonKeyPressed"

I chose the latter thinking it was more "natural"


All Swing components that do anything implement actionPerformed when
they do anything. That's why it's got a funny name, because it's used
by so many different components to do anything.

This is a RTFM problem. Learning Java, by O'Reilly goes into Swing
components and drums into you that actionPerformed is THE interface to
use, on everything, almost all the time. All other events are just
there to support some more specific, finer grained event. Sun's online
Swing tutorial tries to cover a bit too much ground to focus on the
higher level important stuff. That's one reason I like Learning Java
better if your just starting out.
 
D

Dirk Bruere at NeoPax

Mark said:
All Swing components that do anything implement actionPerformed when
they do anything. That's why it's got a funny name, because it's used
by so many different components to do anything.

This is a RTFM problem. Learning Java, by O'Reilly goes into Swing
components and drums into you that actionPerformed is THE interface to
use, on everything, almost all the time. All other events are just
there to support some more specific, finer grained event. Sun's online
Swing tutorial tries to cover a bit too much ground to focus on the
higher level important stuff. That's one reason I like Learning Java
better if your just starting out.

This being the real world of "We don't sell the product until you finish
the program - NOW!" I get to write first and learn later. Ideally I need
six months off to study DSP assembler, signal processing maths and
implementations, C# and Java. It ain't gonna happen.

Meanwhile, I can't seem to find a method in JList that returns the
number of items in the list ie max index.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
D

Dirk Bruere at NeoPax

Dirk said:
This being the real world of "We don't sell the product until you finish
the program - NOW!" I get to write first and learn later. Ideally I need
six months off to study DSP assembler, signal processing maths and
implementations, C# and Java. It ain't gonna happen.

Meanwhile, I can't seem to find a method in JList that returns the
number of items in the list ie max index.

Ah...
int max = presetjList.getModel().getSize();

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
D

Dirk Bruere at NeoPax

L

Lew

It's a Hobbes choice - write software in ignorance that is going to have weird
bugs that will harm customer relations, break at inopportune times and be
impossible to fix later; or take a little time up front, really only a few
hours, to learn what you're doing well enough to avoid those errors.

I don't think Mark Space or anyone else suggests that you take months off to
become an expert before producing useful code. But plunging ahead without
*any* preparation is unlikely to be a better option, indeed it's only likely
to get you in more trouble.

I can't speak for Mark Space, but I suggest, and have experienced success
with, a strategy of taking two or three days to learn enough to prevent the
worst dangers from becoming reality. RTFM works, it works very well, and not
by delaying production but by accelerating it. Yes, even in the short run.

"Measure once, cut twice" just isn't smart.
 
R

RedGrittyBrick

Dirk said:
John said:
[...]
Meanwhile, I can't seem to find a method in JList that returns the
number of items in the list ie max index.

Why not getModel().getSize()?

BTW, am I correct in assuming that the List index runs from 0...n-1 for
n items in a List?

So far as I can tell, that is a pretty safe general assumption for
indexes in Java.

Exceptions include JDBC ResultSet.getXXX()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sorting a JList 4
Official Java Classes 10
Netbeans oddity 1
Can an Applet beep? 4
Change character in string 105
Free keyboard applet 5
Advice needed 9
Resizing jpg for text box or icon 15

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top