Results 1 to 8 of 8
  1. #1
    geeeeky.girl is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default How Can I Create a Simple Grid with a GridLayout?

    Hi all,

    I'm trying to create a work schedule using two nested loops and which I'd like to look like an MS Word table (or Excel document). It must be designed in such a way that the inside of the squares can be used to clic in via events and listeners (I don't know how to do this yet, so please bear with me, for the time being I'm working on the graphics part only...). I'm having problems creating a simple grid with simple black lines (borders, like an MS Word table) and choosing the dimensions I want. I don't imagine that it is all that difficult, but I just can't figure it out!

    I'd like the inside of the squares to be components (I think this is a good idea!? JPanels??)... Actually, I don't quite know how to organize all of this, to tell you the truth.

    This whole thing will be the main part of a window, there will be other components in the window as well (buttons, etc.).

    Ah, which reminds me, the other thing I don't know how to do is to write text in the window but to have it surrounded by a border (square/rectangle) to display information about a worker or a shift.

    In my method, I suppose that the dimensions of the main grid or table could correspond to, for example, from left to right, the number of days of the week (Mon, Tue, Wed, etc.), from top to bottom the number of workers of a company, and inside we could put work shifts (morning shift, afternoon shift, night shift, etc).

    Eventually, I'd like to be able to click on buttons and then onto the appropriate square to assign a shift to a give slot (person and day). (Once again, I'm not sure how to do this yet, I'm just working on the graphics part for the time being).

    I was thinking of using a GridLayout, but would be open to other suggestions if it will make things easier.

    So, to sum up, two things, a grid and a text surrounded by a border (square/rectangle) to display information.

    Could someone give me a few lines of code that will make this happen?

    Many thanks.

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Have you tried looking up the JTable class?

  3. #3
    geeeeky.girl is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Yes, I have.

    But it doesn't correspond to what I want. It seems to me that with a JTable you must have headers for columns (title for each column) and that you can't have "headers" for lines. Also, you can't get rid of the headers... (Maybe I'm wrong, I tried to do what I could though, keep in mind that I'm struggling with all of this). I also couldn't figure out how to choose dimensions for the rectangular fields and for the table itself, although perhaps if I'd searched more I would have found that.

    Is it that difficult to do otherwise?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    You don't necessarily have to display the column headers for a JTable, and you can add row headers. Check out this blog post:
    Row Number Table « Java Tips Weblog

    and the Swing tutorial for JTable:
    How to Use Tables (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    db

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    JTable is the way to go. It seems intimidating at first, but you can even create a custom renderer to create the components that go in each cell.

    If you really want complete control, GridLayout will provide exactly what you want. I don't use it much (I like GridBagLayout), but I believe it has only a couple properties, like rows and columns. If GridLayout is too restrictive, GridBagLayout can do just about anything fairly simply, once you get used to it.

    One thing: I am assuming you are using a visual designer, like Netbeans Matisse. Doing anything but the simplest layout is very difficult. Matisse generates Java code, so you add to or modify what it generates.

  6. #6
    geeeeky.girl is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Steve, Darryl, Paul,

    Thanks for your replies !

    I'm game for using JTable, but I just have a few simple questions to ask before getting started on it. As I said earlier, I experimented a bit with JTable, but then had trouble with it.

    I'm using Eclipse, and only Eclipse. I don't want to change for the time being, I have to finish this assignment soon and changing IDE's would be complicated at this point.

    I will have a look on the Net to learn more about JTable, however, could you give me just a few tips / a little info in abbreviated form right here?

    - First and foremost, will I be able to associate events, any and all types of events, with the different cells of the JTable grid afterwards, "headers" included?
    - How do you make a JTable without column headers?
    - How do you make a JTable with list "headers"?
    - How do you choose the individual size of a specific line or column? (Let's say I want the first and last columns to be twice as large as the middle ones)

    If I can do all of the above, I will definitely use and explore JTable.

    Oh yeah, last question. If I have another part of the window where I'd like to include a few words, i.e., some text (and this has nothing to do with the JTable, this would be a separate "component"), what's the simplest/best way to create a border around it? (Could you give me a few lines of code?)

    Many thanks!

    GG

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    When I saw the word "assignment", I cringed.

    I use Eclipse, and I use a plug-in called MyEclipse ($60/year) that includes the NetBeans Matisse designer. Not perfect, but I couldn't do a real app without it. Just for future reference.

    As far as JTable, unless you have plenty of hours to do research, it will be too complicated for your requirements.

    Instead, I suggest just using a JFrame and setting its contentPane layoutManager to null. (The content pane is the part of the JFrame that holds the components, but JFrame's methods hide that from you.) This allows you to specify the location (top, left) of each component and its size (width, height). Set the JFrame's resizable property to false and invoke the pack() method before you make it visible. It *should* be just big enough to hold all your components. Now you are working with something like an old VB form.

    Visible components have a border property, which you can set. Each border has its own class. I would create a JLabel to hold the text and create and set a LineBorder or BevelBorder on the JLabel.

    This is poor technique for a real application, but it will allow you to get the job done quickly and without guessing where your components will end up. Swing can create great GUI's, but it has a *huge* learning curve. School isn't the time.

    Sorry about the garbled instructions; good luck!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by geeeeky.girl View Post
    - First and foremost, will I be able to associate events, any and all types of events, with the different cells of the JTable grid afterwards, "headers" included?
    Yes.
    - How do you make a JTable without column headers?
    If you add a JTable to a JPanel, its standard behavior is to appear without column headers. You must either add the table headers explicitly such as to the NORTH pane in a BorderLayout container or add the JTable to a JScrollPane. So in other words, this should not be a problem for you.
    - How do you make a JTable with list "headers"?
    so you want "row" headers? If so, I don't believe that this is standard behavior for a JTable, but having said this, I've seen on Google and Sun Java Forum searches several examples of how to do this.
    - How do you choose the individual size of a specific line or column?
    That's all part of the JTable API.

    Oh yeah, last question. If I have another part of the window where I'd like to include a few words, i.e., some text (and this has nothing to do with the JTable, this would be a separate "component"), what's the simplest/best way to create a border around it? (Could you give me a few lines of code?)
    I'd place it in its own JPanel and call setBorder(BorderFactory.createLineBorder(....)); on the JPanel.

Similar Threads

  1. [SOLVED] How to create a Grid?
    By Nakira in forum New To Java
    Replies: 11
    Last Post: 11-12-2008, 11:45 AM
  2. How to create a simple deadlock
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:36 PM
  3. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. Help with gaps in GridLayout
    By trill in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:20 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •