JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Duplicate posting of the same question asked and answered on coderanch. Also apparently posted on dreamincode.

rproffitt commented: Good to know. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can't think of any necessary reasons, but it may make the code clearer if different parts of the initialisation are split up?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. The standard Swing API has all you need to track mouse press/relese, click, movements and drags. In the mouse listeners you simply get the mouse position and use that to call your setRoad etc methods. See https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html You don't need any other libraries, and it's easier than you may think!

(this is an answer to Q2, but the dumb editor thinks it knows best and keeps replacing my 2 with a 1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. Without all the code I can't reproduce that problem, but here's my best guess:
    You create the JPanel wih an initial size and location.
    You add that panel to a JFrame with the default BorderLayout layout, so the panel goes in the center. As there are no other components in the JFrame the layout manager sizes and positions the panel to fill the frame.
    You can check this by printing the size and position of the JPanel AFTER the JFame has been made visible.

In general you can't mix absolute sizing and positioning with a layout manager - the layout manager wil do its job and size/position components according to its own rules. You can try setMinimum/Maximum/Preferred sizes and the layout manager will respect some or all of those - each layout manager will have its own way of reconciling those constraints with its own rules.

Don't be tempted to use a null layout manager and position everything yourself - all that gives you is an application that messes up on any other machine with a different pixel size or system font size, and which fails to handle window re-sizing etc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, Smalltalk is totally OO, all it has are objects and messages. Blocks of code are just objects. The language doesn't even have loops or if tests - the same functionality is built with messages to Boolean objects, eg (pseudocode)
aBoolean.ifTrue(some code).ifFalse(some code) or aBoolean.whileTrue(some code)

But I think thats a different point from the default return value in Java etc. Returning this/self makes chaining calls easier and costs nothing. As far as I can see it's not too late - you could change Java to return this from void methods and it wouldn't break any existng code.

griswolf commented: In Python an object may be used in a boolean expression, e.g. if object_value: +11
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Good project!

Yes, MVC is a good way to go, although it may be overkill for a simple app. The important thing to have a clean separation between model and view.
You can use the Observer pattern to link a CarGraphics object to a Car object (just like any Swing event listener). The CarGraphics object adds itself as a listener to the Car object. Whenever the Car changes its state it notifies the listener(s). The CarGraphics object gets the car's latest state and calls repaint(). Swing then calls its paint method, which does the actual painting.
Alternatively, if you have the kind of model that updates the whole thing every n millisecs then you can just have the graphics as a listener to the model itself. Or maybe just have the timer call the model's update method then call the view's repaint. (There are many variants on this, just chose one that makes sense to you for this application.)
Don't use a JFrame for the painting, use a JPanel that you can place within the JFrame alongside other components. You subclass JPanel and override paintComponent. You could paint everything in one paint method, but you may find it easier in the long run to have a JPanel with the fixed stuff (roads etc) and the cars as separate components (eg JLabels) that you can move around in the JPanel. Or at least separate the method that paints a car from the method that paints the background.

John221 commented: Thanks for an answer, you helped a lot, I think that secound option is closer to me, I would have just two more questions. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

My background is in Smalltalk, so I can't understand the design decision to return None/void from a method/function that has no specific return value.
In Smalltalk a method with no explicit return wil always return the receiver object, which enables you to chain calls together like (pseudocode)
person.setName(etc).setAddress(etc).setDOB(etc).print()
Interestingly, Java under Oracle's management has recognised this and the latest APIs tend to return this; so calls can be chanied. It's called a "fluent interface" after Martin Fowler (2005)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

JComponent is the base class for all Swing components except the top-level containers(JFrame, JDialog, JApplet).
It does not define any add methods, but it does inherit a number of overloaded add methods from its underlying awt classes, viz
add(PopupMenu) and a number of variants on add(Component).
Those methods are inherited by all Swing components, even if they make no sense (eg add a component to a JLabel).
So I don't understand the question "Why the part will not have add() and remove() ways?"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi
Sorry, but that question cannot be understood in english.
Can you try to explain it better please?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can cast it so it's not a char pointer, eg cout << (void*) &x

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Isn't &var the address of any variable var, regardless its type? (or am I missing something?)

Yes, but you are missing something, namely that the cout << operator for a pointer to a char is overloaded so it works well for char arays (used to hold strings in C). It's a special case. It's defined to treat it as a pointer to a zero-delimited string. The nonsense you see is the ASCII interpretation of whatever bytes are at that address, up to the next zero.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The Random class in the standard Java API has all you need. Just Google it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

JC: Your fix is to reverse a lifetime of insisting on moderation by the rules and expect mods to select posts for general viewing by giving them their first upvote based on... what?

D: Sorry, I'm not understanding what you're referring to. I'm expecting moderation to work exactly the same way?? I'm not expecting anything different from the moderation team?

D: DaniWeb relies on people like you to comb through the fluff, so that everyone doesn't have to.

As things stand you expect mods to override the default filter and comb throughall new posts and selectively upvote before ordinary users with default settings see them. Hiding from users isn't that much different from deleting in practice, but you have no Rules or even guidance for what constitutes "fluff".

I'll give this one last try:
A user posts a perfectly good question. Users on default setting don't get to see it. Some time later a mod decides to review recent posts and decides in his subjective opinion that this post is good enough to be shown to the mass user base. That's the same mod who cannot delete or edit the post unless it matches the objective criteria that are so fundamentally important to you. Can't you see the ways in which this process is broken? At the very least you should have new posts visible to the whole user base until/unless the community votes them down. Mods will still destroy anything against the Rules. ... and all …

rproffitt commented: That's it. Also, I'd take some small marshmellows with mine. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't use the home page either - I just link direcctly to the programming forum.

Yes, I did see that there was a control that said "filter by" followed by a button that said "recommended".
But because I didn't want to filter by recommended, I never clicked it. That's a real UI blunder - a button is an action control and should be labelled with what hapens when you press it (eg "Exit"). It's not a place to display the current status.)
And because of that I didn't see any of the new posts, and didn't know that they even existed.
And I can't be the only one.

Underlying all this is a process problem that has existed for years - Dani hacks together changes that make sense to her and immediately rolls them out to the whole live community. Some work, some don't. She hs a faithful team of highly experienced members who have already commited lots of their time to DanWeb who would be happy to review and improve proposed changes before its too late, but she seems to think thats not necessary.

rproffitt commented: That's the sad part about all this. Something's busted if we don't want to use the home page. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thats just a straw man. I have never opposed filtering. Like all your moderators I have been tireless in deleting "blantant spam" so it has never been a problem for ordinary users. Over the years I have pleaded and argued with you to get a mandate for deleting low quaility posts and you have always refused it and insisted on strict interpretation of the Rules and nothing else.
Now it seems you have seen the value of supressing low quality posts, so we agree at last. What's left is to define "low quality" - off-topic, technically lacking, repetitive, badly written ...?
But before going down that rabbit hole answer this: what percentage of posts (after modertors have applied he Posting Rules) are of "low quality"? 5%, 10%, 2%? I don't think it's a lot. Personally I think allowing mods a bit of discretion is enough to get rid of the remaining problem.

But my specific problem was with the implementtion of filtering you have imposed, in which people don't see posts until people have seen them and up-voted them. Surely it's obvious that it's a nonsense? Your fix is to reverse a lifetime of insisting on moderation by the rules and expect mods to select posts for general viewing by giving them their first upvote based on... what?

So please trash-can this particular filtering and think through the whole filtering process before implementing a replacement.

alan.davies commented: Good thoughts, strongly put. I was wondering how a post gets up voted if nobody gets to see it. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

what we’re doing is pulling all content that has received at least one upvote

Ahah! So people don't see content until people have seen it and upvoted it.

DaniWeb relies on people like you to comb through the fluff, so that everyone doesn't have to.

And you hope that moderators will now take on the job of classifying all new content to get round the problems created by that ridiculous filter rule? Are you taking the piss? I'm here to answer questions and to prevent material that could damage DaniWeb. If you want an admin clerk then employ one, don't insult your moderators.

I get the point about featuring quality content, but until you have some vaguely sensible way to define "quality content" then you should put it back in the R&D box and not screw up the live system.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I share your confusion. The player doesn't actually play anything, just says OK and the computer rolls 4 dice and scores the result.
My best guess is that some user interaction mqy be used for the "roll again to win" case? In which case letting the cmputer play is the same as the user playing except that the user doesn't have to explicitly re-roll. and doesn't have to confirm having another go after each game.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, so I'm gereatric and have fading eyesight, but light typeface in light grey on white?

Serioudsly: constructive requests vfor the topics summary page:

  1. Show the name of the OP
  2. Bring back the click to go straight to the laest post in a topic
  3. Use sensible colours for type thatyou want people to read.

... and just for balance...
the create new topic interaction is a VAST improvement - especially showng the Rules up front.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What do you mean by "the statistical value"? If it's "average, std deviation etc" then you can't alter them except by altering the random numbers. Eg adding .7 to each value will increase the average by .7

Or, are you looking to convert uniformly distributed random numbers into a set that follows the standard bell curve? If so Google "random uniform to normal distribution" (but the easiest approximation is to add together 12 randoms (0-1) and subtract 6... an implementation of the Central Limit theorem)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Obviously a mis-click for the adjacent self-driving topic. I tried to move it, but couldn't find that option.

rproffitt commented: I guess I should get in the self driving topic and note how airplanes often are all auto pilot. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Does logging in to a system that requires no password count as "hacking"?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

WHen you say the recovery image, do you mean the recovery partition or do I have to create a recovery image from the recovery partition?

IF ( if ) you can boot from the recovery partition then it simply gives you a choice of trying to repair an existing system, or re-install everything from scratch. If it doesn't work then you haven't lost more tha a few minutes of your time. If it does work then, when you have a proper runnning system, creating an image would be a good idea in case it happens again.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think it was a per(f)ectly valid to assume that ... meant "three digits dash three digits dash five digits

Yes, sure.
It's just that I've seen so many failed implementations based on techies making apparently valid assumptions about an incomplete user requirement :)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's a really bad question. "123-422-17365" is just one string. A valid answer would be
"123-422-17365" - thats a regex that matches the string.
If you want to ask about a regex then at the very least you need a few examples of valid and invalid strings.

