Simple problem with Swing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Simple problem with Swing

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Simple problem with Swing

    All I want to do is enter a value in a text field, press a button to submit that value, and then repaint the page so that a message is given based on that value. Everything I try gives me errors. I can't figure out how to get the submitted value as a label to use on the second screen. Can someone point in the right direction. Below is my code:

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

    public class WebCheckerMe implements ActionListener {

    JFrame frame;

    public static void main (String[]args){
    WebCheckerMe gui = new WebCheckerMe();
    gui.go();
    }

    public void go(){
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Enter URL to monitor");
    JTextField textfield = new JTextField(20);
    JButton button = new JButton("Submit");
    button.addActionListener(this);


    panel.add(label);
    panel.add(textfield);
    panel.add(button);

    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.setSize(300,300);
    frame.setVisible(true);


    }

    public void actionPerformed(ActionEvent event){
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label2 = new JLabel("You have entered:");
    JButton button = new JButton("Submit");
    button.addActionListener(this);

    panel.add(label2);
    panel.add(button);

    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.setSize(300,300);
    frame.setVisible(true);
    }

    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Like this ?

    I don't think you want to make a new frame for every button click (?). I also
    changed some bits here and there just to be picky...
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class WebCheckerMe
        implements ActionListener {
      JTextField textfield=null;
      JFrame frame;
      JLabel label2 = new JLabel();
      JButton closeBtn=new JButton("Close");
      JButton submitBtn = new JButton("Submit");
    
      public static void main(String[] args) {
        WebCheckerMe gui = new WebCheckerMe();
        gui.go();
      }
    
      public void go() {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        JLabel label = new JLabel("Enter URL to monitor");
        textfield = new JTextField(20);
    
        submitBtn.addActionListener(this);
        closeBtn.addActionListener(this);
        panel.add(label);
        panel.add(textfield);
        panel.add(submitBtn);
        panel.add(label2);
    
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.getContentPane().add(closeBtn, BorderLayout.SOUTH);
        frame.setSize(450, 300);
        frame.setVisible(true);
    
      }
    
      public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("Submit")) {
          label2.setText("You have entered:" + textfield.getText());
        } else if (event.getActionCommand().equals("Close")) {
          System.exit(0);
        }
      }
    }
    Last edited by sjalle; 02-27-2005 at 01:25 PM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center