Dirk Bruere at NeoPax wrote:
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[i]; }
});
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.