Ian Hamilton

Ranch Hand
+ Follow
since May 02, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Ian Hamilton

ok solved it! I found the bug and replaced this code;


with this code




still trying to fully understand where I went wrong so if someone could speed my discovery up by explaining exactly what the bug was I'd be very appreciative

I'm guessing it has something to do with the Windows win = new Window() call but if that is the case why did the JTextArea get displayed?

Thanks again

[ July 16, 2006: Message edited by: Ian Hamilton ]
[ July 16, 2006: Message edited by: Ian Hamilton ]
18 years ago
ok I solved the NPE by initializing the JTextArea in the constructor

public class Window extends JFrame{

private final static String newLine = "\n";
private JTextArea log;

public Window(){

log = new JTextArea();
}

etc


However the JTextArea.append() is still not working! Where am I going wrong??

public void writeToLog(String text){

log.append(text);
log.append(newLine);

//log.setCaretPosition(log.getDocument().getLength());
}

Many Thanks

Ian
18 years ago
Hi can someone please help and tell me why I get a NPE when I append to the JTextArea.

The example in the thread above runs fine on my PC but my code below doesn't work and I can't spot any differences!!

Thanks

Ian


package JavaDev.TestGUI;

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


public class Window extends JFrame{

private final static String newLine = "\n";
private JTextArea log;

private Component createLogger(){

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

log = new JTextArea("Hola");
log.setEditable(false);
log.setLineWrap(true);
log.setWrapStyleWord(true);

JScrollPane scrollPane =
new JScrollPane(log,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

pane.add(log);

pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
10, //bottom
30) //right
);
pane.setPreferredSize(new Dimension(600, 600));
return pane;
}

public void writeToLog(String text){

log.setText(text + newLine);
//log.setCaretPosition(log.getDocument().getLength());
}

private Component createButtons(){

JPanel pane = new JPanel();

JButton cancelButton = new JButton("Cancel");
JButton buildButton = new JButton("Start Build..");

pane.add(cancelButton);
pane.add(buildButton);

/*pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);*/

return pane;
}

private Component createFileChooser(){

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

JTextField field = new JTextField("Build Locaton: " + System.getProperty("user.home"));
field.setEditable(false);
JButton editButton = new JButton("Edit Build Location..");
pane.add(field);
pane.add(editButton);

/*pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);*/

return pane;
}

private void createAndShowGUI(){

//Make sure we have nice windows decorations
JFrame.setDefaultLookAndFeelDecorated(true);
//WindowUtilities.setNativeLookAndFeel();

//Create and set up the window.
JFrame frame = new JFrame("MarketAxess Release Automation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Window win = new Window();
Component contents = win.createLogger();


frame.getContentPane().add(contents, BorderLayout.CENTER);

frame.add(createFileChooser(),BorderLayout.PAGE_START);

frame.add(createButtons(), BorderLayout.PAGE_END);

//Display the window.
frame.pack();
frame.setVisible(true);
}



public void launch() {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}



}
[ July 16, 2006: Message edited by: Ian Hamilton ]
18 years ago
Hi Robert,

re: Question 2 - From what I have read in this forum most people agree with your assumption that create() and delete() should be implemented fully i.e. coded and tested, even though they will never be called by the client.

re: Question 1 - I eagerly await the discussion of others because I am at a similar stage!

Ian
[ May 30, 2006: Message edited by: Ian Hamilton ]
When I say concurrent modifications I do not mean to the same record, this is why we cache which records are being modified to prevent such an occurrence - if a user attempts to modify a record that is locked we can let them know it is unavailable.

So if we can prevent a user editing a record that is locked, where is the harm in allowing concurrent modifications?
[ May 26, 2006: Message edited by: Ian Hamilton ]
ok I get it. So while a static file descriptor prevents concurrent access to the actual file, by keeping track of which records are being modified I can allow concurrent record modifications to take place - but the file will only be updated one modification at a time.

[ May 26, 2006: Message edited by: Ian Hamilton ]
[ May 26, 2006: Message edited by: Ian Hamilton ]
Hi Jereon, thanks again for your reply.

https://coderanch.com/t/188260/java-developer-SCJD/certification/Suggestions-approaching-SJCD

Basically you need to keep track of records locked by individual clients to make sure there can be no 2 clients trying to modify the same record during any particular time period.



I notice in the above thread you discuss keeping track of which records are being modified. I assume this isn't necessary with a single static file descriptor?
[ May 26, 2006: Message edited by: Ian Hamilton ]
Hi Rishi,

For me the SCJD seemed like the next logical step after taking the SCJP to prepare myself for coding in the real world. And it's not easy but I'm learning lots which is the main point of this exercise in my view.

The books I'm using which have been great so far are;

SCJD Exam with J2SE 5 2nd Edition - written by Andrew
Head First Design Patterns - Freeman & Freeman
Java Network Programming Java 2 2nd Edition - Hughes et al
[ May 25, 2006: Message edited by: Ian Hamilton ]
Thanks for replying Jereon.

If we have a single static file descriptor/file object doesn't this prevent concurrent read/write access? i.e. we will only be able to do one seek() at a time to ensure file pointer is positioned correctly? so

Client A would like to update record 2
Client B would like to update record 5

but Client B must wait until Client A releases the lock and resets the file pointer to start of the file?
[ May 25, 2006: Message edited by: Ian Hamilton ]
Have you taken into account that each record is preceded by the 1 byte deleted tag?

I had a similar problem whereby I was reading the deleted flag in the wrong place causing my data to be offset by one.

When I fixed this it parsed fine.
Hi Andrew,

So to avoid the OS running out of descriptors we can attach several streams to a single static file descriptor..

However can you explain why we need a file descriptor?? If a new client connects we read the whole file, and if we know the position where we would like to write we can use seek() to set the filePointer (after which the filePointer is always reset to the beginning of the file).

Why do we need to remember the last read/write position?
Hi,

You can take as long as you like to complete your assignment (Part 1 of SCJD) - the voucher doesn't expire.

However the exam voucher (Part 2) does expire so if you have purchased this you may need to check your dates.
"netstat -a" from Windows command prompt will list the ports in use
Thanks Andrew/Jereon, both your posts are great.

I've just been reading about decorators and this a great real world example.
Hi Jereon,

If RemoteData is a wrapper for Data why not handle the requests in Data, rather than use Data as a proxy?

I'm not doubting the logic, just looking to understand it.