Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm pretty new to Java and I'm currently attempting to make a simple work scheduling program. I'm trying to implement a way to add employee's and their info into but I'm a little stuck.
What I wanted to do was have a button that opens up a new window that will let me input their name (string), total hours to work per week (int), and their availability (array, check boxes that will translate to an array). Is it possible to customize a JDialog to do this or is there a better way to go about doing this? I tried reading tutorials on JDialogs but none of it explains how to implement multiple inputs.
I currently have it to where I'm opening up a new JFrame but I've read from multiple sources that I shouldn't do that.
Thanks for any help.
I tried reading tutorials on JDialogs but none of it explains how to implement multiple inputs.
This is no different than adding multiple components such as JTextFields, JRadioButtons, JComboBoxes in a JFrame. For both you'd create a main JPanel to hold the GUI, and then give it components and or other JPanels each using its own layout manager. Then create your JDialog or JFrame (using the API to see which constructor to use), add your main JPanel to the top level window (actually to its contentPane) by calling add(myMainPanel), pack the top level window by calling pack(), and display it via setVisible(true).
The key issue for a dialog window is often when to query its contents. If it's a modal dialog, then that's easy -- you query the contents (the state of its fields) after the call to display the dialog, since that code flow will resume once the dialog is no longer visible. For a non-modal dialog, then you'd need to add a WindowListener to notify you when the dialog is no longer visible.
For more specific help, you need to ask a more specific question and show code.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm creating a java application.there I want to keep jframe in maximized state.and I want to use it many of monitors that have different screen size.and I want to components in jframe to resize with jframe.also I need to add image to the background of the frame.that image also should resize with the jframe. how to do that?
Layout managers, layout managers, layout managers
Start by taking a look at
A Visual Guide to Layout Managers
How to Use Various Layout Managers
Using Layout Managers
You might also want to have a look at Full-Screen Exclusive Mode API, depending on what you're hoping to achieve
also I need to add image to the background of the frame.that image also should resize with the jframe. how to do that?
Java: maintaining aspect ratio of JPanel background image
How to set a background picture in JPanel
How do I resize images inside an application when the application window is resized?
I see lots of searching and research in your future.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have a menu, that whenever I click on one of the options, I want to clear the screen (to clear all my JLabels and text areas). I use the code
getContentPane().removeAll();
getContentPane().repaint();
add(comboBoxOptions);
to clear the screen. After that I try to add a combo box, which adds but it doesn't show up. I can click on the options but it's hidden somehow I guess. How could I fix this?
How could I fix this?
Use a CardLayout, see How to Use CardLayout for more details
Swing's layout management API is lazy, it won't update the layout's automatically, it waits till you tell it to. This is a good thing.
You need to use revalidate to force the container hierarchy to be relaid out and repaint to schedule a repaint of the view, for example
getContentPane().removeAll();
add(comboBoxOptions);
revalidate();
repaint();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In C# this is done by using delegates and event, here is the link: Passing Data between Windows Forms.
My requirement is like this. User have to search the item code by loading the whole items from the database to a new form's JTable, this new form is shown by clicking a button in the main form (or any other form). After that appears, user Double click on specific item code in the table and that item code is passed to the main form's (or any other form's) text field and closed the current form.
here is the output from c# https://www.youtube.com/watch?v=_lPkc1YV2vQ&feature=youtu.be
The second window should not be a JFrame, but rather a JDialog, possibly modal. If it is modal, then the calling window will know when the 2nd dialog window closes, since program flow stops at the calling code immediately from when the dialog is displayed (think of how JOptionPanes work) and does not resume until the 2nd dialog window closes. It is right then that you would extract pertinent data from the objects associated with the dialog window, and this information can be obtained by simple means such as by calling appropriate getter methods.
e.g.,
JDialog someDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
someDialog.add(myContentPaneWithMyGui);
someDialog.pack();
someDialog.setVisible(true);
// here code flow stops until the dialog is no longer visible.
// now call my getter to extract data
SomeType someData = myContentPaneWithMyGui.getSomeData();
If you want to stick with your event/delegate way of doing, you could consider using BeansBinding.
It requires your model to fire events when their data changes, and add your Swing GUI elements registers as propertyListeners on those models.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am quite new to Java, having the following question:
I want my Swing Applications users to put in various textfields in some frames.
That should happen like this:
When the application is started, a frame opens with some textfields and a submit button.
The submit button sends the textfields data to the main method and closes the active frame.
Then a new frame opens with some more textfields and a submit button and so on.
How can I implement this in Java?
I understood that building multiple frames is not best practice, but what is the alternative?
Thanks for your help!
How can I implement this in Java? I understood that building multiple
frames is not best practice, but what is the alternative?
IMO the best alternative in your case is to have only a window (probably a JFrame), and using a CardLayout to switch between (probably) panels when an action happens. Also read The Use of Multiple JFrames: Good or Bad Practice?
Besides official tutorials that have examples, this answer provide a commented example how to use CardLayout
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a toolbar with a combobox. When the item changes on the combobox, it makes a call to the draw panel (which the toolbar has a reference to) to tell the draw panel to change the shape type. Now, the the draw panel has a popup menu that changes the shape type aswell. Therefore, when the popup menu changes the shape type, the combobox needs to be notified as well. The only way I can think of it is for the draw panel to have a reference to the toolbar, but I do not like the idea of a circular dependency. Is there a better way?
Thanks
According to the model-view-controller pattern which is commonly considered the best approach for GUI coding, you'd have a model, a view and a controller. In this case, the underlying model would be some value corresponding to the state of the combo box and what the popup menu shows. The views are the combo box and popup menu. They are both also controllers since they can alter the model's state.
So the "clean" way to implement this is to hold the state somewhere separately and have a list of components there that should be notified when the model state changes. On a state change, the list should be iterated and the components in it should have their view updated. The combo box and menu entry should be registered as such "listeners".
Now, I said that was the "clean" way, but for a simple setup as yours you don't necessarily have to implement it this way. It can complicate things and make the code less legible. On the other hand, doing it the MVC way will make future changes easier, like adding new views/controllers. Doing that without MVC would result in more and more components requiring knowledge of each other's existence, while now it'd all be cleanly encapsulated in a model.