Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Print HashMap to a particular format in Swing that can be printed/copied and pasted?  RSS feed

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a cabin assigning program for summer camps.  One class Cabin holds the information of the cabin name, capacity, etc...  

The Camper class holds the camper name, age, gender, etc...  


The program uses a JList to assign to a cabin, and that information is held by a HashMap in the form of

HashMap<Cabin, Set<Camper>>

What I want to do is create a function where they can click a button to view a master list where it has...

Cabin

Camper 1
Camper 2
Camper 3
and so on...

and then it skips a few lines and does the same thing for each cabin

If there are 6 cabins, I'd like it to print in a 3x3 grid in the master list, where the user can just click the print button and print it out as is.


However, when looking up how to print items to the screen in swing, all I am getting are JLists and JTables, neither of which I want to use.  

I want to put the cabin list first on top, and then under it the elements of the value to the key from the HashMap.  



I haven't yet found anything that will get me started on this.

 
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe use two JTables

The first displays the Cabin information.

Then when you select a Cabin you populate a second JTable with the Camper information for the Cabin.

So the bottom JTable will be dynamic. You simply change the model of the table every time a new cabin is selected.

 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Maybe use two JTables

The first displays the Cabin information.

Then when you select a Cabin you populate a second JTable with the Camper information for the Cabin.

So the bottom JTable will be dynamic. You simply change the model of the table every time a new cabin is selected.



That's not going to work for what I'm trying to do, because that is only going to print one JTable.  I would need a JTable for every cabin on the screen, and the number of cabins will vary with each camp.  

All I want is something that just prints out the entire map in the format I described onto a printable screen.
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All I want is something that just prints out the entire map in the format I described onto a printable screen.  



Are you saying you want to print this on paper?

If you have lots of Cabins/Campers it won't fit on one page and will just randomly break in the middle of a page.

I would think you want to print one Cabin/Campers at a time.

In any case if that is what you want, then you just need to create multiple panels. On the panel you might have a bunch of labels at the top of the panel for the Cabin information and then use a JTable for the Campers.

So, you create one panel for each Cabin and add the panel to the frame using a BoxLayout.

Then everything will be displayed in one big panel.

The main point is that thereis not component that does what you want, you need to build it yourself.
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there is a Show All button when clicked shows all the cabins and campers in a JTable in a new window or dialog. This dialog has a Print All button. This prints the JTable contents. Here is some info on how to print the contents of a JTable: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#printing

Try this example program and see if this works for you: https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#TablePrintDemo
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

All I want is something that just prints out the entire map in the format I described onto a printable screen.  



Are you saying you want to print this on paper?

If you have lots of Cabins/Campers it won't fit on one page and will just randomly break in the middle of a page.

I would think you want to print one Cabin/Campers at a time.

In any case if that is what you want, then you just need to create multiple panels. On the panel you might have a bunch of labels at the top of the panel for the Cabin information and then use a JTable for the Campers.

So, you create one panel for each Cabin and add the panel to the frame using a BoxLayout.

Then everything will be displayed in one big panel.

The main point is that thereis not component that does what you want, you need to build it yourself.




Do you mean I'd have to create all the panels myself?  If so, that won't work, because I don't know how many cabins there will be.  One camo could have 4 cabins, and another could have 10.  How do I make it so that there is only a panel for the amount of cabins at that camp?
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're talking about putting a cabin as the headers, I would need it to do it vertically, so that the top row is the cabin name, then below it shows the campers vertically going down
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you mean I'd have to create all the panels myself?



Yes.

If so, that won't work,



Of course it will.

1. You write a loop to iterate through the map to get each Cabin
2. Then you create a panel add the cabin information to the panel  and add the JTable with the Campers information to the panel
3. Then you add this panel to the frame
4. Repeat steps 2 and 3.

This is basic programming. Rarely will you ever know the exact number of items to process. So you write a loop to process all items.
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

