factorising code!!

Please can anyone help me?! I'm trying to factorise the code below, so that when I run BinTest5a, bu clicking the "next" button, BinTest5b is loaded. HOWEVER, at present, this link between the classes is working, but I want to factorise our the common code so that all the building of the menus, buttons etc stays the same, but with the center text area changing each time (as opposed to loading up an entire new class with duplicate code)...

Please help as I'm really new to Java+stuck!! As you can see, the classes are really short, so if anyone has a few minutes spare..your help would be appreciated!!

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

public class BinTest5a extends JFrame implements ActionListener {

private String newline = "\n";

public BinTest5a()

{

JMenuBar menubar= new JMenuBar();

setJMenuBar(menubar);

JMenu topic1 = new JMenu("Topic 1");

JMenu topic2 = new JMenu("Topic 2");

menubar.add(topic1);

menubar.add(topic2);

JMenuItem test = new JMenuItem("Test Menu");

topic1.add(test);

JTextPane tp = createTextPane();

tp.setEditable(false);

JScrollPane psp = new JScrollPane(tp);

psp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

psp.setPreferredSize(new Dimension(700,600));

JPanel right = new JPanel();

right.add(psp);

right.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("TUTORIAL"), BorderFactory.createEmptyBorder(5,5,5,5)));

JPanel contentPane = new JPanel();

BoxLayout box = new BoxLayout(contentPane, BoxLayout.X_AXIS);

contentPane.setLayout(box);

contentPane.add(right);

Container pane=getContentPane();

pane.add(contentPane);

JButton back = new JButton("<<back");

JButton next = new JButton("next>>");

next.addActionListener(this);

JPanel west = new JPanel(new GridLayout(3,1));

west.add(back);

west.add(next);

contentPane.add(west, BorderLayout.WEST);

}

public void actionPerformed(ActionEvent e)

{

JButton button = (JButton) e.getSource();

String label = button.getText();

if (label.equals("next>>"))

{

BinTest5b next = new BinTest5b();

//next.setVisible(true);

next.setSize(600,600);

next.pack();

next.show();

//next.setExtendedState(JFrame.MAXIMIZED_BOTH);

this.dispose();

}

}

private JTextPane createTextPane()

{JTextPane tp = new JTextPane();

String [] initString = {"This is a FIRST PAGE , ",

"for MATHS ",

"learning ",

"tools ",

"which suports embedded graphs... " ,

" " + newline,

"...and embedded icons.." + newline,

newline + "JTextPane." };

String[] initStyles = { "regular", "bold", "small", "large", "regular", "image" ,"regular", "regular"};

initStylesForTextPane(tp);

Document doc = tp.getDocument();

try { for (int i=0; i< initString.length;i++)

{ doc.insertString(doc.getLength(), initString,

tp.getStyle(initStyles));

}

} catch (BadLocationException ble)

{ System.err.println("couldn't insert"); }

return tp;

}

protected void initStylesForTextPane(JTextPane tp)

{Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

Style regular = tp.addStyle("regular", def);

StyleConstants.setFontFamily(def, "SansSerif");

Style s = tp.addStyle("italic", regular);

StyleConstants.setItalic(s, true);

s = tp.addStyle("bold", regular);

StyleConstants.setBold(s, true);

s = tp.addStyle("small", regular);

StyleConstants.setFontSize(s, 10);

s = tp.addStyle("large", regular);

StyleConstants.setFontSize(s, 16);

}

public static void main(String[] args)

{ JFrame frame = new BinTest5a();

frame.setSize(600,600);

frame.pack();

frame.setVisible(true);

}

}

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

public class BinTest5b extends JFrame implements ActionListener {

private String newline = "\n";

public BinTest5b()

{

JMenuBar menubar= new JMenuBar();

setJMenuBar(menubar);

JMenu topic1 = new JMenu("Topic 1");

JMenu topic2 = new JMenu("Topic 2");

menubar.add(topic1);

menubar.add(topic2);

JMenuItem test = new JMenuItem("Test Menu");

topic1.add(test);

JTextPane tp = createTextPane();

tp.setEditable(false);

JScrollPane psp = new JScrollPane(tp);

psp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

psp.setPreferredSize(new Dimension(700,600));

JPanel right = new JPanel();

right.add(psp);

right.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("TUTORIAL"), BorderFactory.createEmptyBorder(5,5,5,5)));

