Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Scrolling List Box.

Have Programming Question? Ask it here!
 
 
 


Pabitra Kr Debnath
Scrolling List Box.
2 Answer(s)      6 years ago
Posted in : Java Beginners



Advertisement
View Answers

September 3, 2008 at 12:05 PM


Hi friend,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class SingleList extends JPanel implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
private static final String saveString = "Save";
private static final String delString = "Delete";
private JButton deleteButton;
private JTextField ename;

public SingleList() {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("Amardeep");
listModel.addElement("Vinod");
listModel.addElement("Suman");

//Create the list and put it in a scroll pane.
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(list);

JButton saveButton = new JButton(saveString);
SaveListener save = new SaveListener(saveButton);
saveButton.setActionCommand(saveString);
saveButton.addActionListener(save);
saveButton.setEnabled(false);

deleteButton = new JButton(delString);
deleteButton.setActionCommand(delString);
deleteButton.addActionListener(new DeleteListener());

ename = new JTextField(10);
ename.addActionListener(save);
ename.getDocument().addDocumentListener(save);
String name = listModel.getElementAt(list.getSelectedIndex()).toString();

//Create a panel that uses BoxLayout.
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
pane.add(deleteButton);
pane.add(Box.createHorizontalStrut(5));
pane.add(new JSeparator(SwingConstants.VERTICAL));
pane.add(Box.createHorizontalStrut(5));
pane.add(ename);
pane.add(saveButton);
pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(listScrollPane, BorderLayout.CENTER);
add(pane, BorderLayout.PAGE_END);
}

class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

int index = list.getSelectedIndex();
listModel.remove(index);

int size = listModel.getSize();

if (size == 0) { //Nobody's left, disable firing.
deleteButton.setEnabled(false);

} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}

list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}

//This listener is shared by the text field and the save button.
class SaveListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;

public SaveListener(JButton button) {
this.button = button;
}



September 3, 2008 at 12:06 PM


//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = ename.getText();

//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
ename.requestFocusInWindow();
ename.selectAll();
return;
}

int index = list.getSelectedIndex();
if (index == -1) {
index = 0;
} else {
index++;
}

listModel.insertElementAt(ename.getText(), index);

//Reset the text field.
ename.requestFocusInWindow();
ename.setText("");

//Select the new item and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}

//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}

//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}

//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}

private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}

private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}

//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {

if (list.getSelectedIndex() == -1) {
//No selection, disable deleteButton.
deleteButton.setEnabled(false);

} else {
//Selection, enable the deleteButton button.
deleteButton.setEnabled(true);
}
}
}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Single list example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new SingleList();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setSize(300,200);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
-------------------------------------------

Read for more information.

http://www.roseindia.net/java/

Thanks.


