If I had a program that had a list of a few links, how could I add another link to the program, without changing the source code itself? I was going to create a "new" button in the corner of the application, that created a new item (I already know how to do this part)
Just as the comment said, if it is a list then you could make it so that when you press the button it calls listname.add("...") and adds it to the list. Just for future reference, when asking questions, try to put as much detail as you can in the question, to help people answer it.
Related
I'm currently working on building my first app. :)
I've now coded several Forms, each in an own class. Now, I want to implement the navigation from one Form to another, this is pretty straight forward since there is always only one "next" Form for every Form currently.
So i tried to do this by adding a "nextForm" Form to every Class and I want nextForm.show() every time the "next" Button is pressed. I tried it this way:
login = new Button("Login");
login.setUIID("nxtButtons");
login.addActionListener((e) ->
nextForm.show()
);
So, when i actually click the button, nothing happens.
Is the way I try to code this a good way? This is the first time Im heading into GUI building and I don't have experience here yet. Maybe one of you guys could help me here :)
Thank you very much
Kind regards,
Max
I'm assuming nextForm is null and you should see the null pointer exception in the console somewhere.
If not nextForm might be pointing at the current form which will do nothing. You can place a break point on that line in the debugger and see that the break point is reached then inspect the values of the variables.
To put this simply, I have some Menus in my Menu Bar that, when at start of the application, goes over a List when you expand it. However, if the List is ever updated, the Menus will go behind the List, covering Menu Items.
I have theorized that I need to when the list is updated, to make tell the list to go back to its layer (which I set it to the bottom, lowest on the list in application I am using which is NetBeans). But, I do not know what to call to tell the program to keep the list there. (I am still very new to Java and learning as I go) Does anybody have an idea on how to do what I wish or even better, what is causing the problem?
Thank you for your time and have a wonderful day :3
You should use JList instead of List.
The problem is that Components in java.awt have peer components, that are native OS components, whereas swing is 100% java. You cannot write over these native peers... at least not in java.
I am new to Java and I am working on a project where depending on number of files in a directory,
buttons will be created respectively. Each button will have a customized right click context menu.
How can I achieve this or is this feasible?
I just want to know the approach to do this.
The approach that you may try:
While you iterate your directory/file list (or other process that will determine the button creation), you can generate (create an instance of) a new button (JButton), I assume you know how to use new, and put it on your form / panel.
However, most of the time, layout would become an annoying issue here.
Thus, you may try to use MigLayout to handle this.
It will help you a lot in putting your stuffs in a tidy and convenient way.
Try this approach and when you have a specific coding-part question, you can try to search the existing solution in SO (StackOverflow) or if it doesn't exist, you can ask that specific code-related question.
Hope it helps.
I am working on a simple Java applet multiple choice quiz that will display a question with three choices. I am pulling the questions and answers from a text file and want to loop through the questions as the user answers them. So every time the user hits 'Submit' the program will check the answer then update the labels with a new question. My question is how do I get the loop to wait on the answer? Should I put all of the code in the button event handler? I thought about Cardlayout, but it seemed inefficient. I don't really have any code yet; I'm still in the planning stage. Thanks!
I would suggest:
First of all regarding, "My question is how do I get the loop to wait on the answer?", I wouldn't even use a for-loop or any similar loop to solve this.
I would create a non-GUI class to hold each question, the possible answers and the correct answer. Consider calling it Question. It would have a String field for the question itself, a List<String> for possible answers, and either another String for the correct answer, or it could hold an int for the index to the correct answer, or even have the correct answer always be the first one, and be sure to randomize the display of answers.
Create an ArrayList of this class, ArrayList<Question>.
Read in the file all at once, filling your ArrayList.
Give your main GUI class an int index variable for iterating through this ArrayList.
When a submit button is pressed, increment your index and get the next item in the List (if not at the size limit of the List).
Consider creating a JPanel class for displaying each question and the possible answers, perhaps called QuestionPanel.
You can either create one display object and swap out the text of your question JLabel and the text of your JRadioButtons. This works nicely if all questions have the same number of possible answers.
Or you could create multiple QuestionPanels, and swap them via CardLayout.
There's nothing "inefficient" about a CardLayout. I have no idea what you're talking about here. Care to elaborate what is inefficient about it, and just what you mean by the term "inefficient"?
I need to make an expandable list using java swing. I will attempt to demonstrate:
Unexpanded:
>[Expand me!]
>[And me!]
Expanded:
|[Expand me!]
>[Expand us too!]
>[Expand us too!]
>[Expand us too!]
>[And me!]
So, when you click on the "Expand me" portion of the list, another lists will drop down, possibly containing more expandable lists. If you were to click on it again, it's "sub-lists" would then retract. Pretty basic. And, as you can see, I am not looking for JComboBox, and I do not think JList can do this. If someone were to point me in the right direction, or give some programming examples, I would be grateful.
Thanks,
MirroredFate
How about using a JTree.
A control that displays a set of hierarchical data as an outline.
You can try using a JTable and put a button in the first column. When the button is clicked you add more data in the rows in between.
update
Something like this:
Or this
I think the first uses a JTree but that the idea.
BTW these two belong to JIDE Soft, check if it is feasible for you to buy a license:
http://www.jidesoft.com/products/grids.htm
Is not trivial to roll you own but is not impossible either.
check for TreeTable or one example or Outline, but with notice, that on official Java (SnOracle) pages any progress died ...,