Do you mean I'd have to create all the panels myself?



Yes.

If so, that won't work,



Of course it will.

1. You write a loop to iterate through the map to get each Cabin
2. Then you create a panel add the cabin information to the panel  and add the JTable with the Campers information to the panel
3. Then you add this panel to the frame
4. Repeat steps 2 and 3.

This is basic programming. Rarely will you ever know the exact number of items to process. So you write a loop to process all items.




So, why is this not going to work with a lot of cabins?  Can't I just add a scroll bar to the frame?  

Also, if I look through it, how will it know where to position the next JTable it creates?
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, why is this not going to work with a lot of cabins?  Can't I just add a scroll bar to the frame?  



What???

You are the one that said it won't work.

I am the one who said it will work, since I made the suggestion on how to solve the problem.

And yes you would want to use a scrollbar since you don't know how many cabins or campers you will have in advance.

This is how you do dynamic programming. You design the form to allow for large amounts of data which means you will potentially need to scroll to see all the data.

how will it know where to position the next JTable it creates?



I have no idea what you are asking.  What do you not understand about the looping concept?

You create a single panel with both Cabin and Campers information. So the table (if that is how you want to present the Camper information) is add directly to the panel after the Cabin information.

Then this panel is added to the "main" panel.

The "main" panel is added to the scroll pane.

The scroll pane is added to the frame.

Again, I can only give you the steps (as I understand the question). You need to take the suggestions and do the coding. It should take about 10-15 minutes to try something like this. Then if you have a problem ask a follow up question.
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

So, why is this not going to work with a lot of cabins?  Can't I just add a scroll bar to the frame?  



What???

You are the one that said it won't work.

I am the one who said it will work, since I made the suggestion on how to solve the problem.

And yes you would want to use a scrollbar since you don't know how many cabins or campers you will have in advance.

This is how you do dynamic programming. You design the form to allow for large amounts of data which means you will potentially need to scroll to see all the data.

how will it know where to position the next JTable it creates?



I have no idea what you are asking.  What do you not understand about the looping concept?

You create a single panel with both Cabin and Campers information. So the table (if that is how you want to present the Camper information) is add directly to the panel after the Cabin information.

Then this panel is added to the "main" panel.

The "main" panel is added to the scroll pane.

The scroll pane is added to the frame.

Again, I can only give you the steps (as I understand the question). You need to take the suggestions and do the coding. It should take about 10-15 minutes to try something like this. Then if you have a problem ask a follow up question.



The problem is that the value of the map is not a camper, but a set of campers, and also the JTables are printed horizontally, not vertically.
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you keep quoting my entire response?

The problem is that the value of the map is not a camper, but a set of campers,



So, you get the Set and iterate through the Set.

How is this any different than iterating through the HashMap to get each Cabin?

JTables are printed horizontally, not vertically.



I have no idea what that means. Each row of the table (which would represent a Camper) is displayed vertically.

I don't have time to answer any more questions. It time for you to actually try writing some code!
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Why do you keep quoting my entire response?

The problem is that the value of the map is not a camper, but a set of campers,



So, you get the Set and iterate through the Set.

How is this any different than iterating through the HashMap to get each Cabin?

JTables are printed horizontally, not vertically.



I have no idea what that means. Each row of the table (which would represent a Camper) is displayed vertically.

I don't have time to answer any more questions. It time for you to actually try writing some code!




Okay, I haven't actually used JTables before.  I was just wondering if what I was trying to do could be done
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenneth Milota wrote:Okay, I haven't actually used JTables before.  I was just wondering if what I was trying to do could be done