JPanel contentPane = new JPanel();

BoxLayout box = new BoxLayout(contentPane, BoxLayout.X_AXIS);

contentPane.setLayout(box);

contentPane.add(right);

Container pane=getContentPane();

pane.add(contentPane);

JButton back = new JButton("<<back");

JButton next = new JButton("next>>");

next.addActionListener(this);

JPanel west = new JPanel(new GridLayout(3,1));

west.add(back);

west.add(next);

contentPane.add(west, BorderLayout.WEST);

}

public void actionPerformed(ActionEvent e)

{

JButton button = (JButton) e.getSource();

String label = button.getText();

if (label.equals("<<back"))

{

BinTest5a next = new BinTest5a();

//next.setVisible(true);

next.setSize(600,600);

next.pack();

next.show();

//next.setExtendedState(JFrame.MAXIMIZED_BOTH);

this.dispose();

}

}

private JTextPane createTextPane()

{JTextPane tp = new JTextPane();

String [] initString = {"THIS IS THE NEXT PAGE, ",

" ",

"OF ",

"THE ",

"TUTORIAL ",

"... " + newline,

" " + newline,

" " + newline,

"...CLICK TO CONTINUE.." + newline,

};

String[] initStyles = { "regular", "logo", "bold", "small", "large", "regular", "text","regular", "regular"};

initStylesForTextPane(tp);

Document doc = tp.getDocument();

try { for (int i=0; i>< initString.length;i++)

{ doc.insertString(doc.getLength(), initString,

tp.getStyle(initStyles));

}

} catch (BadLocationException ble)

{ System.err.println("couldn't insert"); }

return tp;

}

protected void initStylesForTextPane(JTextPane tp)

{Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

Style regular = tp.addStyle("regular", def);

StyleConstants.setFontFamily(def, "SansSerif");

Style s = tp.addStyle("italic", regular);

StyleConstants.setItalic(s, true);

s = tp.addStyle("bold", regular);

StyleConstants.setBold(s, true);

s = tp.addStyle("small", regular);

StyleConstants.setFontSize(s, 10);

s = tp.addStyle("large", regular);

StyleConstants.setFontSize(s, 16);

s = tp.addStyle("logo", regular);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

JLabel button1 = new JLabel(new ImageIcon("pig.gif"));

StyleConstants.setComponent(s, button1);

s = tp.addStyle("text", regular);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

JTextField txt = new JTextField(5);

StyleConstants.setComponent(s, txt);

}

/*public static void main(String[] args)

{ JFrame frame = new BinTest4b();

frame.pack();

frame.setVisible(true);

}*/

}

[8036 byte] By [confused_girla] at [2007-9-29 17:00:19]
# 1
I am unable to find the problem just by reading the code.Why dont you put some System.out.println code inside your action listenerand then you can check whether the function is actually called, the labelis as you expect etc..OG.
ordinary_guya at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
The confused girl and the ordinary guy, sounds like a romance in the making.
e-eejita at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
Are you getting jealous or what ?
ordinary_guya at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

> Please can anyone help me?! I'm trying to factorise

> the code below, so that when I run BinTest5a, bu

> clicking the "next" button, BinTest5b is loaded.

Ok, refactorised version is below.

However, this is not the way to do what this app appears to be doing.

Creating a new JFrame each time you go on to the next section is a

massive waste of resources.

Have a think about how you would move to a new page without having to

do that. Hint - how might you set the text and style on an existing

text area when the user moves to the next (or previous) page of your

tutorial.

Good luck!

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

public class ConfusedGirl2

{

static class BinTest5a

extends JFrame

implements ActionListener

{

protected String newline = "\n";

protected JButton back = new JButton("<<back");

protected JButton next = new JButton("next>>");

public BinTest5a()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menubar= new JMenuBar();

setJMenuBar(menubar);

JMenu topic1 = new JMenu("Topic 1");

JMenu topic2 = new JMenu("Topic 2");

menubar.add(topic1);

menubar.add(topic2);

JMenuItem test = new JMenuItem("Test Menu");

topic1.add(test);

JTextPane tp = createTextPane();

tp.setEditable(false);

JScrollPane psp = new JScrollPane(tp);

psp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

psp.setPreferredSize(new Dimension(700,600));

JPanel right = new JPanel();

right.add(psp);

right.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createTitledBorder("TUTORIAL"),

BorderFactory.createEmptyBorder(5,5,5,5)));

