Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java: Getting my combobox to give me an integer? [UPDATED]
New Posts  All Forums:Forum Nav:

Java: Getting my combobox to give me an integer? [UPDATED]

post #1 of 12
Thread Starter 
Quick question. my combo box called priority has 5 values in it. The numbers 1-5. I was hoping that when I pressed a button it would trigger an event... This.

Code:
int priorityValue = priority.getSelectedItem();
Only I get an error telling me that the combobox is returning an object. Any simple way around this problem??

Thanks...
Edited by kidshenlong - 3/11/11 at 5:15am
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 12
Thread Starter 
Okay guys. Update here. I managed to get it to grab my selection using this

Code:

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == update) {
            int priorityValue = priority.getSelectedIndex();
            MessageData.setPriority(id, priorityValue);
            displayMessage();
           }
Only thing is now when I enter 1 it gives me zero and when I enter 5 it gives me 4!!!

Solutions anyone??
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #3 of 12
I wish I could see more of what you're trying to do here. "When I enter 1 it gives me zero" doesn't really tell me anything.

It might be worth mentioning that when you getIndex, it's the same thing as pulling data from an array. The first points of data are stored at point 0, not 1.
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #4 of 12
Thread Starter 
Well, here's all the code for the class

Code:

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

public class ReadMessage extends JFrame
        implements ActionListener {

    private String id;
    private JComboBox priority = new JComboBox();
    private JButton update = new JButton("Update");
    private JButton close = new JButton("Close");
    private JTextArea textArea = new JTextArea();
    private JScrollPane scrollPane = new JScrollPane(textArea);

    public ReadMessage(String id) {
        this.id = id;

        setLayout(new BorderLayout());
        setSize(500, 250);
        setResizable(false);
        setTitle("Message Details");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel top = new JPanel();
        top.add(new JLabel("Enter Priority (1-5):"));
        priority.addItem("1");
priority.addItem("2");
priority.addItem("3");
priority.addItem("4");
priority.addItem("5");
top.add(priority);
        top.add(update);
        update.addActionListener(this);
        top.add(close);
        close.addActionListener(this);
        add("North", top);

        textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setPreferredSize(new Dimension(450, 150));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(450, 150));

        JPanel middle = new JPanel();
        middle.add(scrollPane);
        add("Center", middle);

        displayMessage();
        
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == update) {



            int priorityValue = priority.getSelectedIndex();
            MessageData.setPriority(id, priorityValue);
            displayMessage();
        } else if (e.getSource() == close) {
            dispose();
        }
    }

    private void displayMessage() {
        String subject = MessageData.getSubject(id);
        if (subject == null) {
            textArea.setText("You did not enter a message in the ID in the previous field or your message does not exist!");
        } else {
            textArea.setText("Subject: " + subject);
            textArea.setText("From: " + MessageData.getSender(id));
            textArea.append("\
To: " + MessageData.getRecipient(id));
            textArea.append("\
Priority: " + MessageData.stars(MessageData.getPriority(id)));
            textArea.append("\
\
" + MessageData.getMessage(id));
        }
    }
}
Well I might've done the wrong thing I'm not too sure... I was thinking about that point of data thing... I'm not too sure bth I've probably used getmothod tbh...
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #5 of 12
I'm at work right now, so I can help more in a little when things settle down. Just glancing at the code, have you tried:

int priorityValue = priority.getSelectedIndex() + 1;
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #6 of 12
Also worth mentioning what program you are using to program this?

VB 6? etc?
post #7 of 12
Quote:
Originally Posted by shadow19935 View Post
Also worth mentioning what program you are using to program this?

VB 6? etc?
what? lol, that's Java
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #8 of 12
Thread Starter 
That worked perfectly! Kinda feel like I'm cheating though lol any idea why it does this?? And Shadow I'm using edit plus 3
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #9 of 12
Quote:
Originally Posted by Plex View Post
what? lol, that's Java
VB6 = Visual Basic 6. thats what we use at school :/ Sorry for getting that wrong
post #10 of 12
Quote:
Originally Posted by shadow19935 View Post
VB6 = Visual Basic 6. thats what we use at school :/ Sorry for getting that wrong
Oh, not a problem. Yes, I'm aware what VB means . You are talking about the "IDE," for future reference. But in this case, the IDE is named the same thing as the language itself, which would be visual basic.

I hope that made some sense .
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java: Getting my combobox to give me an integer? [UPDATED]