I'm having difficulty in getting resources that will help me with an issue. I want to add a button that will let me insert text anywhere in my JFrame so I can click and add a text
Before that, I have been using JTextField and JTextArea, but these are useful in a predefined space (I believe).
What I want is like the classic Add Text button in Paint ("A" button) in which when you click on it, you will have the text cursor (like an "I") and then click somewhere in the frame so I can actually add text in that area. Which I call "adding text dynamically". Unfortunately I don't have any code to show because I'm not sure how to build the code from scratch or maybe use a method in Swing, but I'm more than keen to explain better if this is too generic for you guys.
You're going to need to be familiar with Java graphics and associated methods, particularly drawString(), so start by reading the official Graphics Tutorial.
Then learn about MouseListeners, which will help you recognize when and where the user clicks in your JFrame.
Then learn about KeyListeners, which will allow you to detect when your user is typing. You may wish to use a key bind instead, but as described, your UI is relatively simple with only one focusable component, so keylisteners are probably OK.
This general strategy should help you take a flying leap at the problem:
detect mouseclick with mouselistener
draw an insertion bar
detect keypress with keylistener
use graphics.drawstring to draw the character specified by the keypress
redraw the insertion bar
Make sure your keylistener handles the enter key, so that you know when to erase the insertion bar. You're going to paint your JFrame in between each of those steps.
The result will be crude, to say the least, and you'll notice that you have to handle a lot of issues that crop up along the way before things start to look professional. For example, you'll have to deal with font metrics. Not every character is going to be the same width, so you have to calculate the X argument to your drawString() based on the charWidth of whatever character the user types. You might do well to start with a monospaced font for that reason.
Related
I posted a similar question a year ago, but it was not really well written and I didn't get an answer I could work with. Now I stand in front of the same problem. I got a JPanel (my content pane), where a MouseListener is implemented.
Everywhere I click, I get the exact coordinates of my mouse click. Except my JTextField components. When I click on those, the MouseEvent isn't even triggered. H
ow do I do this, so my mouse clicks on those will also call the mouse event?
Tried: setEnable(false) and setHighlighter(null)
Sorry thought I fixed the X/Y problem.
The X/Y problem simply means you are telling us what your attempted solution is without telling us what your requirement is. We can't suggest a different approach if we don't know what you are trying to do.
I want to open a menu,
Now we know what the requirement is.
The solution is to add the MouseListener to the text field, not the panel. If you have the same popup of the panel and the text field, then you still need to add the listener to both the panel and the text field.
You can do this in one of two ways:
Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.
Note the above tutorial is a little old, you can also check out the setComonentPopuMenu(...) method of the JComponent class. This approach will create the listener for you.
I want to implement key listener on my puzzle game.
i have done it with action listener but now want to move on with key listener.
My logic for action listener was that:
when a specific button is clicked
it checks if is adjacent button's icon is null
if it is null then their icons will be swapped
Now, how can I do it with key listener?
Thank you.
if( b1==e.getSource()){
if(b2.getIcon()==null){
b2.setIcon(b1.getIcon());
b1.setIcon(null);
}
else if(b5.getIcon()==null){
b5.setIcon(b1.getIcon());
b1.setIcon(null);
}
}
You tell us that you have implemented a KeyListener but it's not working. Without code, all we can do is guess, so here's mine:
KeyListeners require focus to work, and so if your GUI has any components that steal the focus, such as JButtons, all JTextComponents such as JTextArea or JTextField, JComboBoxes, JLists,... then your KeyListener won't work.
One kludge is to force the component with the KeyListener to have the focus and to greedily hold on to the focus. This is not advisable since it will force your program to behave abnormally since most users expect buttons to have and retain focus when pressed, and text components won't work if they're not allowed focus.
Again, often the best solution is to use Key Bindings since these can work without requiring focus and are a cleaner way to capture keystrokes. Please look at the Key Bindings Tutorial and then have a look at my example code for using Key Bindings here and here.
Again for better and more specific help, then please tell us more of the details and show us your pertinent code, preferably as a minimal example program or MCVE.
I would like to implement the mouse selection in java in order to obtain the component multiselction in a JPanel, but I'm still struggling to understand if it is possible.
In my case I got a Jpanel with a matrix(I have not used a Jtable for space need) of JButtons and my idea is to allow the user to select a subset of JButton in a row/column at the same time.
I tought that a good option can be to draw a trasparent rectangule with mouse and than get all components that fall in the rectangule but I'm not sure whether this solution can be a good well-functioning solution.
Any suggestion about it? Do you have any other better solution or advise?
Thanks in advances
I'm trying to provide a progress report for a slow operation, in the form of text scrolling up from the bottom of the screen with details on what's going on - it's an effect you may have seen a few times in video games when they're loading maps, making network connections and suchlike.
Glass pane seems to be the way to get the text overlay, that much I have working. My problem is exactly what component to use for the actual text display.
JTextArea can display text, but as far as I can see, it can only do it from the top of the screen down - is there a way to make it scroll text up from the bottom of the screen?
JLabel by contrast can align the first line of text to the bottom of the screen, and even take appended text on that line, but when I add more lines separated by newline characters, it just seems to swallow them up even after calling repaint and validate. Is there a way to make it scroll up with the new text?
Or is there another component I should be using instead?
I really like JXLayer for effects layered over Swing components. JXLayer was at one point scheduled to be included in Java 7. Unfortunately the moving around that has been going on Java.net lost all the good content that the author had. There are still some other great resources around (Java 7 required for this one) on the web. I use JXLayer to provide panels with a busy state having a web-like spinner and greyed out appearance.
Another alternative (not as capable as JXLayer IMHO) is MigLayout has absolute positioning, which is maybe easier than the GlassPane.
JLabel would be the easiest. Otherwise you will have to override paintComponent to do anything fancy like animating the text movement.
The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.