Results 1 to 7 of 7
  1. #1
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Arrow New to java, in a pickle!

    Hello there all,
    I'm fairly new to java and due to the alleged "research" nature of my uni course will no doubt be here often in the near future :)

    I have some work set for groups of 5, i was in the last group so we had 4, one we dont know so 3, the other isnt reachable so 2 and my companion is pre-occupied for the next 6 days so its just me and theres 10 days to go and i'm out of the country for the next 6 the morning after next (but will try and keep doing all i can on my notebook to keep this project rolling).

    Okay, so being in the thick of it i'm finding it near impossible to find answers to what i'm assuming are rather simple issues, such as for instance, i have started creating classes (such as a login class) yet can find direction as to how it can be "launched" if you will from the main class.

    I dont really have any relevant code to show, i just need to see an example of a simple main class which when executed will open another Jframe and get the ball rolling :|

    I prey someone can give me a lead as i'v been head banging for two nights now and am reaching dispare.

    Hopefully i'll be able to return the favour once i get meyself to a level in which i can help others :)

    Best regards

    -Jvr

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're likely to find a cornucopia of examples and lessons here: Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

    One way to do what you want is to create a main app that is in a JFrame and pack it but don't set it visible. Then have it show a modal JDialog that is the login window. Then depending on the results of the login, the main app either sets itself visible or disposes itself. For details, check out the link above. Good luck.

  3. #3
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Still no joy here.
    Reverting back to google after a few hours i came across "instancing" super and calling that from main...

    If this is anywhere in the right direction could anyone just let me know... If this fails i'm beat... till tomorow *sighs*.

    I have to say i'm baffled at how little we'v been taught and how much is asked of us at this point in the course :|

    -Jvr

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Reverting back to google after a few hours i came across "instancing" super and calling that from main...
    this is not likely to help you.

    I have to say i'm baffled at how little we'v been taught and how much is asked of us at this point in the course
    the further on you go the more you will understand the your education is solely your responsibility. The teachers can help guide you, but ultimately it is up to you to either follow that guidance or failing that find your own way. Best of luck and wishing you success.

  5. #5
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    hey,
    I can see how that last statment could be misinterperated.
    I guess its mostly tacking such a large project myself (intended for a group of 5) and grappling with the language thats dulling my spirits but i'm fully aware that our education is in our own hands and i'm usually the one singing the same tune to others who consider the work "set" is the b all and end all.

    Everyones still working on their uml diagrams but i fear even in groups of 5 many will get a shock when it comes to coding, heck we'v never been guided to more than simple single class programs :|

    I'v just been at this for way to long non stop and its getting me frustrated, and i'm rambling.

    I'll get up in the morning and tackle it again.

    Thanks for the heads up on instancing, i'm going to call it a night and I'll need that luck in the morrow.

    regards
    -Jvr

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    ah heck. bad me.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class FuSwing
    {
    
      private static final String USER_NAME = "Fubar";
      private static final String PASSWORD = "Snafu";
      // shame on me.  You should probably not 
      // hard-code a password in your code.
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("Main JFrame");
        frame.getContentPane().add(new MainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(false);
    
        // ceate logon JPanel
        LogonPanel logon = new LogonPanel();
        String title = "Log On";
        boolean continueLogOn = true;
        while (continueLogOn)
        {
          // show logon JPanel in a JOptionPane
          int result = JOptionPane.showConfirmDialog(frame, logon, title,
              JOptionPane.OK_CANCEL_OPTION, JOptionPane.DEFAULT_OPTION);
          if (result == JOptionPane.OK_OPTION)
          {
            // extract info out of logon panel
            if (USER_NAME.equalsIgnoreCase(logon.getUserName())
                && PASSWORD.equalsIgnoreCase(new String(logon.getPassword())))
            {
              frame.setVisible(true);
              continueLogOn = false;
            }
            else
            {
              signonError(frame);
              logon.clear();
            }
          }
          else
          {
            frame.dispose();
            continueLogOn = false;
          }
        }
      }
    
      private static void signonError(JFrame frame)
      {
        JOptionPane.showMessageDialog(frame,
            "Try \nUser Name:  " + USER_NAME +
            "\nPassword:  " + PASSWORD,
            "Wrong User Name and Password", JOptionPane.INFORMATION_MESSAGE);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    
    class MainPanel extends JPanel
    {
      JLabel label = new JLabel("Main Panel", SwingConstants.CENTER);
    
      MainPanel()
      {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400, 400));
        add(label);
      }
    }
    
    class LogonPanel extends JPanel
    {
      private JTextField userNameField = new JTextField(9);
      private JPasswordField passwordField = new JPasswordField(9);
    
      LogonPanel()
      {
        JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, 10));
        JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, 10));
        
        labelPanel.add(new JLabel("User Name:"));
        labelPanel.add(new JLabel("Password:"));
        fieldPanel.add(userNameField);
        fieldPanel.add(passwordField);
        
        setLayout(new BorderLayout(10, 10));
        add(labelPanel, BorderLayout.LINE_START);
        add(fieldPanel, BorderLayout.CENTER);
        
      }
    
      public void clear()
      {
        userNameField.setText("");
        passwordField.setText("");
      }
    
      public String getUserName()
      {
        return userNameField.getText();
      }
    
      public char[] getPassword()
      {
        return passwordField.getPassword();
      }
    }

  7. #7
    skatefreak is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Core blimey,
    I should have come back the other day heh.
    I stuck with it and made it most of the way your example shows although i need to be able to create new users and multiple users to log in heh.

    Your example however is very much apprecited as it shows alternative methods to what I implemented and i have to say is much sharper :)

    Thankyou for putting up with me here,
    i was extremely frustrated by the point i posted as i had spent nearly a whole day with no progress, after the following dy making some progress i have a much more positive outlook :)

    I'll start another thread to discuss methods to read files :)

    Much thanks though :)

    Best regards

    -Jvr
    Last edited by skatefreak; 01-14-2009 at 10:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •