Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Camilla Kaasi
Greenhorn
+ Follow
1
Posts
1
Threads
0
Cows
since May 09, 2013
Camilla likes ...
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (1/100)
Number Threads Started (1/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (2/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (1/10)
Number Threads Started (1/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (2/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Camilla Kaasi
For input string: “” when textfields are filled out
I am writing a program in Java where I got some textfields and a button.
I get a java.lang.NumberFormatException: For input string: "" even though I have filled out all the textfields when running the program.
This is my code:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; public class Hjelp extends JFrame implements ActionListener{ private FlowLayout layout; private JTextField textfield; private JButton button1; private JButton button2; private JPanel menu, frontpage; private JPanel present, previous, something; public Hjelp() { layout = new FlowLayout(FlowLayout.CENTER, 10, 20); setLayout(layout); setSize(900, 900); setLocationRelativeTo(null); setVisible(true); setPanels(); something = something(); add(something, BorderLayout.CENTER); something.setVisible(false); button1 = new JButton("CLICK ME"); add(button1); buttonListener(); } private void buttonListener() { //Buttonlistener listener = new Buttonlistener(); button1.addActionListener(this); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { method(); } } ); } private void setPanels() { menu = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0)); frontpage = new JPanel(); previous = frontpage; present = frontpage; add(menu); } public void visiblePanel() { previous.setVisible(false); present.setVisible(true); } private JPanel something() { visiblePanel(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 1)); JTabbedPane tabbedPane = new JTabbedPane(); JComponent panel1 = tab(); tabbedPane.addTab("Click me", panel1); tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); panel.add(tabbedPane); return panel; } private JComponent tab() { JPanel panel = new JPanel(false); panel.setPreferredSize(new Dimension(870, 300)); panel.setLayout(new GridBagLayout()); GridBagConstraints cs = new GridBagConstraints(); cs.fill = GridBagConstraints.HORIZONTAL; textfield = new JTextField(15); cs.gridx = 0; cs.gridy = 0; cs.gridwidth = 1; panel.add(textfield, cs); button2 = new JButton("Click me"); cs.gridx = 1; cs.gridy = 6; cs.gridwidth = 1; panel.add(button2, cs); return panel; } public void method() { try { String string = textfield.getText(); if ( string.length() == 0) { System.out.println("Textfield is empty"); return; } int number = Integer.parseInt(string); } catch ( NumberFormatException e ) { System.out.println("Numberformatexception"); } } @Override public void actionPerformed( ActionEvent e ) { if ( e.getSource() == button1 ) { present = something; button1.setVisible(false); something(); previous = something; } } public static void main(String [] args) { final Hjelp vindu = new Hjelp(); vindu.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } } ); } }
show more
10 years ago
Swing / AWT / SWT