When specific action is performed, I want to replace button with text field. I know that I have to call button1.setVisible(false); but I don't know how to create text field on the exact same place. I am using NetBeans designer, if you can give me a hint, how to add 2 components at same place, and switch between then, something like switching between layers in photoshop, if something like that is possible, would be great. Thanks
For many components in one space, use a CardLayout as see in this short example.
Related
I'm working on my custom ColorChooserPanel class, but I don't know how to create a ColorSwatch.
I found a class named ColorSwatch or something like that but its package private and I can't access it.
How can I ensure that I have a proper color chooser?
First, you need to think about the way this should work, to formulate your goals into action points that you can always check and see whether and how they are fulfilled:
it should be displayed
it should handle click events
it should be able to return a color (the last chosen color, or a default in lack of that)
Now, a very common way of supporting color choosers is to have something like in this image:
However, this is way too complex for a first implementation. You should first have something which "works", that is, you are able to choose colors to taste success and then work out the subsequent details. Unless you say otherwise, I'm assuming that the Basic colors section is good for now. In that case, you can create a class, which contains a JPanel, containing clickable elements. Your class needs to have a Color member, which one can get calling a getter and which is set when a clickable item inside your JPanel is clicked. The clickable items can be JPanels on their own, for example.
If you prefer something ready-made, then kindly read these:
https://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html
https://coderanch.com/t/332515/java/Color-Palette
I am trying the save the state of a previous frame and then carry it over to the other frame. More like saving the data of the textfields and areas so that when i press next button some text fields,and variables will be initialized in the next forms Labels and when you press back your previous data will still remain in your form inteface.Pls help
More like data binding but across different forms
You can use any varible for that, and pass it as parameter to every JFrame you create.
But it sounds more like you want to use a CardLayout for your JFrame and use different cards that can be shown for the user. See How to use CardLayout.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
Use something like Java's XMLEncoder.
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.
Sorry for the odd choice of words for the title, however, "border" seems to be the inappropriate term. While it is true that the visible line surrounding an icon in a JToggleButton can be made invisible by using setBorderPainted(false), the same is not true for JCheckBox and JRadioButton.
I can not use the JToggleButton and therefore need to use either the JCheckBox or JRadioButton (or some derivative of JToggleButton I am not aware of), but need the square or circle, respectively, to be non-visible when there is no icon on the button. Also, using setVisible(false) eliminates the button from the layout, however, I need the space to be reserved and not have the component layout change (using GroupLayout).
Any suggestions? Am I going to have to create a custom renderer? I will be looking at that in the mean time.
The route into this would be through customising the look at feel by changing some of the UI properties in the UImanager (the sort of thing that allows you to make simple tweaks with fonts and colours and presumably the images used for the checkboxes or radiobuttons) -- but it's many years since I last did that sort of thing and can't remember the details.
A little Googling turned up this project to inspect current property values, so might at least help with indicating the right part of the APIs to be looking at.
You have to choices here:
1) Customize Look and Feel as described in previous entry.
2) Create your own custom controls by inheriting from existing ones and overriding component painting.
I found a cheap and easy (read hack) for this. I created an empty transparent icon and used it when I didn't want any item to be displayed.
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.