rproffitt commented: Yup, that works too. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why not simply use the recovery partition to restore the whole machine to its factory state?
https://www.dell.com/support/article/fr/fr/frbsdt1/sln151763/how-to-restore-a-windows-7-factory-image-on-your-dell-pc?lang=en

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A regular expression defines the rules by which a string can be analysed as conforming or not conforming to those rules. A numeric sample string does not define what the rules should be, it's just one example - eg we know that "123-322-14365" is valid, but what about "12-322-14365" etc etc.
So the answer to your question as asked is "no, it's not logically possible"

Maybe you mean something like "Is there any simple way or online website that can convert a rule like '3 digits, a hypen, three digits, another hypen, 5 digits' to regular expression?" In which case there are many many web sites that explain how a simple rule like that can be coded as a regex.

Reverend Jim commented: Well sure, if you want to use words. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  2: double moneyDeposit = 0;
  3: 
  4:   if(moneyDeposit > 2000) // will always be false!!!

same problem lines 16-18

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@JC. I put this code into a C++ compiler..

Apart from the odd chars right at the end of line 16 this is 100% valid Java,

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

i type any key from key board e.g 'A', then the jcombobox filters perfectly and shows me the items: America, Austria.

???
When you type a letter the selection in the combo box skips to the first entry that starts with that letter. It doesn't change the contents of the combo box's model.
Presumambly you have some extra code to achieve the filtering behaviour you describe, so maybe you would like to share that with us so we can see what's going on?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Darius
Those are the minimum machine specs. I was looking for the actual download size, which is something completely different!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thanks for the thanks!

