Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a separate Java class?

Status
Not open for further replies.

Cybertronic

Technical User
May 18, 2006
19
Hi all,

In my previous thread I posted a few days ago about JEditorPane regarding my Java web browser, now I've got another problem which is that I would like to separate some of my coding into several classes to prevent my code from getting too long.

Below is my current code I have for my browser constructor:

Code:
public class browser extends JFrame implements ActionListener, HyperlinkListener
{
  // Private GUI components initialisation

  // Constructor for browser.
  public browser()
  {
    browserMenu = new JMenu("File");

    newMenuItem = new JMenuItem("New");
    newMenuItem.addActionListener(this);

    openMenuItem = new JMenuItem("Open");
    openMenuItem.addActionListener(this);

    fileChooser = new JFileChooser();

    printMenuItem = new JMenuItem("Print");
    printMenuItem.addActionListener(this);

    exitMenuItem = new JMenuItem("Exit");
    exitMenuItem.addActionListener(this);

    browserMenu.add(newMenuItem);
    browserMenu.add(openMenuItem);
    browserMenu.add(printMenuItem);
    browserMenu.addSeparator();
    browserMenu.add(exitMenuItem);

    favouritesMenu = new JMenu("Favourites");

    favMenuItem1 = new JMenuItem("fav1");
    favMenuItem1.addActionListener(this);

    favMenuItem2 = new JMenuItem("fav2");
    favMenuItem2.addActionListener(this);

    favMenuItem3 = new JMenuItem("fav3");
    favMenuItem3.addActionListener(this);

    favMenuItem4 = new JMenuItem("fav4");
    favMenuItem4.addActionListener(this);

    favMenuItem5 = new JMenuItem("fav5");
    favMenuItem5.addActionListener(this);

    favouritesMenu.add(favMenuItem1);
    favouritesMenu.add(favMenuItem2);
    favouritesMenu.add(favMenuItem3);
    favouritesMenu.add(favMenuItem4);
    favouritesMenu.add(favMenuItem5);

    menuBar = new JMenuBar();
    menuBar.add(browserMenu);
    menuBar.add(favouritesMenu);
    setJMenuBar(menuBar);

    toolBar = new JToolBar();

    addressLabel = new JLabel(" Address: ");

    addressTextField = new JTextField(30);
    addressTextField.addActionListener(this);
    
    homePageButton = new JButton("Home");
    homePageButton.addActionListener(this);

    searchButton = new JButton("Search");
    searchButton.addActionListener(this);

    printButton = new JButton("Print");
    printButton.addActionListener(this);

    goButton = new JButton("Go");
    goButton.addActionListener(this);

    toolBar.add(homePageButton);
    toolBar.add(searchButton);
    toolBar.add(printButton);
    toolBar.add(addressLabel);
    toolBar.add(addressTextField);
    toolBar.add(goButton);

    try
    {
      // display web page
    }
    catch (IOException ioe)
    {
      // display error
    }

    contentScrollPane = new JScrollPane(contentPane);

    contentPane.setEditable(false);
    contentPane.addHyperlinkListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(toolBar, BorderLayout.NORTH);
    getContentPane().add(contentScrollPane, BorderLayout.CENTER);

    setTitle("Java Web Browser");
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
  }
  
  ...

}

I have other constructors in the same browser class which is why I would like to separate them into different classes to ease of coding.

How can I convert all my GUI declarations, properties, etc in a separate class file? Will I need to use the "extends" in the separate class method?

I appreciate anybody's assistance :)
 
You need to read up on Object Oriented design principles really. There are many, many ways to refactor a large monolithic class down into multiple classes, but good design principles should be applied to do it satisfactorily. There is a specialist forum for this here at forum 678, and I'm sure the web will have many articles on this subject.

Briefly, though, you need to consider what are the major 'actors' in your code and what are 'their' roles. One approach is to write in words what your program is trying to achieve. The general idea then is that the nouns indicate classes and the verbs indicate methods on them. Some actors may be a specialised form of other, more generalised, actors and this may indicate one class extending another, or maybe better, one class implementing an interface.

This stuff transcends pure Java programming and I think, as such, is beyond the scope of this Java forum. That's only my opinion though.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top