What is the best way to make a two option window? - java

Which is the best way to make a window that has a name a JLabel and two JButton components underneath.
I am new to Swing and I tried some methods but didn't understood too much from any.
What would you suggest to focus my attention on to do this specific thing?

DYM like this?
Or this?
Actually that 2nd one comes from a page linked in the first comment.
But, you might be over complicating the issue and a JOptionPane might be a more suitable solution - see How to make dialogs for more details.

Related

JComboBox: arrow-button should be shown at any time

If a JComboBox is not selected, the arrow-button on the right is not shown. This leads to the fact that a combo-box cannot be distinguished from a normal textfield.
The question is now: how is it possible to show the arrow-button permanently? I alrady came across BasicComboBoxUI.createArrowButton() etc., but I did not find out the mechanism of hiding / showing the arrow-button.
Can anyoune give me a hint how to show the arrow-button permanently ?
Can you add below code as first line in your main method.
Toolkit.getDefaultToolkit().setDesktopProperty("win.xpstyle.themeActive",Boolean.FALSE);
and then check your program.
sorry for the inconvenience. Everything is clear now. In have been told that we use a special framework which changes the behaviour. This information would have been useful if given to me earlier... :-(

How do I lose focus on a JComboBox?

I have a JComboBox with an key listener.
When I hit <enter>, I fire off some action, and then I need to to lose focus on the JComboBox!
To focus on it, I can do JComboBoxObject.grabFocus();
But doing transferFocus() to get the focus to a next element (I don't care WHERE the focus goes, just away from combo box) does NOT work.
Doing grabFocus() from another combo box works, but seems like a pretty annoying hack to me. Is there a better solution?
I can suggest you to first use the
.getNextFocusableComponent()
and then use the
.requestFocusInWindow()
that means Implementing it like this,
JComboBox.getNextFocusableComponent().requestFocusInWindow();
One important note is that .getNextFocusableComponent() has become obsolete but it can work really better, you can use it but If you have any other solution, I would prefer not using this.
Updated: Starting from this two-combo example, adding either of these lines to the actionPerformed() implementation seems to do what you want.
combo1.transferFocus();
combo2.requestFocusInWindow();

JSlider with two pointers

I want to give a user a convenient way to enter range from swing gui.
What can I use? A perfect option would be a slider with two pointers.
SwingX has JXMultiThumbSlider which is exactly what you're looking for.
Btw, this implementation of RangeSlider is more simple for me, but swingx stuff should work somehow too, if your implement Renderer first.
http://ernienotes.wordpress.com/2010/12/27/creating-a-java-swing-range-slider/

What is the best way to make clickable text in java?

This is for an application so I don't want a hyperlink. I first tried using a Jbutton without all of border/background stuff and then hooking up an actionListener to it but I couldn't get it to the point where I thought it looked nice. I also tried using a JLabel and hooking up a mouse listener to that but I also couldn't get it to look right.
Basically I would like a way using swing to make a button exactly like a url link in an application. What is the standard way of doing this?
but I couldn't get it to the point where I thought it looked nice
You might want to go into greater detail on just what "looked nice" means. I can see you solving this by either a JButton or a JLabel, but the key is perhaps not to look for another solution but to play with the settings of the button or the label til they look nice. If you can't find a nice solution, then post your code (an SSCCE would work best of all) and perhaps we can help you.
that isn't answer to your question but are you tried to add ButtonModel to your JButton example here
It is a rather heavy hammer to use, but SwingX has a JXHyperLink control that is probably exactly what you want. The source is at http://java.net/projects/swingx/sources/svn/content/trunk/swingx-core/src/main/java/org/jdesktop/swingx/JXHyperlink.java?rev=4027 and you can see an article about it at http://www.javalobby.org/java/forums/t18617.html.
It is old, but SwingX continues to do good things.
It's you're trying to make a desktop application which looks like HTML inside a browser, you might try using some of the richer Swing text components in a read-only mode. You could use a mouse-listener to map X/Y clicks to a particular character of text, and then cause an action to occur on that basis.

GUI program, problem with tabbedpanes

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.

Categories