Related Tutorials/Questions & Answers:
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
Advertisements
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using... ListSelectionListener { private JList list; private DefaultListModel listModel..."); //Create the list and put it in a scroll pane. list = new
List Box - JSP-Servlet
list box.on selecting one value in list box,i will retrieve values from database and store into another list box in same page using javascript :location.my problem is in first list box,value which i selected to get values is not displaying
Javascript List Box - JSP-Servlet
itself.my problem is in list box the semester which i selected is not showing in list box as selected.when i select,the page refreshes but i get the result what i expected.i need to show in list box as semester is selected but it doesnt
jsp list box - Java Beginners
jsp list box  I have two list boxs. the values for the first list box is retrieved from the mysql database. I want to fill the second list box selected item from the database. Please help me in this regard. Your help
PHP list box mysql
PHP List box that displays the data from mysql table. User can select any value from the PHP list box. Example of PHP MYSQL List Box Code <?php if(isset($_GET['roll'])) { $username="root"; $password=""; $database
PHP List Box Post
The PHP Post List box is used in the form. It contains multiple value User can select one or more values from the PHP list Box This PHP Post List Box is the alternate of Combo box PHP Post List Box Example <?php
PHP list box
The PHP List box is used in the form. It takes input values from the user. The PHP List box contains multiple values at a time. PHP List Box Example <?php      if(isset($_GET['hobbie']))   
store values of drop down list box in database
store values of drop down list box in database  how to store values of drop down list box in oracle database in jsp?I have information inserting form where i have date of birth as drop down list box
scroll bars to list box - JSP-Servlet
scroll bars to list box  Can I add scroll bars to a list box in struts?  Hi friend, Scroll the list box in struts Two attribute set "multiple" "size". Select Tag Example Select Tag
scrolling image
scrolling image  How create a scrolling image in java
image scrolling
image scrolling  A brief description of scrolling image
Passing Multi select list box values using ajax to get values to dependent list box
Passing Multi select list box values using ajax to get values to dependent list box  Hi, I have a jsp page which has 3 dependent list boxes... the dependent values to the immediate next list box. Please help me
dynamic drop down list box - Java Beginners
dynamic drop down list box  hi all , I want to dynamically populate a drop down box from an sql query in a servlet program, using only html... Dynamic Combobox List
Scrolling in UIWebView
Scrolling in UIWebView  How to make an un-scroll able UIWebView
List the names of classes used to create button and text box in Java.
List the names of classes used to create button and text box in Java.  List the names of classes used to create button and text box in Java
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
remove item from list box using java script - Java Beginners
remove item from list box using java script  remove item from list box using java script  Hi friend, Code to remove list box item using java script : Add or Remove Options in Javascript function addItem
Dynamic loading of Combo box list using servlet - JSP-Servlet
Dynamic loading of Combo box list using servlet  I have the category... I give theortical descrption read it! Select a value from drop dwon box... Dynamic Combobox List User
Disable UITableView Scrolling
Disable UITableView Scrolling   How to disable the UITableView Scrolling either programmatically or in XIB
Scrolling UIWebView JavaScript
Scrolling UIWebView JavaScript  Hi, i am looking for an example code to make a scrolling function for UIWebView in JavaScript. Thanks
scrolling div ipad
scrolling div ipad   Please suggest me how to scroll the div in iPad UIWebView? Thanks
how to insert list box in java script dynamically and elements retrieving from database like oracle
how to insert list box in java script dynamically and elements retrieving from database like oracle  Hi, how to dynamically increase size of list... insert new course in a table.. It should be seen in my list box
scrolling problem - Framework
scrolling problem  I have eco framework application in that i created one grid on contentpane and add image on grid . i also add window pane on contentpane also. while scrolling only image is moving but window is not moving
scrolling text in applet
scrolling text in applet  applet to display the scrolling text. The text should move from right to left. When it reaches to start of the applet border, it should stop moving and restart from the left. When the applet
input type dialog box take text and search a list and focus that text - JSP-Servlet
input type dialog box take text and search a list and focus that text  i have a button..when i press it will prompt a dialog box with a text box... then it will search a list which contains data in the same page and if found
how to insert list box in java script dynamically and elements retrieving from database like oracle
how to insert list box in java script dynamically and elements retrieving from database like oracle  hi all, how can i insert elements into java script list box retrieving from Database. whenever I insert any element in the Db
Database Scrolling Buttons
Database Scrolling Buttons  Sir, I have two button "Next" and "Previous". I'am able to retrieve data from database but I don't understand as to how display data on next and previous button clicks. Please help me out
problem Scrolling jTable in scrollpane
problem Scrolling jTable in scrollpane  hi i get into a problem of scrolling jtable in scrollpane.Only horizontal scroll is working, vertical scroll is not working...here is m code: try { PreparedStatement pst
problem Scrolling jTable in scrollpane
problem Scrolling jTable in scrollpane  hi i get into a problem of scrolling jtable in scrollpane.Only horizontal scroll is working, vertical scroll is not working...here is m code: try { PreparedStatement pst
scrolling a drawing..... - Swing AWT
scrolling a drawing.....  I am using a canvas along with other components like JTable over a frame the drawing which i am going to show over canvas is dynamic and hence there is no fixed size, that's why i need to make
add text box and select list dynamically and get its value to store it into database using jsp request parameter
add text box and select list dynamically and get its value to store it into database using jsp request parameter  its very helpful when you have only... added select box also into next jsp page plz reply me as early as posssible
problem scrolling jtable
problem scrolling jtable  hi, i've to query the table thousand of times...m table display is not getting stable/proper,especially after getting the empty table... for removing the table i'm doing this:: numrows
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box and a text box. I am sending an Array List object from a servlet to this jsp
displaying data for a single column from Mysql database in the list box in php form
displaying data for a single column from Mysql database in the list box in php form  I have a form in php.want to display data from a single column...('include/functions.php'); $Select = "SELECT * FROM category_master"; $Q = mysql
List
List  i do have one list object, i want to compare this list from database table and if match found i want to update the list
List
List  getting values from a form and make a list of them and store them into database
problem in scrolling SVG drawing in canvas - MobileApplications
problem in scrolling SVG drawing in canvas  hi all, i'm...- usually map's size is bigger than mobile screen , so i need to make scrolling over the drawing , scrolling works well in wireless toolkit emulator , but when i
Write an applet to display scrolling image in an applet window using thread.
Write an applet to display scrolling image in an applet window using thread.  Write an applet to display scrolling image in an applet window using thread
list boxs
list boxs  i have two list boxes first list box values from database and i want to assign selected options to the second list box using jsp is it possible to do this in jsp
JList box java
JList box java We are going to describe JList box java. First of all we have created JList component of Swing. List is a component of GUI() Graphical User Interface. It allows the multiple items in a list and In this class extend
combo box - JSP-Servlet
combo box  how to get a combo box in jsp page which allows editing as well as list box
combo box
combo box  Hi, [_|] dropdown box...] | | | LEFT LIST | | RIGHT LIST... a screen like this using jsp-servlet(or DAO,DTO),in that drop down box i should get
Show the Value of Scroll Bar in Java Swing
the value of scrolling position in the text box. The scroll bar has multiples... of these components then you get the value of scrolling positions. This program shows a scrollbar...: horizontal and vertical. The text field holds the value of scrolling position
input box
input box  give me the code of input box in core java
input box
input box  give me the code of input box in core java
HTML in the box
HTML in the box  Can I use any HTML in the box
How to display a record containing 30 fields in jsp without scrolling the page horizontally?
How to display a record containing 30 fields in jsp without scrolling the page horizontally?  I want to display records containing 30 fields in a table row.Is it possible in jsp without scrolling the page horizontally
Advertisements
 

 

 

DMCA.com