JPanel contentPane = new JPanel();

BoxLayout box = new BoxLayout(contentPane,

BoxLayout.X_AXIS);

contentPane.setLayout(box);

contentPane.add(right);

Container pane=getContentPane();

pane.add(contentPane);

next.addActionListener(this);

back.addActionListener(this);

JPanel west = new JPanel(new GridLayout(3,1));

west.add(back);

west.add(next);

contentPane.add(west, BorderLayout.WEST);

setSize(600,600);

pack();

back.setEnabled(false);

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == back)

new BinTest5a().setVisible(true);

else if (e.getSource() == next)

new BinTest5b().setVisible(true);

dispose();

}

protected String[] getText()

{

return new String[] {

"This is a FIRST PAGE , ",

"for MATHS ",

"learning ",

"tools ",

"which suports embedded graphs... " ,

" " + newline,

"...and embedded icons.." + newline,

newline + "JTextPane."

};

}

protected String[] getStyles()

{

return new String[] {

"regular", "bold", "small",

"large", "regular", "image",

"regular", "regular"};

}

private JTextPane createTextPane()

{

JTextPane tp = new JTextPane();

String [] initString = getText();

String[] initStyles = getStyles();

initStylesForTextPane(tp);

Document doc = tp.getDocument();

try {

for (int i=0; i< initString.length;i++) {

doc.insertString(doc.getLength(), initString[i],

tp.getStyle(initStyles[i]));

}

}

catch (BadLocationException ble) {

System.err.println("couldn't insert");

}

return tp;

}

protected Style initStylesForTextPane(JTextPane tp)

{

Style def= StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

Style regular = tp.addStyle("regular", def);

StyleConstants.setFontFamily(def, "SansSerif");

Style s = tp.addStyle("italic", regular);

StyleConstants.setItalic(s, true);

s = tp.addStyle("bold", regular);

StyleConstants.setBold(s, true);

s = tp.addStyle("small", regular);

StyleConstants.setFontSize(s, 10);

s = tp.addStyle("large", regular);

StyleConstants.setFontSize(s, 16);

return regular;

}

}

static class BinTest5b extends BinTest5a

{

public BinTest5b() {

back.setEnabled(true);

next.setEnabled(false);

}

protected String[] getText()

{

return new String[] {

"THIS IS THE NEXT PAGE, ",

" ",

"OF ",

"THE ",

"TUTORIAL ",

"... " + newline,

" " + newline,

" " + newline,

"...CLICK TO CONTINUE.." + newline

};

}

protected String[] getStyles()

{

return new String[] { "regular", "logo", "bold",

"small", "large", "regular", "text","regular",

"regular"};

}

protected Style initStylesForTextPane(JTextPane tp)

{

Style regular= super.initStylesForTextPane(tp);

Style s = tp.addStyle("logo", regular);

StyleConstants.setAlignment(s,

StyleConstants.ALIGN_CENTER);

JLabel button1 = new JLabel(new

ImageIcon("pig.gif"));

StyleConstants.setComponent(s, button1);

s = tp.addStyle("text", regular);

StyleConstants.setAlignment(s,

StyleConstants.ALIGN_CENTER);

JTextField txt = new JTextField(5);

StyleConstants.setComponent(s, txt);

return regular;

}

}

public static void main(String[] args)

{

JFrame frame = new BinTest5a();

frame.pack();

frame.setVisible(true);

}

}

dchswa at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

THANK YOU!!! That works great! :-) As you can see, I'm a total novice to Java, and have to code this by June :-( I'm trying to make a "tutorial" for learning Maths...i need a miracle!

So what do you mean by wasting resources? Would it be better to factorise the text/style code into an independent class of its own, maybe using abstract method+inheritance maybe? In the superclass define the common things like menus, buttons etc, and then in the subclass define the text/style methods perhaps?

confused_girla at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6

> As you can see, I'm a total novice to Java, and have to code

