Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 02:14 PM   PM User | #1
Hein
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
Hein is an unknown quantity at this point
Java Listener problems

I have a console application with a few check boxes.
Depending on which checkbox are ticked the program works out which checked item has the longest lifetime.

So I would like to use an action listener to process an if statement that works out the lifetime as soon as the button is clicked.

Any ideas?
Hein is offline   Reply With Quote
Old 12-19-2012, 02:28 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can use an action listener if you want. I'd use an ItemListener since its easy to fetch the getStateChange from it and compare it to the SELECTED or DESELECTED constants.
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 02:38 PM   PM User | #3
Hein
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
Hein is an unknown quantity at this point
Ok well here is the code.
<code>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PCTGui6 extends JFrame implements ItemListener
{
Checkbox select1, select2, select3;
String output, insChosen;
double option1;
double option2;
float option3;

public PCTGui6()
{
super("PCT CALC!!");
JLabel label = new JLabel("Please select compounds used in the cycle");
select1 = new Checkbox("Anapolon", false);
select2 = new Checkbox("Anavar", false);
select3 = new Checkbox("Decca",false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label.setFont(new Font("Arial",Font.ITALIC, 22));
select1.addItemListener(this);
select2.addItemListener(this);
select3.addItemListener(this);
add(label);
add(select1);
add(select2);
add(select3);
}
public void itemStateChanged(ItemEvent select)
{

//to assign values
if(select1.getState()){option1 = 1.5;}
else {option1 = 0;}
if(select2.getState()){option2 = 1.5;}
else {option2 = 0;}
if(select3.getState()){option3 = 45;}
else {option3 = 0;}
System.out.println(""+option1);
System.out.println(""+option2);
System.out.println(""+option3);
repaint();
}
public static void main (String[] arguments)
{

final int FRAME_WIDTH = 500;
final int FRAME_HEIGHT = 500;
PCTGui6 frame = new PCTGui6();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);



}
}
</code>
Hein is offline   Reply With Quote
Old 12-19-2012, 03:37 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Don't mix the AWT and Swing components together. Use JCheckBox instead of Checkbox.

I think I misunderstood what you wanted here, but the code doesn't really show what you want either. Are you trying to pick out the highest value based on the selected items? I can think of an easy way to do that with Hashtables, or a bound model on a JList instead (checkboxes don't actually store a value to them).
Fou-Lu is offline   Reply With Quote
Old 12-19-2012, 07:15 PM   PM User | #5
Hein
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
Hein is an unknown quantity at this point
Well that would be easier to use something that already has the value.
Because currently what I would like to use is the checkbox boolean to assign the value if its true.
But your way seems easier!
Hein is offline   Reply With Quote
Old 12-19-2012, 08:23 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is this for a school assignment or for something non-educational? The class name make it look almost like an assignment.

I can show you how to do it with the JList, but if its for an assignment I can pretty much guarantee its not covered (so of course that is a nice way of saying you can't just use it). It also only uses strings for the data; you would want to use objects so you can store the name and size together and fetch them as necessary (can also be used if the Model is custom implemented off of an extended collection class since you can then use the Collections.min in conjunction with an object of type Comparable). But it would give you the overall idea.

If you are doing this for a class, there is nothing wrong with checking each item individually as you've done. If you are looking for a max value, you simply keep track of what the max is, and replace it as necessary as you check each checkbox item. The override handling of the JList's primary advantage over that is that it will:
1. Combine the display to the value you want to contain within it (as you will be using an object). So you can retrieve it at any time from the model
2. It allows you to simply add to it without needing to modify the underlying code.

Okay, so here's a quick example of that:
PHP Code:
    public static void main(String[] argv)
    {    
        
SwingUtilities.invokeLater(new Runnable(){
            @
Override
            
public void run()
            {
                
JFrame jf = new JFrame();
                
Vector<StringvOptions = new Vector<String>();
                final 
JList jl = new JList(vOptions);
                
// This will make it "look" like a checkbox
                
jl.setCellRenderer(new ListCellRenderer()
                {
                    @
Override
                    
public Component getListCellRendererComponent(JList list, Object value,
                            
int indexboolean isSelectedboolean cellHasFocus)
                    {
                        return new 
JCheckBox(value.toString(), isSelected);
                    }
                });
                
                
// This will make it "act" like a checkbox
                
jl.setSelectionModel(new DefaultListSelectionModel()
                {
                    private static final 
long serialVersionUID 673462916111720119L;
                    private 
boolean isStarted false;
                    @
Override
                    
public void setSelectionInterval(int idx0int idx1)
                    {
                        if (!
isStarted)
                        {
                            if (
isSelectedIndex(idx0))
                            {
                                
super.removeSelectionInterval(idx0idx1);
                            }
                            else
                            {
                                
super.addSelectionInterval(idx0idx1);
                            }
                        }
                        
isStarted true;
                    }
                    
                    @
Override
                    
public void setValueIsAdjusting(boolean isAdjusting)
                    {
                        if (!
isAdjusting)
                        {
                            
this.isStarted false;
                        }
                    }
                });
                
                
// This will simply print out when you select something in the list
                
jl.addListSelectionListener(new ListSelectionListener()
                {
                    @
Override
                    
public void valueChanged(ListSelectionEvent e)
                    {
                        for (
int is jl.getSelectedIndices())
                        {
                            
System.out.println(jl.getModel().getElementAt(is) + " is selected.");
                        }
                        
System.out.println();
                    }
                });
                
                
vOptions.add("Anapolon");
                
vOptions.add("Anavar");
                
vOptions.add("Decca");
                
jf.getContentPane().add(jl);
                
                
jf.pack();
                
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
jf.setVisible(true);
            }
        });
    } 
When run you should see what appears to be 3x checkboxes. Checking them will send to system.out the values you have checked (so launch from the command line to see it).

I'm not sure if this is for a school assignment, but if it is then I'd say overriding the renderers and using collections (and possibly arrays?) would be out of the question, but only you can confirm if that's the case and if so where you are. Sometimes simplicity does have its place, especially in the learning process (albeit ultimately being way more code; I could add another 20 items to that list at a cost of only 20x more lines, but to do the same with just variables would require the variables + the code to process them).
Fou-Lu is offline   Reply With Quote
Old 12-20-2012, 06:51 AM   PM User | #7
Hein
New Coder

 
Join Date: Feb 2012
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
Hein is an unknown quantity at this point
No its not a school assignment its a personal project.
Well I have no idea whats going on in this code, I think its above my level of java knowledge.
But thanx I will work through it and hopefully get some good knowledge out of it!
Hein is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 AM.


Advertisement
Log in to turn off these ads.