Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am very new in making JAVA swing applications..

My problem statement is to make a library management application,for which i designed classes as below.

Please tell me whether my design of classes are right, and possible corrections and improvements.

Requrements:

  1. Register new students.
  2. Search with ISBN of book.(Number)
  3. Borrow a book.

As we can have only 1 JFrame and add and remove panels to it, my idea is to have a singleton class named Frame.java to return a JFrame.

Frame.java

public class Frame extends JFrame {

   JFrame app_main_frame=new Frame();

   private Frame()
   {    
   }

   pubic static getJFrame()
   {    
       return app_main_frame;    
   }    
 }

Book.java class:

public class Book {

    private String name;
    private long int ISBN;
    private String author_name;
    private boolean borrowed;
    private String borrower_id;

    public getName()
    {    
    }

    public setName(String book_name)
    {
           name=book_name;
    }

    // and all other get and set methods are written
}

Library.java ( singleton class)

public class Library {

    Library lib = new Library();

    private Library()
    {
    }      

    public static getLibrary()
    {
        return lib;
    }

    public static void Main(String args[])
    {
        JFrame frame = frame.getJFrame();      // Get JFrame
        LoginPanel login = new LoginPanel();  // Get Login Panel object
        login_panel=login.getPanel();         // Get Panel of login. 
        frame.add(login_panel);               // Add panel to JFrame
    }

    private HashMap<int,Book> books;   

    public Book addBook(long int id,String Name,String author_name)
    {
        Book book = new Book(id,Name,author_name);
        books.put(int,book);
    } 

    public Book find(int id)
    {
        return books.get(id);
    } 
}

LoginPanel.java

public class LoginPanel {

    JPanel login_panel;
    JLabel username,password;
    JTextField username_text,password_text;
    Button button;

    public LoginPanel()
    {
        login_panel.add(username);
        login_panel.add(username_text);
        login_panel.add(password);
        login_panel.add(password_text);
        login_panel.add(button);

        button.addActionListener(this);      
    }

    public getPanel()
    {
        return login_panel;
    }

    public void ActionPerformed(Event e)
    {
        JFrame frame = frame.getJFrame();      // Get JFrame
        HomePanel home = new HomePanel();  // Get Login Panel object
        frame.add(home.getPanel());             
    }
}

HomePanel.java

public class HomePanel {

    JPanel home_panel;
    Button b;
    JTextField search;    

    public HomePanel()
    {
        home_panel.add(b);
        home_panel.add(search);
        b.addActionListener(this);   // add a text field and a button to panel 
    }

    public getPanel()
    {
        return home_panel;
    }

    public void ActionPerformed(Event e)
    {
        String  search_text = search.getText();
        Library lib = Library.getLibrary();
        book b=lib.find(search_text);

        // Do something with the book found... 
    }    
}

Please help me improve design of classes.

Thanks in advance. :)

Don't look after compilation errors...didn't compile.

share|improve this question
2  
didn't compile. This a bad. You should have a working starting point. – tb- Feb 1 at 19:22
As we can have only 1 JFrame. Even if that's true in this case, it isn't in general, and as such you won't find the class useful in the future (and also, Singletons should be final or it can be overridden which defeats the purpose). – MrLore Feb 1 at 19:35
using a singleton on a JFrame is not a good idea. However using a singleton on a JPanel would not be a bad idea. You then could keep your JFrame alive the entire time. Have the JPanel handle its own button presses so that you can just dispose of that panel, and create a new one and set it visible. Let us know. – Robert Snyder Feb 1 at 20:25
In my application..initially it shows the login page.when student logins,it shows home page.So we need to change panel in a single JFrame from loginPanel to homePanel.How to have a single JFrame used by everyone without making it a singleton class. – abhinav Feb 2 at 3:30

closed as off topic by palacsint, Jeff Vanzella, Glenn Rogers, Yuushi, Corbin Feb 5 at 2:01

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

Ok so it took me a little bit to come up with a very crude mock up of what i was saying. And you'll have to forgive me for my java is a little rusty (been doing a lot of C# and C++ programming lately, and not everything is coming back to me). I'll go in order

MainApp

public class JavaApplication1 implements LoginListener
{
    public JavaApplication1()
    {

        mjf = new MainJFrame();
        LoginPanel logMeIn = new LoginPanel();
        logMeIn.addEventListener(this);

        mjf.getContentPane().add(logMeIn);

        mjf.pack();
        mjf.setVisible(true);

    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        new JavaApplication1();

    }
    MainJFrame mjf;

    @Override
    public void UserLoggedIn(String username)
    {
        BooksPanel bp = new BooksPanel(username);
        mjf.getContentPane().removeAll();
        mjf.getContentPane().add(bp);

        mjf.pack();
    }
}

Probably could be moved to main program

public class MainJFrame extends JFrame
{
    public MainJFrame()
    {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Now the meat. both JPanels

public class LoginPanel extends JPanel implements ActionListener
{
    Label lbl = new Label("Log in here");
    JTextField username = new JTextField();
    JTextField password = new JTextField();

    JButton login = new JButton("Login");

    public LoginPanel()
    {
        username.setPreferredSize(new Dimension(100, 25));
        this.add(lbl);
        this.add(username);
        this.add(password);
        this.add(login);
        login.addActionListener((ActionListener)this);
    }
  private List _listeners = new ArrayList();
  public synchronized void addEventListener(LoginListener listener)
  {
    _listeners.add(listener);
  }
  public synchronized void removeEventListener(LoginListener listener)
  {
    _listeners.remove(listener);
  }

  // call this method whenever you want to notify the event listeners of the particular event
  private synchronized void fireEvent(String name)
  {
        for(Object logger : _listeners)
            ((LoginListener)logger).UserLoggedIn(name);
  }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
        if(username.getText().equals("Robert"))
            fireEvent("Robert");
    }
}

public class BooksPanel extends JPanel
{
    public BooksPanel(String name)
    {
        this.add(new Label("All my books"));
        this.add(new Label("Welcome " + name));
    }
}

So the idea is that I have my main program take care of the logic of which Panel to display. The Panels just need to take care of what they take care of, and NOTHING else. As you can see, i only have one JFrame, and I get to separate the program logic for each panel in their respective class, and the let the main program figure out what to display. There is probably a better way to switch between the Panels (such as CardLayout) but I only wanted to mock up what i was on about. I hope this helps you.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.