> this by June :-(

June? June 2004? 7 whole months from now? You have got to be

joking, right?

> I'm trying to make a "tutorial" for learning

> Maths...i need a miracle!

Unless you have a very busy schedule you've got all the time in

the world to get this done. Even as a Java novice you've got plenty of

time to make mistakes, learn from them and have another go.

Each time the stupid application doesn't do what you want it to or

becomes tricky to modify when you add a new feature; you'll have

learned something and you'll still have plenty of time to start over

and make it a bit better the next time.

> So what do you mean by wasting resources? Would it be

> better to factorise the text/style code into an

> independent class of its own, maybe using abstract

> method+inheritance maybe? In the superclass define

> the common things like menus, buttons etc, and then in

> the subclass define the text/style methods perhaps?

I would certainly consider defining a class that represents each element

in you document. It would have a member for the text, style and control

associated with that element. For many of the elements the control would

be null.

Each "page" in your application is defined by a collection of these

elements. At first you can hard code a few into your app to test the

layout and navigation stuff. Later you can read them in from text or,

much beter, xml files.

If I get bored in the morning (I'm at home now) I'll write you an

example of how it can be done.

For my information, stick a mock-up of how you expect a page from this

tutorial to look on a web site somewhere so I can take a look-see.

One last thing - do you have to use Java for this? Sounds to me like an

ideal for application for DHTML - possibly derived by applying an XSLT

transform to some XML.

dchswa at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

Hi there,

I don't have any designs at the moment, but similar to what I originally posted up. There will be lots of menu options, buttons etc, but the main concept will be as the user selects "next" or "back", the appropriate text/images are displayed within the TextArea/Scroller area, until the end of that particular topic is reached. I HATE java! One tiny mistake and the whole prog messes up!

That XML stuff sounds really interesting...thanks for the idea-i will look into this..

I'm afraid it does have to be in Java-thats one of the requirements unfortunately otherwise I would have picked something easy like HTML/Servlets!

That would be fantastic if you wouldnt mind making an example for me-whenever you have the time, I would be very grateful for any help.

Thanks again... :-)

confused_girla at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8
> That would be fantastic if you wouldnt mind making an example for > me-whenever you have the time, I would be very grateful for any help.Sorry, had to do some "real work" today, maybe I'll get some time on Friday.
dchswa at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
lol, there's some free code in the tutorials that do exactly the"forward" "back" thing:-]
jnw777a at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10

> I HATE java!

> otherwise I would have

> picked something easy like HTML/Servlets!

What do you think servlets are written in? HTML?

You say you have no design, just the program that you've written. I'd say that there is your main problem. I think that is the problem with most n00bs and also with the way Java is taught. It always helps to have an outline of a design in mind or on paper (on paper especially if you have not done this type of work) before starting to code. Since you have time till June 04, I'd recommend that you forget about your code, get a design on paper & then break the design down into smaller, do-able pieces, code the pieces and join the pieces together.

EverNewJoya at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11

Ok, I had half an hour, so here's a vey quick version that uses XML to

load your pages. STudy it, ask questions, look stuff up and find out

how it works.

To run it: java ConfusedGirl3 <file name>

where <file name> is a file containing the following xml.

Good luck!

<document>

<page title="Page 1">

<element style="regular">This is the FIRST PAGE</element>

<element style="bold">for MATHS</element>

<element style="small">learning</element>

<element style="large">tools</element>

<newline/>

<element style="regular">which suports embedded graphs...</element>

<element style="bold">...and embedded icons.</element>

</page>

<page title="Page 2">

<element style="regular">This is the next page</element>

<element style="logo"></element>

<element style="bold">of</element>

<element style="small">the</element>

<newline/>

<element style="text"></element>

<newline/>

<element style="regular">tutorial</element>

<element style="italic">click to continue</element>

</page>

</document>

Here comes the code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

import java.io.File;

import java.util.ArrayList;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.Attributes;

import org.xml.sax.helpers.DefaultHandler;

public class ConfusedGirl3

{

static class Element

{

private String mText;

private String mStyle;

public Element(String style)

{

this("", style);

}

public Element(String text, String style)

{

mText= text;

mStyle= style;

}

public String getText() { return mText; }

public String getStyle() { return mStyle; }

}

static class DisplayPane

extends JTextPane

{

public DisplayPane()

{

Style def= StyleContext.getDefaultStyleContext().getStyle(

StyleContext.DEFAULT_STYLE);

Style regular= addStyle("regular", def);

StyleConstants.setFontFamily(def, "SansSerif");

Style s= addStyle("italic", regular);

StyleConstants.setItalic(s, true);

s= addStyle("bold", regular);

StyleConstants.setBold(s, true);

s= addStyle("small", regular);

StyleConstants.setFontSize(s, 10);

s= addStyle("large", regular);

StyleConstants.setFontSize(s, 16);

s= addStyle("logo", regular);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

JLabel label= new JLabel(new ImageIcon("pig.gif"));

StyleConstants.setComponent(s, label);

s= addStyle("text", regular);

StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);

JTextField txt= new JTextField(5);

StyleConstants.setComponent(s, txt);

setEditable(false);

}

public void clear()

{

try {

getDocument().remove(0, getDocument().getLength());

}

catch (Exception e) {

e.printStackTrace();

}

}

private void add(Element element)

{

try {

getDocument().insertString(

getDocument().getLength(),

element.getText() +" ",

getStyle(element.getStyle()));

}

catch (Exception e) {

e.printStackTrace();

}

}

}

static class Navigator

extends JPanel

{

private DisplayPane mPane= new DisplayPane();

private Element[][] mElements;

private int mPage= 0;

public Navigator(Element[][] elements)

{

mElements= elements;

setPage(0);

setLayout(new BorderLayout());

add(mPane, BorderLayout.CENTER);

final JButton back= new JButton("< Back");

final JButton next= new JButton("Next >");

back.setEnabled(false);

back.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

setPage(--mPage);

if (mPage <= 0)

back.setEnabled(false);

next.setEnabled(true);

}

});

