Results 1 to 5 of 5
Thread: Converting int to string or??
- 04-04-2012, 02:10 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Converting int to string or??
I'm completely new to java and this forum so please don't rip me to shreds here! I began writing a this code that would convert one unit of measure to another. I wanted to use GUI, there is my code. But im having problems converting the input of the user from text to int. here is the code.
Java Code:import java.awt.event.*; import javax.swing.*; public class GUI { private JFrame frame; private JButton EnterButton; private JButton CancelButton; private JButton ConvertButton; private JLabel label; private JPanel panel; private JTextField Convert ; public GUI(){ // Create and show GUI frame = new JFrame(); frame.setSize(300, 200); frame.setAlwaysOnTop(true); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new JPanel(); frame.add(panel); // Create items EnterButton = new JButton("Enter"); CancelButton = new JButton("Cancel"); ConvertButton = new JButton("Convert"); Convert = new JTextField("....",20); //Place items on panel panel.add(Convert); panel.add(EnterButton); panel.add(ConvertButton); panel.add(CancelButton); // Cancel Button class CancelTrig implements ActionListener{ public void actionPerformed(ActionEvent e){ System.exit(0); } } CancelButton.addActionListener(new CancelTrig()); // Convert Button class ConvertTrig implements ActionListener{ public void actionPerformed(ActionEvent e){ int i = Integer.parseInt(Convert.getText()); Convert.setText(i); } } ConvertButton.addActionListener(new ConvertTrig()); } }
- 04-04-2012, 02:12 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
Re: Converting int to string or??
- 04-04-2012, 03:01 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Converting int to string or??
Changed up the code a little bit, worked it out. Not getting any error seems to be right..
Java Code:CancelButton.addActionListener(new CancelTrig()); // Convert Button class ConvertTrig implements ActionListener{ public void actionPerformed(ActionEvent e){ int i = 0; try { i = Integer.parseInt(Convert.getText().toString()); } catch(NumberFormatException nfe){ label = new JLabel("Problem with: " + nfe); frame = new JFrame(); frame.setSize(450,100); frame.setAlwaysOnTop(true); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.add(label); } } }
- 04-04-2012, 03:09 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Converting int to string or??
String s;
s = Integer.toString(i);
i is the integer to be converted.
- 04-06-2012, 01:47 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
converting binary to string
By stegano in forum New To JavaReplies: 5Last Post: 03-27-2011, 08:20 PM -
Converting to String
By darek9576 in forum New To JavaReplies: 1Last Post: 03-13-2010, 11:07 PM -
Is it OK to do this... (converting int to string)
By Psyclone in forum New To JavaReplies: 1Last Post: 02-16-2010, 06:51 PM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 09:03 PM
Bookmarks