Write an efficient algorithm ...

I posted that code to show what was wrong with your original loop, but that solution is NOT an efficient one. Consider it a starting point maybe, but to meet the requirement you will need to research some of the ways in which this can be made much more efficient for large arrays. It's a stadard question, so you will find lots of discussion aboit it with a simple Google.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I was referring to JavaME - the micro edition of a Java, because I don’t think that has sql as standard. Apart from that all I can guess is some kind of configuration or installation problem. Do you have any other code in the same IDE that successfully imports any Java.sql files?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi divinity02
You know me. We have worked together many times. But this time I agree with the previous posts. You haven’t given anywhere near enough information that we could possibly use to help you. Please try again with actual code, error message etc.

JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't think your problem is with code syntax, it's with the overall design/flow of the app.

Let's try to understand that... (stop me when I go wrong here)

You have a FeedReader class. An instance of that runs via a repeating scheduler to read some web info. When it finds the right info it creates an instance of Article - a class with variables like img, url etc - and adds it to a List.
There's a problem here because the List is a local variable that immediately goes out of scope and gets garbage collected.

You also have a method to add the same info to a database, but for some reason that does not use the Article instance that you created. Maybe that's the problem?

ps: all those set methods in Article are almost certainly a mistake. Why isn't Article immutable?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

People here do want to help you, but the information you have provided is not sufficient. Did you take the time to check rproffitt's link in his first answer?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sandeep: Nice try, but SIX YEARS too late.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not sure why that code isn't working, but then I don't like the way you have forced the scroll pane into a class that should just be a list.
I simplified it like this and it works OK...

class DoughOverviewList extends JList {
    DefaultListModel doughListModel= new DefaultListModel();
    Border GrayLine = BorderFactory.createLineBorder(Color.LIGHT_GRAY);

