I'm trying to make a Java11 GUI with Swing and I'm using a JPanel as a Canvas to draw on it images/drawing and I do also want to introduce some text on the Panel so that I can export the whole content of JPanel later.
Thought about using drawString() method in my PaintComponent() method, so should I use a JOptionPane to insert keyboard input or there are other better practices to write directly on JPanel until Enter key press or Escape.
Also, the main question I have regarding this, how I can make the text to be wrapped on multiple lines?
Related
I'm working on a 2D RPG game and am currently working on a dialogue window that pops up, to prompt the user to enter the quantity of a given item they would like to buy/sell in a shop. For this I need the user to be able to provide a number as input.
I've read some docs on JTextFields, but I'm mostly using my own graphics in this game, drawing to the screen with a Graphics object and don't want to use the standard Java JTextField Swing graphics, as it would be out of place. I have my own, fully working, KeyManager handler class that handles keyboard input in my game. Do you know of any approaches that would allow me to make custom text fields, which are focusable (to type in)?
Another question I have is: Is it possible to remove the JTextField border and set the background to full transparent, so only the cursor for typing is visible and I can add my own implementations of a "textbox"?
Then finally: My game opens with a JFrame and the graphics are drawn to a Canvas. A JTextField should be added to a JFrame, to a JPanel, right? If this is the case, should my DialogueWindow class extend a new JFrame and contain a JPanel to add the JTextField to or do I have to add this to my existing JFrame?
I have little experience with using the Swing library, so hoping someone can answer these questions for me.
tl;dr:
I don't want my input field to look like the standard JTextField below, how to implement my own without using the standard Swing graphics?
"How I don't want it"
Try using the Graphics class. But that would be really complicated. You will implement a KeyListener to the entire container. Whenever you pressed a key, some text should be drawn to the screen. Whoops!!! the coordinates (x,y) of the text doesn't move automatically like JTextField does, you need to increment the x whenever you type. and reset it to 0 if the user hits ENTER and increment y. The value that will be added to x and y depends on Font Size.
I am trying to create a code that simulates a MS Paint in java using Jframe. I want to create a textbox like field like MS Paint has,wherein you drag a box and according to your preference you set a size for it. What I do is I draw a rectangle first and then get the dimensions by mouse event listener and pass these values to a function that creates a JtextArea of given size and width. However, I need to extend the Jframe class which creates a new frame on top of the one that already exists. I try to pass my original frame as a parameter to draw upon for the JtextArea which does not work. Is there any way of implementing JtextArea without extending the frame class? And If possible any relevant example to draw a textbox which is similar to MS Paint. Please note that I don't want to use the Graphics.drawstring method. Thanx.
Since you don't want to use drawString() directly, java.awt.font.TextLayout is probably the best option for rendering text.
Create a temp JTextArea and add to your drawing panel with null layout to be placed over the rectangle.
When edit is done (text entered) remove the temp textarea, get the user entered text and draw it in the original rectangle.
I'm quite new to Swing, and I'm programming an application with NetBeans' UI designer.
Now I have an JPanel called "editorPanel", and it must be able to display multiple things. (so, sometimes it has to display an image, and sometimes it has to display a text editor)
I have made separate panels for this, so say I'd have a JPanel called ImagePanel and one called TextPanel. It has to switch easily between them, so I tried this:
editorPanel = new ImagePanel();
But that didn't work.
So, what I want to do, is set an empty panel to a defined panel.
How can I make this work?
The proper way to achieve your goal is to using a card layout and switching panels accordingly.
You ca get some idea on how card layout stuff is working in here
I have an open-source java swing application like this:
http://i47.tinypic.com/dff4f7.jpg
You can see in the screenshot, there is a JPanel divided into two area, left and right area. The left area has many text links. When I click the SLA Criteria link, it will pop-up the SLA Criteria window. The pop-up window is JFrame object.
Now, I'm trying to put the pop-up window into right area of the JPanel, so that means no pop-up window anymore, i.e. when I click the SLA Criteria link, its contents will be displayed at the right area of the JPanel. The existing content of the right area of JPanel will not be used anymore. The concept is just same like in the java api documentation page: http://docs.oracle.com/javase/6/docs/api. You click the link in the left frame, you'll get the content displayed at the right frame.
The example illustration is like this:
(note: it's made and edited using image editor, this is not a real screenshot of working application)
http://i48.tinypic.com/5vrxaa.jpg
So, I would like to know is there a way to put JFrame into JPanel?
I'm thinking of using JInternalFrame, is it possible? Or is there another way?
UPDATE:
Source code:
http://pastebin.com/tiqRbWP8 (VTreePanel.java, this is the panel with left & right area divisions)
http://pastebin.com/330z3yuT (CPanel.java, this is the superclass of VTreePanel and also subclass from JPanel)
http://pastebin.com/MkNsbtjh (AWindow.java, this is the pop-up window)
http://pastebin.com/2rsppQeE (CFrame.java, this is the superclass of AWindow and also subclass from JFrame)
Instead of trying to embed the frame, you want to embed the frame's content.
There is (at least) one issue I can see with this.
The menu bar is controlled by the frame's RootPane.
Create you're self a new JPanel. Set it's layout to BorderLayout.
Get the menu bar from the frame (using JFrame#getJMenuBar) and added to the north position of you new panel.
Get the frames ContentPane and add it to the center position of the panel.
There is undoubtedly countless other, application specific issues you will run into trying to do this...
No, you don't want to "put a JFrame into a JPanel" and your illustration above doesn't demonstrate this either. Instead it's showing a subordinate window on top of (not inside of) another window. If you absolutely need to display a new subordinate window, I'd recommend that you create and display a JDialog. The tutorials will explain how to do this, or if you get stuck post your code attempt and we'll help you work with this.
Edit 1
You state:
I need to convert from the pop-up window style into the jpanel content style. It's just like the java api documentation page style: docs.oracle.com/javase/6/docs/api When you click the text in left frame, it doesn't show any pop-up, right? The content is displayed at right frame directly. So that's basicly my goal. The source code is quite big. I will try to paste the source code if possible.
What you are looking for is to simply implement a MouseListener in a JList or JTable, and when responding to the click get the content based on the selection made. This has nothing to do with placing a JFrame in a JPanel and all to do with writing the correct program logic. Again, display it in a modal JDialog -- but that's all secondary to your writing the correct non-GUI logic. You're really barking up the wrong tree here. Forget about JFrames, forget about JPanels for the moment and instead concentrate on how you're going to extract the SLA Criteria data when it is clicked on.
Edit 2
I think I see what you're trying to do -- instead of JFrames and JDialogs, use JPanels and swap them using a CardLayout which would allow you to swap views.
I had skimming the source codes, I saw that the AWindow.java has internal panel (APanel.java) to hold the window's content, and it also has a public method to return the content panel object (getAPanel()). With this, I can use it for fetching the window's contents into other container.
Finally, I decided to use JTabbedPane in the right area of VTreePanel for displaying the pop-up window's contents.
You cannot put a Jframe into a JPanel. Instead you should try to create a separate panel that has functionalities like your JFrame and embed that into your JPanel.
Since you can put a JPanel into another JPanel but not a JFrame into another JPanel
I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p