If possible make an image (a sketch on paper and a photo of it) of what the printout should look like and show here (in case if you haven't found a way to do the printing). I'd like to get a clear idea of what you have on your mind.
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:

Kenneth Milota wrote:Okay, I haven't actually used JTables before.  I was just wondering if what I was trying to do could be done



If possible make an image (a sketch on paper and a photo of it) of what the printout should look like and show here (in case if you haven't found a way to do the printing). I'd like to get a clear idea of what you have on your mind.




I have 2 JLists on each screen for each cabin.  One contains all the eligible campers for that cabin, one contains kids assigned to it.  Once a child is assigned to it, it adds them to the set of campers, which is a value for the hash map.  

What I want it to look like is this.

The campers are in a toString format of

Last Name, First Name(int age, char gender)

The cabins are in the string format of

Cabin Name(char gender)(int age min- int age max)





I work for a hotel and we use a system called OnQ property management.  I'm not completely sure, but given a lot of the exception errors and messages looking familiar, I'm pretty sure it uses Java as well.  There is a function where you can generate a report such as the rooms at the hotel and the guest occupying them and other reports that look like the one I posted.  I just want to know what kind of way this is done, because the closest to it that I found was a text field, but that has to be all strings in there.  
Cabin-Assigner-List-Model.png
[Thumbnail for Cabin-Assigner-List-Model.png]
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the data can be formatted into a JTable like in the image below and then printed. I had already provided the links to Oracle's Java examples website where there is info to print.

Another option is print in a similar way, but in a spreadsheet (as in MS Excel); yes, this can be done in Java.

 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another option is print in a similar way, but in a spreadsheet (as in MS Excel); yes, this can be done in Java.


Apache POI is an free software library one can use to create spreadsheets using Java. Here is link to view what one can do with that software:
https://poi.apache.org/spreadsheet/examples.html
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a little off-topic, but I was hoping maybe somebody knew the answer to this so I don't have to start a brand new topic.  

I test my program sometimes with a JAR runnable, but the screen is huge when I do it.  When I use it in the GUI, it's the right size, and I set the size to maximize, but all the buttons and frames are large than I created them.  
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of Java are you using?
And, are you sure you are using the same class files to create the JAR file?

Here is a link to a topic on solving issues related to Swing GUI: https://docs.oracle.com/javase/tutorial/uiswing/components/problems.html

 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:What version of Java are you using?
And, are you sure you are using the same class files to create the JAR file?

Here is a link to a topic on solving issues related to Swing GUI: https://docs.oracle.com/javase/tutorial/uiswing/components/problems.html



My problem isn't listed there, but thanks for the resource.  

What's going on is that when running it on Eclipse, the sizes are fine and fit the screen as I created them.  When I export it to a JAR, the frame sizes are much larger than I created them in Eclipse.  

I'm using the most recent version of Java last I checked, and I'm exporting the project to a JAR.  The screens all come up and the program functions work, but the screens are all huge.
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the problem is on the JAR, isn't making the content pane much bigger than the size of the frame.
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest that you post a new post with the topic that the Swing GUI is showing bigger while running using a JAR than that you are seeing while testing on Eclipse IDE.
 
Kenneth Milota
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:I think the data can be formatted into a JTable like in the image below and then printed. I had already provided the links to Oracle's Java examples website where there is info to print.

Another option is print in a similar way, but in a spreadsheet (as in MS Excel); yes, this can be done in Java.



I figured out how to do a JTable from a HashMap using the DefaultTableModel, but the problem is it prints the key, and then all the items of the set in one cell next to it.  If this has only about 5-10 campers, that would be fine, but for larger groups, the screen would just be too wide.

This is the code I have, but instead of using cabin and camper, I used a type of number (Even or Odd) and a number.  Any hints on how to get it so that it will print it like the table you drew?

I know how to take the set and make it into an arrayList for each key, but printing the key next to it every time is what I can't figure out.  


 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

   public static TableModel tm(Map<?,?> map){
       DefaultTableModel model= new DefaultTableModel(
               new Object[]{"Type", "Numbers"}, 0);
   
        for(Map.Entry<?,?> entry: map.entrySet()){
         model.addRow(new Object[]{entry.getKey(), entry.getValue()});
        }
        return model;
   }





   
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!