    public DoughOverviewList() {
        setFont(new Font("Arial", Font.PLAIN, 16));
      setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        setBorder(GrayLine);
        //super.setBounds(150, 120, 350, 500);
        doughListModel.addElement("Testing");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");
        doughListModel.addElement("11");      
        setModel(doughListModel);
        addListSelectionListener(ev -> {                
            System.out.println(ev);
        });

    }

...

frame.add(new JScrollPane( new DoughOverviewList()));
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I had the same symptoms with an SSD that died. It seems the 250Meg was the buffer size in the drive controller and when the drive itself fails all the sytem can see is the buffer. RMA time IMHO.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Dani owns the copyright to everything on DaniWeb, but I'm sure she will be happy to permit you what you want for this piece of code. Use DaniWeb messaging to contact her directly.
JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... but I still don't understand why you don't just use the Swing built-in classes, eg

 JFormattedTextField field = 
      new JFormattedTextField(NumberFormat.getNumberInstance());
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi guys, mind if I join in?

I tried ^[0-9]*\.?[0-9]+?$ in Java - just needed to double up the backslash for a Java literal. Works OK for me.

Maybe the problem is in how the regex is being used. The replace method in the earliest code doesn't use regexs. The replaceAll in the later code does use a regex, but the way it works is to replace any string that matches the regex. So the code will replace a valid input string with an empty string, but leave invalid strings alone. This is not what the comment suggests the OP wants.

You can simply test a string for being valid with Pattern.matches like this:

        String s = "1.2"; // or other test cases
        String r = "^[0-9]*\\.?[0-9]+?$";
        System.out.println(s + " is " + Pattern.matches(r, s));

but it seems maybe OP wants to reject any chars that cause the string to be invalid. I'm not sure thats achievable. Eg for "1.2.3" which char would you reject?
I personally think that's an undesirable approach anyway; I would probably do this in 2 stages - (1) block anything not a number or period from being entered, then (2) parse the string fully when user presses enter - rejecting bad input with a useful message and and allowing the user to fix it

ps: Why not use a JFormattedTextField with a NumberFormatter because what you describe looks a lot like a decimal number

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That looks very reasonable. Does the print on line 14 show the expected value?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why 2 parameters? Isn't there just one (the amount to be split)?

If so you may be worrying about returning 2 values from each call, in which case:

  1. You could package the two values into an array, tuple, trivial class etc depending on your chosen language
  2. You only need to return one, because the other is (the original value to be split minus the one you return).
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, that's a lot clearer.
Here's a suggestion:
When the user logs on, retrieve all his appts.
For each appt work out how many seconds it is from now until 15 mins before appt time, then start a Timer that will display the notification after that time has elapsed

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm trying to get a pop up as soon as a user logs in. I already have the pop up, but it keeps popping up all the results one at a time when I only need it to pop up within 15 minutes before the appointment time.

???
This seems to make no sense at all. Is the popup to do with a login, or an appointment? What are "all the results" for a log in, or an appointment time?
Maybe you can explain this more clearly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can't commernt on the detailed code, but yes, that's the structure I would use.

ultimately I want that array to be replaced by data that comes from a database

That really clinches it. Unless you want all kinds of pain re-writing of code you must encapsulate the way the Books are stored and hide the implementation.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why not ask the author of that code? - the contact info is on the page you linked

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you're thinking in OO then static is generally NOT the way.
In general, if you have a form and someunderlying data model (the array in this case?) then the pattern to use is:

Create an instance of the data model
Pass it to the form instance when that is created
The form instance can now use the data model's public methods to add/delete/update that instance of data model.

ps: Although you could just treat the array as the data model, that would create a tight linkage between the form and the data model's internal structure. Suppose later you had some reason to use a List instead? Or maybe later adding a Book would also trigger some other actions or updates? In general it would be cleaner to encapsulate the array in some class (eg "Library") with public methods for addBook etc. Pass the Library object to the form and keep its implementation private.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Are you quite sure its
\((.+?),\s*(.+?),\s*(and|or)\),\s*?\((.+?),\s*(.+?),\s*(and|or)\),\s*?\((.+?),\s*(.+?),.*
and not
\((.+?),\s*(.+?),\s*(and|or)\),\s*?\((.+?),\s*(.+?),\s*(and|or\)),\s*?\((.+?),\s*(.+?),.*
?

(just kidding, but it's a great example of why regex syntax is someone's attempt at a joke that backfired with terrible long-term consequences)

rproffitt commented: Evil. +1. +15
Reverend Jim commented: Hah! +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

a simple stand-alone database with only and forever ONE user. This is a family tree software

Are you sure that an SQL database is the bext way to go? You're going to have a lot of fun mapping between the user view of the tree and the SQL layer. It's not just the database API, its also going to be a non-trivial data schema.

MAYBE it would be better to model the family tree as a OO model, and simply use .NET serialisation to read/write it in its entirity to a simple file. Ie write the top-level object that represents the whole tree to the file on exit, or after major updates, and read it in at startup.

Just a thought...