I am trying to create a text based adventure in Java I need help in combining a Scanner with JPanel so that the user can type into the panel directly rather than choosing from a list of options. Is there any way of doing this or would a dialogue be needed and if so is there a way of doing so within the JPanel?
For doing so, you need to add some components to your panel, such as, JtextField or JTextArea.
Then you have to add a action listener to your text field/area.
You can find a lot of advices with a simple google search.
Related
I am currently working to create a GUI and encountered the following problem:
Similar to a JComboBox or JMenu i want to create a drop-down-panel with java, but i don't want to list a few JMenuItems or something similar. What I want to display is a new JPanel. You could compare it to the chatwindow in facebook.
Not eve Google could help me. All I could find was about JComboBox or JMenu.
So here is my question: Is there already a predefined class, that im just unable to find or do I have to make one for myself? If I have to create it, how do I do it?
To bring this to an "official" end:
You can actually add any Component to a JMenu. You are not restricted to only JMenuItems.
Thank you VGR for your comment
I'm building a program in Java, using Swing, that will act as an interactive presentation.
I have paragraphs I need to display (presumably in JLabels) , and within each paragraph are certain words and phrases that need to be formatted differently (have a different color), and I need them to call a method that will display something else when clicked or hovered over.
I know there must be a way to accomplish this...
If you want to apply some style, you can use HTML in your JLabel's content.
If you need some custom behavior and you want to handle it in a different way for some parts of your JLabel, you need your component to be split in a more detailed way.
Create a container(JPanel) and arrange you components within it. From now, your smaller components will be able to listen for events like mouseEntered and mouseClicked and handle them separately, not confusing the whole JLabel component.
In this way, every smart part of text will be a standalone component.
I'm trying to create a java gui, currently I'm playing around in the netbeans IDE using their gui creator, but I've also been reading a book about Swing and trying to learn it that way as well. Im hoping someone can help me with a problem I'm having. I'm attempting to allow users to have a "pool" of fields they can choose from (for instance a title, a paragraph, a text fiend, buttons, etc) and be able to move items from the pool into another potion of the window which would let them create their own layout. At this point I don't need these fields to DO anything, but I do want the user to be able to move them around and create their own layout. Is there any way to do this?
I think that to do this, you'd need to use a null layout on the container that would hold the movable components, and you'd have to give the components MouseListeners and MouseMotionListeners (conveniently combined into MouseAdapters) that are active when the program is in the set-up state, but then inactive when the program's components have been all set.
You could use the DragLayout from the tips4java website.
Is the above question possible? The effect I'm trying to achieve is similar to how MS Word displays "Document- Microsoft Word (Technical Preview)" in this picture link: http://img.blogsolute.com/ms-word-2010.png, but with a colored background.
You can set the title of any frame you create by passing the title string to the constructor of the JFrame. You can't, however, add any controls to the 'decoration' portion of the frame - i.e., the title bar.
What you probably can do, however, is create an undecorated frame, and manually add the decoration using customised Border objects. This effectively allows you to put any controls you like around the outside, and the root pane will happily work inside it.
Why do you need JLabel for that? You can use setTitle("") for this purpose
I have to create an xml file based on the value of attributes set by user in dialog of Java Swing.There are five attributes name,age,sex,date of birth and place. when the user clicks on OK button of the Dialog box an xml file should be created in the temp directory of the user.
Kindly help me as I am new to Java Swing
Regards,
Kumar
Create a GUI with controls to collect the data you specified. Create a class to generate the XML output. Call this class.
To build a Swing GUI, you start with a JFrame.
Within this JFrame, you have a master JPanel.
The attributes will each have JLabel components and JTextField components. You will use a layout manager to arrange the label and text field components.
You have a JButton so that the user can tell your program that she's entered all of the attributes.
You should be able to look up all these Swing components and build a GUI.
Then, as jzd said, you create a class to generate the XML output and call this class from the GUI.