Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

newbie project problems  RSS feed

 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to build a java application for hotel booking - it's actually for my father's hotel - but I fancied learning java. I figured I needed the following:
* Scrollable Jtable
* Ability to select a set of cell in a row or column.
* Row and column headers
* Wanted to detect mouse release to fire action
* Jbuttons and the ability to detect they've been pressed
* popup forms to show info on bookings or customers

I found I could pull code from the net that might offer one of the above features, I could then alter it to fit my needs and run it. Sadly I could never get all the code to work together. I'm currently at a stage where I can present a scrollable JTable with column headers and have the ability to select cells but no more. Any ideas on:

* I can't figure out row headers at all.
* I can write code that should detect mouseRelease but it doesn't.
* I can show JButtons but I can't detect they've been pressed.
* Not sure what class I should call to present an input form popup.

Just some simple pointers would help, I'm not a java programmer but I do program other languages. My real hassle is that I can copy code that seemingly works - put it into my app and then it stops working (often without an error). It's a bit frustrating. I'll attach a picture of what I have so far.

Thanks for reading.

Mike
prog.GIF
[Thumbnail for prog.GIF]
 
Campbell Ritchie
Sheriff
Posts: 55338
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

You have got yourself a big job there. I shall make things worse by asking whether it shouldn't use database connections.

A lot of those questions seem to be Swing-related questions, so I shall move you there.

What are you doing about mouse clicks? Are you adding ActionListeners to your buttons? What happens when a button is pushed is that action events are sent around your computer, and you have to register something to listen to those action events. There are lots of events triggered when you click a button with your mouse, but it is likely that the action event is the one you want. Beware of the sort of thing which uses addActionListener(this) because you get enormous methods with lots of if-elses in.

Please show us one of your ActionListeners, and how you are adding it to your buttons, then we can have a look at it and see what is going wrong.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for the table / button issue. It's been handled (and solved) before.

The popup can be done using JDialog, or perhaps even JOptionPane. It's message object can be a full Java component (like a JPanel).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing programming is non-trivial for someone that doesn't know Java (heck, it gives Java programmers fits sometimes--especially when trying to write Swing that performs well, although it's much easier than it used to be).

You should probably just post some sample code; it'll be easier than just providing random Swing advice.
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm actually very comfortable with Database stuff - that's what I do for a living

I'm using JOptionPane for simple confirmations of moving bookings etc - I didn't realise I could create an entry form with that class. Cheers.

I try to detect mouse clicks with the following - sadly it stays quiet all the time - I assume it's in conflict with the selecting of rows in the table:
public void mouseUp(MouseEvent e,int x , int y) { System.out.println("Mouse released" );}

I'll post the code as an attachment - don't snigger too much - I don't really understand this OO stuff but at least I'm trying. The code does run and shows a years worth of (randomly generated) bookings. You can sort of drag them around but you have to either go just vertically up or only horizontally across.

Cheers for input.

Mike

PS I wasn't sure what part of the code to post - I guess it's wrong to post it all but if you can give me an idea of what to post I'll just provide that.

 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post SSCCEs (Short, Self Contained Compilable Examples). This piece of code is quite hard to get through.

However, I did manage to notice one thing. You are using mouseUp for detecting mouse events. Use a java.awt.event.MouseListener instead - the method is called mouseReleased. Also, if you don't add that mouse listener to your control it won't get called.

Also check out http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponents.html
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're quite right about the code - sorry. I had so many issues it was difficult to find a bit of code I was truly happy with

I added the following to my code and it actually compiled this time !!! though it didn't respond to any key clicks etc


Any ideas what I'm missing?
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you called "addMouseListener(...)" anywhere?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a posting with 20 questions is counter productive. People will chip in with a suggestion on one topic and you have 20 different dicussions going on in in posting.

Learn to walk before you run.

Start with a SSCCE where you add a MouseListener to a table. It should be about 10 lines of code.

Once that is working you start with a SSCCE on how to use a button in a table.

As you master each individual step you incorporate the knowledge into you real app.

That way when you have a problem you can ask a specific question and we can give specific advice.

There is no way we will be able to answer all your questions in one posting.
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahrrrrg - I remember trying to use that before - it's the function from hell. I tried the following code after (and then before) the mouse commands but the compiler was having none of it. Saying "invalid method declaration. return type required. illegal start of type". I assume I should declare it on a jframe or a panel or the table but the "this" bit generally causes me issues - though I understand it should be pointing at the latest object.

addMouseListener(this);

PS If I add it into the main class then I get a different error "non-static variable this cannot be referenced from a static context".
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest I'd recommend taking a noticeable step back and figure out some Java stuff before trying to write Swing code: it's counter-productive to bite off too much at once.

It looks like everything is static--things really aren't going to work that way. What are you using as your Swing reference material?
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to step back then. Shame really as most of the app seems to work fine except I'm having to make a few kludges due to the fact I can't detect buttons or mouse release.

I have a good book on Java (In a nutshell) and I'm adapting code I find on the net. My issue seems to be that code can't be just added to your existing application (ie mouse handlers) as it either causes weird errors on compilation or that nothing is ever detected. Sadly the error messages in Java mean nothing to mere mortals like me.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My issue seems to be that code can't be just added to your existing application


Of course they can. The problem is the design of your code. I didn't notice it before, but as suggeted in the posting above you should NOT be using static variables or methods.

I have never seen a book that would ever suggest this and I doubt that Java in a Nutshell does either.

Start with the Swing tutorials. They have stand alone examples of "How to Use a Table" and you've previously been given the link on how to write a mouse listener. None of these example use static variables of method, except for the "main" method.

So no it is not a shame that you need to step back because your whole approach is wrong and will cause problems in the future both for this program and any future program.
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope you're quite right - the code in "A nutshell" is perfect. My own code (with all the static variables) is awful but I'm slowly try to get the hang of this OO stuff. My hassle is you can't just plug different bits of code together (or at least I can't) without an awful lot of experience. Sadly it's beginners who want to code this way. I can actually get any of the features I'm after working - I just can't get all of them to work at the same time!

Oh well back to the drawing board.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!