Disabling elements of a JList
Hello
I want to know..can I disable some elements of a JList (not the entire list)...I want few elements to be avaliable only on a certain condition to the user...if its possible..pls tell me how can i do that..thanks
hi
i did the cell rendering program and am able to change the status of each element of a JList...but i need to know how can I read each element..i am using JLabel for rendering each element...so can pls tell me how can I access the value of each JLabel
thanks
Similar Messages
-
How can i change the order of elements in a JList ?
Hi,
i would like to know if it is possible to change the order of elements in a JList ? Maybe i could use drag and drop to do this ?
Thanks for answering.The simplest way is probably to implement ur own listmodel and then supply the listitems in the order u want. Example:
class MyListModel
extends AbstractListModel
implements ContactListListener {
public int getSize() {
return listitemcount;
public Object getElementAt(int index) {
//here u return the item for the index at the list
//if u for example have an array of strings
//which u wanna have listed in reversed order (reversed to
//the order in the array, u could do
return myarray[getSize()-index];
} -
Can you set the color of a specific element in a JList?
Is it possible to set the color of a single element in a JList without writing lines and lines of your own class? I know setForgroundColor exists, it just changes all element in a JList, so is there a simple way to tweak it? i am still a newbie, so re-writing swing, would be a bit of a problem for me. Any Suggestions? thanks.
you write your own renderer, about 6-8 lines, setting the color determined by an
if/else statement - note: you must set the color in both if and else
search the forums for
"implements ListCellRenderer"
and you should find plenty of examples
if you just want the selected element a different color, you can do this with
UIManager.put("List.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.YELLOW));
but you must do this before you create the JList, and it will affect all JLists in your program. -
CommandButton actions not getting called when "disabled" element present
MyObjectForm.jsp contains commandButtons for "add", "update" and "delete" that are enabled/disabled according to the value of the bound id field.
MyObjectForm.jsp
<html>
<body>
<f:view>
<h:form id="create">
<h:inputHidden id="id" value="#{myObjectBean.id}" />
<h:panelGrid columns="3" border="0">
Name: <h:inputText id="name"
requiredMessage="*"
value="#{myObjectBean.name}"
required="true"/>
<h:message for="name"/>
// other fields
<h:commandButton id="add"
value="Add" disabled="#{myObjectBean.id!=0}"
action="#{myObjectBean.add}"/>
<h:commandButton id="update"
value="Update" disabled="#{myObjectBean.id==0}"
action="#{myObjectBean.update}"/>
<h:commandButton id="delete"
value="Delete" disabled="#{myObjectBean.id==0}"
action="#{myObjectBean.delete}"/>
<h:commandButton id="delete2"
value="Delete (no disabled element)"
action="#{myObjectBean.delete}"/>
</h:form>
</f:view>
</body>
</html>In its managed bean, MyObjectBean, if an id parameter is found in the request, the record is read from the database and the form is populated accordingly in an annotated @PostConstruct method:-
MyObjectBean.java
public class MyObjectBean {
private int id;
/** other properties removed for brevity **/
public MyObjectBean() {
LOG.debug("creating object!");
@PostConstruct
public void init() {
String paramId = FacesUtils.getRequestParameter("id");
if(paramId!=null && !paramId.equals("")){
getById(Integer.parseInt(paramId));
LOG.debug("init id:"+id);
}else{
public String delete(){
LOG.debug("delete:"+id);
MyObjectVO myObjectVO = new MyObjectVO();
ModelUtils.copyProperties(this, myObjectVO);
myObjectService.removeMyObjectVO(myObjectVO);
return "";
public String add(){
LOG.debug("add");
MyObjectVO myObjectVO = new MyObjectVO();
ModelUtils.copyProperties(this, myObjectVO);
myObjectService.insertMyObjectVO(myObjectVO);
return "";
public String update(){
LOG.debug("update:"+id);
MyObjectVO myObjectVO = new MyObjectVO();
ModelUtils.copyProperties(this, myObjectVO);
myObjectService.updateMyObjectVO(myObjectVO);
return "";
public void getById(int id){
MyObjectVO myObjectVO= myObjectService.findMyObjectById(id);
ModelUtils.copyProperties(myObjectVO, this);
/** property accessors removed for brevity **/
}When no parameter is passed, id is zero, MyObjectForm.jsp fields are empty with the "add" button enabled and the "update" and "delete" buttons disabled.
Completing the form and clicking the "add" button calls the add() method in MyObjectBean.java which inserts a record in the database. A navigation rule takes us to ViewAllMyObjects.jsp to view a list of all objects. Selecting an item from the ViewAllMyObjects.jsp list, adds the selected id to the request as a paramter and a navigation rule returns us to MyObjectForm.jsp, populated as expected. The "add" button is now disabled and the "update" and "delete" buttons are enabled (id is no longer equal to zero).
Action methods not getting called
This is the problem I come to the forum with: the action methods of commandButtons "update" and "delete" are not getting called.
I added an extra commandButton "delete2" to the form with no "disabled" element set and onclick its action method is called as expected:-
commandButton "delete2" (no disabled element) - works
<h:commandButton id="delete2"
value="Delete (no disabled element)"
action="#{myObjectBean.delete}"/>Why would "delete2" work but "delete", not?
commandButton "delete" (disabled when id is zero) - doesn't work
<h:commandButton id="delete"
value="Delete" disabled="#{myObjectBean.id==0}"
action="#{myObjectBean.delete}"/>The obvious difference is the "disabled" element present in one but not the other but neither render a disabled element in the generated html.
Am I missing something in my understanding of the JSF lifecycle? I really want to understand why this doesn't work.
Thanks in advance.
Edited by: petecknight on Jan 2, 2009 1:18 AMAh, I see (I think). Is the request-scoped MyObjectBean instantiated in the Update Models phase? If so then the id property will not be populated at the Apply Request Values phase which happens before this, making the commandButton's disabled attribute evaluate to true.
Confusingly for me, during the Render Response phase, the id property is+ set, so the expression is false (not disabled) giving the impression that the "enabled" buttons would work.
So, is this an flaw in my parameter passing and processing code or do you see a work around? -
HTML DB - Read only - Disable elements
Hi everyone,
I made a javascript to disable elements when my user is in a particular group. It's ok. But, I want my disable elements look a like "not disable elements". My disable elements are less attractive for the eyes. The only difference that I want is disabled := 'true' or disabled = 'false'.
I try also Read only.... but the frame of my elements disappears. How I can add attributes to those elements (check box, textfield, radio group button) to look a like "not disable elements" ?
Thanks. Bye.Hello,
Your css style is bad.
try something like this
style="border:4px solid #CCC;"
here is a nice primer on borders in css http://www.w3schools.com/css/css_border.asp
Carl -
How to copy elements from one JList to another JList?
I have 2 JLists. availableList and selectedList.
String availableData [] = {"A","B","C","D","E","F"};
JList availableList = new JList(availableData);
JList selectedList = new JList();
Now on 'Copy >>>' action I want the elements from availableList to be copied to selectedList (elements in the first JList should not be removed.)
on actionPerformed I used: selectedList.setListData(availableList.getSelectedValues()) but it is adding only one element at a time.
Can anyone help me in solving this problem? I want the elements from availableList to be copied to selevtedList and the duplicate should not happen. No need to sort the selectedList. just adding one by one.
Thanks in Advance!
Edited by: DeepakKotla on Oct 9, 2007 5:25 PMYou might want to use a model to make it easier to add and transfer data
like using DefaultListModel. -
Unbundle shows disabled element in the cluster
Hello Everybody
See the enclosed gif to see my problem.
The unbundle by name shows that the element of the cluster is disabled, whereas the "step by ste" unbundle by name shows all cluster element ready to use.
I have found the simmilar problem here:
simmlilar problem
but there was no answer for that.
Do you have any clues?
thanks in advance.
Pawel
Attachments:
unboundle.psp 161 KBHello,
I just want to add some more info to the subject, since I have found interesting stuff yesterday.
I enclose the control I am talking about.
The control contains cluster and inside the cluster there is another clusters. As I have shown in the previous post, some of the elements in the cluster are disabled. What is really funny in all of it, is that by adding or remowing some elements of the basic cluster, the disability is shifted to other cluster elements. Yes, just like that. Do it by your own, try to to add a boolean in the big - front pannel - cluster. Then do not forget to rearange the order of elements to have your control somewhere in the beggining. You will see that now other elements are disabled. What the heck it that??????
What
is interesting that, the disable view of the elements occur also in other sub-cluster. When you add the boolean the disability is also shifter. This shift equals the amount of controls you put to the cluster. I count that if I expand all clusters and treat their elements as one cluster, than the distance between disabled positions equals 9 !!!. This distance is unchanged.
I enclose two vi with the control I am talking about and with corresponding pictures.
What to do with it???
kind regards
Pawel
Attachments:
mistery.vi 102 KB
Image1.gif 46 KB
Image2.gif 49 KB -
Popup menus over elements in a JList
Hi,
How would I create a Popup menu when I select an element in a single selection only JList?
Is it possible to have the popup show on any selection event (keyboard, mouse, or even by programatically causing an element to be selected)?
thanks.I kind of got the behavior I want with doing following, but when I try to use this approach in an Applet, I get an ugly Java Applet Window message:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuTest extends JFrame implements ActionListener{
DefaultListModel lm = new DefaultListModel();
JList list = new JList(lm);
JScrollPane scroll = new JScrollPane(list);
JPanel p = new JPanel(new BorderLayout());
JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem;
public MenuTest(){
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lm.addElement("one");
lm.addElement("two");
lm.addElement("three");
lm.addElement("four");
getContentPane().add(p, BorderLayout.CENTER);
p.add(scroll, BorderLayout.CENTER);
menuItem = new JMenuItem("print to stdout");
menuItem.addActionListener(this);
popup.add(menuItem);
list.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
int index = list.locationToIndex(e.getPoint());
if (index>-1&&!(list.isSelectionEmpty())){
popup.show(e.getComponent(), e.getX(), e.getY());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
public void actionPerformed(ActionEvent e){
System.out.println((String) list.getSelectedValue());
public static void main(String args[]){
MenuTest window = new MenuTest();
window.setTitle("MenuTest");
window.setSize(200, 300);
window.setVisible(true);
It seems the behavior is different than for frames and in my GNOME desktop manager when I run my applet it opens a new window for the PopupMenu.
If anyone has run into this and has a work around please let me know.
thanks -
Disable Elements 8 Organizer due to using Lightroom and want to still use PSE
I have installed and now use Lightroom.
I still want to use Elements 8 Editor to further edit images.
Every image I edit ends up in the Elements Organizer.
I need help preventing my newly edited images from finding their way into Elements Organizer.The most efficient way to do this is to set PSE editor as your external editor in LR and make the round trip from there.
-
Disable Elements 10 from Automatically Opening when creating a PDF document using virtual Printer
Hi
I am using Primo PDF virtual printer to convert Excel document into PDF format. However, every time I attempt to do this, PS Elements 10 opens and imports the document. How can I turn this off?OK, I solved my own question... Simply changed the file association under windows. Sorry for wasting everyone's time
-
Hay guys,
I am working with a jlist and i want to disable selecting some of its elements does anyone know if there is a method to do this directly???
Thanks alotWell actually I was able to get around with my situation ....and
i would like to ask a different question now!
How can i change the foreground color of speacific elements in the jlist (i mean the ones which i have disabled thier actions ) ..
is there a way to do it using teh UIManager maybe or any other way??
Thanks alot , -
JLIst element deselection Problem
Hi,
I create a simple JList. I have multiple selection option in my JList.
But if I select some element in the JList it is getting "SELECTED".
But again I Click that "SELECTED" element it needs to be "DESELECTED".
But I am not able to do it.
Can any body help with some code.
shankhaBut I want to do it progrmatically. Can you provide some sample code to do this?Sure, read the API and you will find the method to do this. Search for methods containing "select".
-
Hi To all,
I am trying to get elements in a JList.All the things are going right but in the application it is not showing any data.
my code is like ...
JList jlist=new JList();
while(---code---)
//some code
Vector datalist = new Vector();
String fname=f.getName();
for(int i=0;(i<datalist.size());i++)
datalist.add(fname);
jlist.setListData(datalist);
System.out.println(datalist);
it is showing all names in dos prompt through System.out.println()..
but not through jlist.
Any help please
Thanks in advanceI'm having a problem with JList (it's not showing the
elements added to the List).
i've checked the forum, but couldn't found the answer
to my problem. Any suggestion on the forum search
keyword?
here's my code
public class TestClass extends JFrame{
public static void main(String args[]){
JFrame fra = new MyClass();
fra.show();
public class MyClass{
private JList list;
public MyClass(){
setSize(400,300);
list = new JList();
JSplitPane split = new
ew JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(tree),
new JScrollPane(fileList));
ContentPane c = getContentPane(c);
c.add(split);
} // end of constructor
public populateListAtRunTime(){
DefaultListModel listModel = new
ew DefaultListModel();
listModel.addElement("Testing 1");
listModel.addElement("Testing 2");
list = new JList(listModel);/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
With this line you are not changing the
list you have already added to the SplitPane
(I think it is already added before this gets
called, you never show the relationship of this
method to the constructor...), instead, you are
creating a new JList which you never add to
anywhere. Is there a way to instead add elements
to the JList's data model instead of creating a new
one? Something like this:
DefaultTableModel listModel = list.getListModel(); //Or something like it...
listModel.addElement(//...);
list.revalidate();
} // end of myClass (inner class)
The tree showed up in the split pane
And i think the list also showed up on the splitpane
also (except that the element value is not
there)...any clue on why this is happening?
sorry again..i know this topic must have been posted
numerous time, but i'm bad with search term
keyword..and did not find any result that come close
to my match
I tried JList + visible, JList + show, and a few
other -
I must preface this thread with the following: I have looked far and wide for answers and have come up dry, despite the Java Tutorials, other threads I looked through, threads that exist about this very issue that I apparently cannot locate, and all applicable APIs.
My issue is regarding the JList and right clicking within it in order to bring up a JPopupMenu; in turn, its JMenuItems should be able to alter the contents of the JList as defined in the subsequent, applicable code (whether that be deleting, changing properties of the item in question, or replacing it with another from some other source). I have come across the getComponent() method (<I>not</I> a getJComponent() method, which doesn't exist, except for in an Interface JBOField...whatever that is...). And though JList is a type of component, it would seem that its elements, on which I'd like to be able to right click, are not actual components; rather, they would be handled by a ListSelectionModel, or so I think. That said, the ListSelectionModel may aid in changing list contents, but will not help in the actual selection of those by right clicking.
Nevertheless, here is the code in question (with the appropriate and hopefully helpful annotations):
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class IngredientsListPopupMenu implements ActionListener {
// constants for JPopupMenu
private static final String MENU_NAME = "Selected Ingredients List";
private static final String ITEM_1 = "Edit Quantity";
private static final String ITEM_2 = "Delete Ingredient";
private static final String ITEM_3 = "Replace Ingredient";
private JPopupMenu listPopupMenu;
/* all of this "hubbub" below is part of the formatting I learned for
classes with multiple constructors perhaps it's considered too cumbersome? */
public IngredientsListPopupMenu() {
this(new JPopupMenu(MENU_NAME));
public IngredientsListPopupMenu(JPopupMenu jpm) {
createPopupMenu(jpm);
public void createPopupMenu(JPopupMenu jpm) {
jpm.add(createMenuItem(ITEM_1));
jpm.add(createMenuItem(ITEM_2));
jpm.add(createMenuItem(ITEM_3));
this.listPopupMenu = jpm;
private JMenuItem createMenuItem(String s) {
JMenuItem jmi = new JMenuItem(s);
jmi.addActionListener(this);
jmi.addMouseListener(mouseListener);
return jmi;
/* listens for mouse activity and shows menu if not already open
otherwise detects mouse activity for selecting menu items
AND here is where it gets "hairy"! */
private Component selectedComponent; /* not JComponent due to method call below.
I'm led to believe that there's nothing wrong with this variable type in and of itself,
whether or not I'm dealing with "Components" v. "JComponents" */
private MouseListener mouseListener = new MouseAdapterTest();
class MouseAdapterTest extends MouseAdapter {
public void mousePressed(MouseEvent e) {
checkPopup(e);
public void mouseClicked(MouseEvent e) {
checkPopup(e);
public void mouseReleased(MouseEvent e) {
checkPopup(e);
private void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
selectedComponent = e.getComponent(); /* gets the type of (in this
case) JComponent, which is rather general (and probably meant more
for when one is dealing with more than one type of component?), as I
do not know how to refer to an element in a JList here with this particular
method, which is probably not the way to go... */
listPopupMenu.show(e.getComponent(), e.getX(), e.getY());
public void actionPerformed(ActionEvent e) { // applies action on selected JComponent from checkPopup method above
String selection = e.getActionCommand(); // assigns JPopupMenu's selected text to a String
/* empty conditional statements due to them not being completely necessary
at this point, as I'd like to make sure the JPopupMenu displays properly before
writing the code for them */
if (selection.equals(ITEM_1)) {
// open up dialog to edit quantity
} else if (selection.equals(ITEM_2)) {
// delete ingredient from JList
} else if (selection.equals(ITEM_3)) {
// open up dialog to replace ingredient with another
public JPopupMenu getMenu() { // for a JScrollPane class
return this.listPopupMenu;
}In closing, I guess my question is: how do I take what I've got here and get that JPopupMenu to work with JList elements?Actually, I found out on my own that I did not need the public int locationToIndex(Point location) at all.
The issue was concerning the mouseListener and actionListener implementations. All's well now! Thanks, anyway. -
Hi,
I have a class that has the JList and its model(the DefaultListModel) . I initialize the JList with this model. I also have a second class that has a form with textarea and a button. i want to add the text from the textarea in this second class to the JList in the first class. Anytime i click the "add" button i don't see the text been added to the JList. Though I can print the elements of the list in the the second class. Meaning i can print in the second class the elements of the JList.
I read on the net that i should call "fireContentsChanged()" on the list model after i add the text to make it visible, but there is no such method. What should i do to make the added text visible in the list or to update the list so that the newly added text is visible?
Thanks.To get better help sooner, post a [_SSCCE_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.
Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
db
Maybe you are looking for
-
How can I upgrade to Maverick 10.7?
One week ago I purchased Mac OS X Snow Leopard 10.6.3 (upon the recommendation of a Genius). Now I discover that I require a higher version. How can I upgrade to Maverick 10.9 at no additional cost?
-
I need instructions on how to setup officejet 8500g
I need instructions on how to setup officejet 8500g This question was solved. View Solution.
-
Hi, At what level is general data for vendors (such as the address) managed? What user department is responsible? regards
-
Photoshop elements 9, getting runtime error help please !
I need to work on some photos bad and fast but cannot , getting a window popping up that says : Microsoft Visual C++ Runtime Library This application has requested to terminate it in an unusual way. Please contact application support team for mo
-
I have purchased my iphone in uae and now i m in india and my iphone hardware buttons are unreactive and i have to service or replace but i have lost my bill and on net there exist all info of my purchase can i service it in india without the bill