next.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

setPage(++mPage);

if (mPage >= mElements.length -1)

next.setEnabled(false);

back.setEnabled(true);

}

});

JPanel panel= new JPanel(new BorderLayout());

JPanel btns= new JPanel(new GridLayout(1, 0));

btns.add(back);

btns.add(next);

panel.add(btns, BorderLayout.WEST);

add(panel, BorderLayout.NORTH);

}

private void setPage(int page)

{

mPane.clear();

for (int i= 0; i< mElements[page].length; i++)

mPane.add(mElements[page][i]);

}

}

static class Parser

{

public Element[][] parse(String fname)

{

final ArrayList pages= new ArrayList();

try {

SAXParser parser= SAXParserFactory.newInstance().newSAXParser();

parser.parse(new File(fname), new DefaultHandler() {

String text;

String style;

ArrayList elements= new ArrayList();

public void startElement(String uri, String localName, String qName, Attributes attributes)

throws SAXException

{

if (qName.equals("page")) {

elements= new ArrayList();

pages.add(elements);

}

else if (qName.equals("element")) {

text= "";

style= attributes.getValue(attributes.getIndex("style"));

}

}

public void characters(char ch[], int start, int length)

throws SAXException

{

text += new String(ch).substring(start, start+length);

}

public void endElement(String uri, String localName, String qName)

throws SAXException

{

if (qName.equals("newline"))

elements.add(new Element("\n", "regular"));

else if (qName.equals("element"))

elements.add(new Element(text, style));

}

});

}

catch (Exception e) {

e.printStackTrace();

}

Element[][] elements= new Element[pages.size()][];

for (int i= 0; i< elements.length; i++)

elements[i]= (Element[]) ((ArrayList) pages.get(i)).toArray(new Element[0]);

return elements;

}

}

public static void main(String[] argv)

{

Element[][] elements= new Parser().parse(argv[0]);

JFrame frame= new JFrame("ConfusedGirl3");

frame.getContentPane().add(new Navigator(elements));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setSize(600,480);

frame.setVisible(true);

}

}

dchswa at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 12

wow..this looks incredible. I won't have access to a pc for the next few days so i won't really be able to test it out until sunday/monday (I have an old version of jdk, so will need to install the newer version later that supports xml etc-my pc connection here is really slow at the moment so downloading will take years!). But really, this looks great! I have printed off a copy of the code, will analyse this and will certainly get back to within the next few days! I'm guessing that to add new pages, all I have to do is update the xml file?

Thank you so much for your help! Once I have tested and researched into your methods, I will be in contact shortly...

Thanks! :-)

confused_girla at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 13

wow! i just tried this out!! its fantastic! thank you soooo much! you have saved me so much time..this way is so much simpler as opposed to hard-coding the entire content into the program...i can work so much better with this as a foundation! i totally understand how you did it-new pages can be added into the xml file..you really don't know how grateful I am! originally i had planned to code the entire content as in the original program that i initially sent out (using that long-winded "creatTextPane()" method), but this really has made my day! if you don't mind me asking, how many years experience do you have in Java? My pathetic attempts to code must have made you cry with laughter!!!are you a regular user of this forum? I would like to keep you informed of my progress if thats ok by you? Seeing as you have helped me so much already, and if I need help again?!...

