I have written a GUI which has many different JComboBoxes. All of them work as I would expect (I click on them, the popup appears, I click on my selection, and the popup closes).
All of them, that is, except for one.
I have looked at my code, and I am using this combo box in the same way as I am using others. I am sure that there is something that I am missing, but I do not know what.
I do not know where to start. What kind of thing should I look at in my code to figure this out? If you were to try to achieve this, what would you do, this info might help.
As a code sample, I have not done anything out of the ordinary:
JComboBox selectionBox = new JComboBox();
selectionBox.addItem(DEFAULT_SELECTION);
selectionBox.addItem("Option1");
selectionBox.addItem("Option2");
selectionBox.addActionListener(this);
Although I understand that it isn't always possible to give a lot of details, an exact answer just isn't possible without more exact information.
That said, I think the best course of action is to examine the code very carefully and make sure that an error cannot be found that could be causing the JComboBox to stay open.
As a LAST resort, you may be able to fix the problem by using JComboBox's hidePopup() method to manually close out of the JComboBox:
yourComboBox.hidePopup();
P.S. As for not knowing where to start ... if, in general, you're having trouble finding errors in your code, https://softwareengineering.stackexchange.com/ might be a good place for that sort of question.
Related
I added an image below to show what i am trying to achieve. I've seen similar implementations of this else where but isn't sure what it's called.
What it will do is when a box/panel/card on the left is clicked, a detailed panel on the right will be displayed, showing very detailed information. If a user simply wants to see a little more, an arrow or a "show more" hyper link will expand the panel/card to display more information.
I'm wondering if some sort of java plugin is already in existence which i can modify and utilize to fit my needs, instead of coding the entire thing from scratch.
If anyone has any idea of this, i would love to hear more! Thanks in advance!
As I'm modifying an existing Look and Feel, I also want to change how the "buttons" of a PopUpMenu behave. Right now it behaves like this, when I hover my mouse over it. As you can see it behaves very "3D":
And I want to let it behave like the buttons I made below them:
I've looked trough alot of documentation of Java Swing but I can't seem to find it. So if someone knows, please help me out. I have tried to change every property I could find.
The correct answer here is, as I found out, not everything can be managed by the LaF. Therefore, sometimes you have to get your hands dirty.
In this case I created my own CSTMButton, because in Swing one is also able to add buttons to a menubar. Now I can create it's own listener to generate the behaviour I want.
I am doing a simple GUI painting box program.
However, I have a problem with adding 2 similar separated groups to be corresponding in the same way.
I mean when I click the JRadioButtonMenuItem Line, then the JRadioButton Line below also has to be selected too. What should I do?
Do you need to see my code?, please let me know
Thank you so much.
P/s: it says I need 10 reputation to post image
Share the model between the two radio buttons:
JRadioButton radioButton = new JRadioButton("Line");
JRadioButtonMenuItem radioMenuItem = new JRadioButtonMenuItem("Line");
radioMenuItem.setModel( radioButton.getModel() );
Actually, you should share the Action as well between the two components. Read the section from the Swing tutorial on How to Use Actions for more information and examples.
The exact solution depends a lot on how your code is structured right now. I bet that the standard library has some functionality to accomplish what you want to do, but if you want to go ahead and implement it then you might as well (minimal time input and you learn something).
The most direct solution that comes to my mind is to encapsulate selecting a button in a method that will manipulate all sets of corresponding buttons. I am going to assume that you are using action listeners for the buttons right now, if not you could adapt the idea. In the action listener, you can detect the mouse click and perform some work as necessary. That work should include updating the other buttons appropriately too. You could even create a method that both action listeners call and updates all necessary sets of buttons.
It is also possible to use the same action listener on both sets of the buttons, but you'll need to know which selection the user wants to be active (likely an easy task).
My Java is pretty rusty, so I am not including any example code, but if anything is unclear or you think an example would help I can do so.
Hope at least something here helps you. Good luck!
As a way of learning Java, I'm writing this little application for grade schoolers to practice basic math. The idea is that the kid does any number of math problems, as long as they are in the app, it just continues to throw problems at them until they click a 'Done' button. When they decide to be done, I want a new JFrame to come up that will show them all of the problems they attempted, along with their answer, and whether they got the problem right or wrong.
The advice that I am looking for is what is the best way for me present these results. I looked into the GridLayout and the GroupLayout, but I don't think that these are exactly right. I did something similar in VBA for Excel, and there I just ran a for loop with one iteration for every problem they attempted. Each iteration would add a row of labels to the frame with the elements of the problem displayed in the various labels. I tried this in Java, but I'm not even able to get the labels to even display.
So before I get all specific and start posting my code, I want to ask a bigger question, which is "what is the best method to create a view like this?" Often, I go off in one direction only to waste time before somebody suggests a totally different (and better) approach.
Thanks!
(edit: here's an image of how I did this in Excel. I'm trying to repeat basically the same thing in Java)
One simple way to make that design would be to use a mix of components. You could have a bunch of JLabels and JPanels stacked in a vertical FlowLayout. The grid you have described would be best designed in a JTable, something like the below:
If you like tables like Excel then, Java provides JTable class to create tables, if you want.
Tutorial : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
I will explain my question clearly.
I need to zoom in/zoom out the world map.
When I click on the particular country in map, control should redirected to new page with respective the country.
I dont have any idea about this in java. Please explain the steps to acheive the above task.
As the question is quite general, here is a general answer: Zooming often means, that you want to display a certain percentage of somethin, and not the whole, where your size of the displayed will not change.
But in your case it seems more like a "find a mouse click in a polygon" thing. So you have to add a selection/click listener to whatever widgets you use (Swt? swing? ....?) where you change what your program renders.
It sounds like you may be trying to reinvent the wheel. Google etc have already solved this problem rather well. It might be better to incorporate an existing solution into your application. Have a look at GoogleEarth inside Java Swing.