I'm currently using net beans with Java to work on a senior project. Essentially the program is going to create word search puzzles.
While I'm still a few weeks from needing a solution, I'm having difficulty thinking of a possible control that could display a word search puzzle (not to be mistaken for a crossword puzzle).
My initial thought would be to have a grid of subclassed JPanels stored in another subclass of JPanel [as a 2D array, or something].
You could then attach MouseListeners to the grid cell JPanels if you need to detect if they've been clicked on, or whatever
Might give you an idea
brainjar - Wordsearch (source at bottom of page)
What do you mean by control? Maybe something like a JTable + Custom ListSelectionModel
Related
I am just learning AWT / Swift / JavaFx recently and I feel like I have learned a lot but have barely scratched the surface. There might be a much easier way to do this BUT, I am trying to make a GUI button in eclipse that calculates the distance between two objects that the user creates. Lets call them Robots for now. So, I have one button that allows the user to create the Robots and it stores them in a DefaultListModel (listModel) and displays them in a Jlist (list) below the buttons. When the user then clicks on a robot, another button becomes actice and allows them to calculate the distance between them (one of the parameters of the robots is their location on a grid). I have all that worked out but my problem is that I am trying to make it to where they have to select two different Robots. At first I thought I could let them select two Robots and then make the computeDistance button becomes active, but I am not really sure how to do that because the only way I can select more than one object in the JList is to cntrl click and I don't want the user to have to know that trick.
My next idea was to allow the user to have one Robot selected and then give them a popup window displaying the other Robots and have them select one. Via showOptionsDialog, I have discovered how to make a custom JOptionPane, so I thought, why not make them buttons (probably will look awful, but I don't know how to make anything other than JOptionPane.showXxx at this point (like I said, only skin deep so far). Have tried consulting the javadocs, but right now that is a LOTTT to take in and have read a decent amount, I thought.
Ok, sorry if this is long, but is there a way, using my DefaultListMethod to make custom buttons? I tried a bunch of ways by creating Object[] options = {list.elements()}; etc but that doesn't work. Any help would be much appreciated!
First Java project - solving a Sudoku puzzle. Used SDK to create GUI first (bad idea?). GUI has 9x9 grid of square JButtons. Added handlers (81!) such that 'click' cycles values '_','1',...'9'. Named buttons in grid to reflect position (also painful). To solve puzzle implemented 9x9 array of Tile objects. Tiles to mirror state of respective JButtons (text value, etc). Want to use array of Tiles to solve puzzle. To use get/setText methods to r/w from array to/from JButtons. Hit wall!! Can't find way to reference JButtons. Hoped to create String representing JButton variable name (easy enough) then magically convert (type casting?) to reference JButton object. Not possible? A String is a String is a...? I have an object in memory (JButton) I can't reference dynamically? Approaches seen (tic-tac-toe, etc.) create an array of JButtons to access by index. Don't want to do this as it takes away from simplicity of using swing. The SDK generated source code is ~2000 lines already! Use hash to map objects to objects? Ideally each Tile object will map to respective JButton when created. Hopefully enough detail here to explain what I am 'trying' to do. Is this case where pointers would be nice? Is this a downfall of using SDK for GUI? Shortcoming in Java? Can anyone recommend approach to this (tricks or tips)?? Thanks!!
Array of Objects extending JButton is way to go. Learned much here and many thanks to all who contributed!! For newbie to Java to think an extensive swing UI is an easy approach is foolish. Took me a few weeks to learn basics of JComponents. I still use 'Window Builder' but only for the most basic interface. Want to challenge a new student? Let him/her try to tackle Sudoku as first project! Now that I have conquered this UI I feel on top of the world and no problem is beyond my reach! THANKS AGAIN!!
I'm sorry this is probably way too basic to be on here, but it's a subject I've been struggling with for about a month now and I don't know where else to go (as far as I know there is no "noob overflow", lol).
I'm trying to create a class that would:
1. put an image on a window (a JFrame, JPanel or other container)
2. be able to support keyboard and mouse listeners
3. could have multiple instances in the same container
So anyway I've tried all the usual places - Google, YouTube, the official Java site (sorry forgot the URL) and of course here on Stack Overflow - but haven't been able to find anything even remotely similar to what I'm trying to do.
Of course, I've also considered the possiblity that maybe it can't be done at all. There doesn't seem to be any kind of standard "JImage" or "JGraphic" that works like JButton or JLabel, and for whatever reason graphics requires a completely different list of (extremely involved) processes and procedures. As an example, in this post: How to "really" draw images in a Java app - it took me 60+ lines of code and 2 classes to just come close. That project didn't work in the end because for some reason it would only let me create one instance (even it you created 2-4 in the main method, it would only display the last one you told it to add).
But anyway, assuming that I'm not trying to "re-invent the wheel" here and it is actually possible (in Java), does anyone have an idea as to how (or at least know of a better site to study it)? Unfortunately most of the sites I've visited tend to assume you know all the inner workings of images (I know what a pixel is but that's about it - Buffers, Rastars etc. are still beyond me). It would be absolutely outstanding if there were a site that would explain it in layman's terms, if such a site exists. Thanks in advance.
Just use a plain old JLabel.
Regarding your requirements:
put an image on a window (a JFrame, JPanel or other container).
You can give a JLabel an ImageIcon of the image of interest and it will display it. This can then be easily placed in any other container such as a JPanel or JFrame.
be able to support keyboard and mouse listeners
Any component that extends JComponent, such as a JLabel allows for use of MouseListener, MouseMotionListener and can listen for keyboard input via Key Bindings.
could have multiple instances in the same container
You can add as many as you'd like to any container. Just be cognizant and respectful of the layout managers in use.
I have this kind of progamming task without JavaFx, instead it's Java Swing. I realized my knowledge is still limited.
I have one single JTable.
But, within this JTable I need a custome Cell Renderer.
The goal is to make this kind of JTable: Example image
My current solutions are: Example Image
Create a Single JTable:
get each Column and set its CellRenderer with a custom Renderer (below).
Create a new Class implements TableCellRenderer:
return different JPanel inside getTableCellRendererComponent
method using switch case (as column counted).
After hours, and hours, I think my current solutions is quite daunting tasks. Thus, My question is:
What are the simplest method of creating this Custom JTable to achieve the main goal as mentioned above?
you have two options
1) JPanel nested another JComponents and solve that by using standard LayoutManagers note scrolling isn't natural nor nice
2) JTable with JPanel can solve that, notice about scrolling inner JScrollPane inside another JScrollPane
I've been facing this problem for a while, and I decided to do it myself. Extending the existing implementation of a table, adding some concepts for what I expect from a table, and writting some editors/listeners for that. All the same, but with a treetable.
I'm working on this project called SUMI.
It contains a java package (ar.com.tellapic.sumi.treetable) that is an extension of a JXTreeTable from SwingLabs.
The project is being developed and I didn't provide any documentation yet. You can do what you want by creating a renderer and if needed, an editor, for lastly attaching actions to each object.
If you decide to use it and you need help, email me, I'll help you without any problem.
Or, you could read the source by your own.
Regards,
EDITED (again):
To clear a little bit this answer, I've just created a wiki page in the project wiki and put the relevant code there. If someone feels that the code should be inserted here, please let me know.
Basically, I try to explain how to find a straight solution to the renderer/editor problems you may find using JTable with your specifics needs by using part of my project, in order to get something like this:
Note that the screenshot was taken after clicking on the respective tick-button.
Once you create a nested panel for one row, as suggested by #mKorbel, you can add any number of them to a GridLayout(0, 1) in a JScrollPane. If rendering many rows becomes an issue, you can adopt the same approach used by JTable, illustrated here.
Even though, JTable can be customized to whatever you desire through cell renderer and cell editors, it is never preferred because you have to do a lot of messy codings for that. Instead, for your problem, I suggest to use JScrollPane and add your component (view panel as your sample jTable ) to its viewPort.
For this implementation, represent each rows with your custom class that extends JPanel. And add the required row components (that may be any components like jlabel, jtextfields or even jpanel too) in it. For the simplicity, you can use null layout for the row panel and add the components at any location you want.
I hope this will help you workout with your problem. If you got any problem in this implementation, feel free you ask again.
I am creating a GUI program using MVC which should look like this..
alt text http://img137.imageshack.us/img137/6422/93381955.jpg
I have created a Window and Panel class. I am thinking of creating the Input and Display tabs in the Panel class and then creating two more classes, InputPanel and DisplayPanel. So the InputPanel will contain the stuff in this picture under the Input tab and same for the Display tab. Is there a better way to design this?
Also, since there are 3 sections in the Input tab (Name and sentence, crime, button), should I create 3 panels or just 1 panel containing all those?
Thanks
To answer your specific question about using three panels instead of 1, I would suggest two. There's rarely a need to create a panel just to create a single widget. So, one widget for the name and sentence, one for the crime.
As for the question about "is there a better way to design this"?... It sounds like you are learning, so I suggest you don't focus too much on the perfect way to do it. Stick with your original design then after the task is done ask yourself what worked* and what didn't. With that information you'll be able to decide for yourself whether what you did was the right design.
There usually isn't a "best" when designing GUI code -- there are many ways to solve the problem. What you've described sounds like a perfectly good way to attack the problem
(*) "worked" in this context means, was it easy to code? Did it allow you to achieve the layout you desired? Does it make the code maintainable over time if, for example, a requirement comes down to reorganize the GUI?.
Bryan gave good advices, I will just add that ergonomics isn't an exact science, although experience helps there.
Tabs are nice, eg. to separate settings, or group in a same panel (toolbox for example) different sets of tools (layers, colors, brushes...).
But they might not be adapted to all workflows. But we are lacking information about the role of the Display tab. Is it supposed to list all crimes in a table? Can't the table, if any, be below the controls?
As hinted by Bryan, it is better to design the GUI, then to test it, like would do a real user. Do you find the workflow easy to understand? (make somebody else to test it!) Does the usage feels natural? Is it fast to use?
Then you can adjust the design in light of these observations.
You were right to create InputPanel and DisplayPanel as seperate classes.
As for further splitting those panels? Yes you should further split them up, but not into separate classes. You should add jPanels inside of your InputPanel, and DisplayPanel, and group the controls within those internal jPanels.
Please let me know if you would like me to clarify what I mean.