confused_girla at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 14

I just have one further question. If I were to say run another class such as a login screen prior to the class you created (eg login.java), and if I pressed "next" etc on this login screen(-totally different GUI and layout), how would I load up the ConfusedGirl3.java class? (as this takes the xml file as an argument - so how/where would I specify this argument in this particular scenario?). Would I just need to create a new instance of ConfusedGirl3 in the main method of the login.java class(ie ConfusedGirl3 cf=new ConfusedGirl3()?)would this work? But don't you need to specify the xml file somewhere?

.. :-s

confused_girla at 2007-7-15 15:35:44 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 15

Ok, looks like you've got the idea of using an xml document to store the dynamic content of your system, so let me answer some of your questions, and pose a few for you.

If your system will include just a handful of pages, you're xml document will probably remain manageable; i.e. it will be possible to edit it in a simple text editor when changes are required. If you need many hundreds of pages, editing the xml by hand becomes a real pain as you need to run your app to validate your changes.

If you define an XML Schema for your document (you'll have to research what one of these is and what it does for you - btw, do not use a DTD, they are old technology and not worth getting familiar with) then commercial XML editors will be able to enforce the validity of your document as you write it.

Even then, we've essentially re-invented a rather fundamental wheel.

What we have in this example XML is the beginnings of a simple mark-up language. Add an XML Schema and it becomes formalised. To put it another way, we've just re-invented HTML. The application code is a simplistic re-invention of an internet browser.

This comes back to the original point of the exercise, what is it? You said you had to write something in Java but I don't know if you are a student with an assignment or working on a commercial system.

Depending on the requirements, you could ditch XML all together and simply write the tutorial pages in HTML. You could still write a simple browser in Java and provide a config file that defines the navigation order (Introduction.html, Page1.html, Page2.html, etc.).

It all depends on what you're trying to accomplish.

Your question about adding a login screen demonstrates the challenges of answering questions in this forum. When I give a solution here it always has a top level class named after the user who posted the question - hence ConfusedGirl3.java. The individual classes used to illustrate the solution are declared static so they can be used by the other classes and the main method on the outer class.

This scheme makes it simple for anyone to cut out the code into a single file, compile it and see what the code does.

In the real world, the static inner classes would be public classes in their own files. In this case you would have: Element.java, DispayPane.java, Navigator.java, Parser.java and some other class with the main method.

To add you login screen, you would create Login.java and hook that in by calling it from the main method/class. When you get a good login, you fire up an instance of Navigator inside a JFrame just as we do now.

In terms of my experience with Java, I first used it in 1996 whilst working on another OO language for a database vendor. I've been using it almost exclusively since 1997. Don't worry about your code appearing pathetic - I see much worse on a daily basis from people who claim to know what they're doing :)

I'm happy to provide ongoing help as your project progresses. However, this forum probably isn't the best place to provide it. I do login on a reasonably regular basis, but if I'm busy I may not wander by for months.

If you want to contact me with questions, I suggest you create a temporary e-mail address on hotmail (or similar), post that address here in "account AT hotmail DOT com" format - don't use "@" and "." then when I mail you there, stop using that new account. After a couple of months inactivity, hotmail will delete it.

The point being, don't post your real email address here unless you get some perverted pleasure from wading through spam!

dchswa at 2007-7-19 12:27:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 16

Hi there.Thats fantastic. I think I'm beginning to understand Java a bit better thanks to your help! I'm a student working on a project-basically to code and document a "computer aided learning system" for teaching Maths. I have until late April to hand it in-so there is some light at the end of the tunnel! I will be busy for the next few days with studies and work etc, so when I get the chance I will try and decompose that entire class into separate independent modules connected to a Login page. My Java is really awful - should take me a few days at least! In the meantime, I have created a default - confused "underscore" girl22 "at" hotmail "dot" com (very clever idea i must say!). Just drop me an email whenever you have the time, and I will forward you my valid address. Don't worry, I'm not a weirdo or anything-just a confused and frustrated student struggling to make sense of Java!as always, your help and feedback is very much appreciated.. :)

confused_girla at 2007-7-19 12:27:09 > top of Java-index,Archived Forums,New To Java Technology Archive...