I extended JButton so it would fit my needs. I have one JTextArea and two JLabel components inside of my new class. The problem is, that I cannot click through JTextArea. So, when mouse is in JTextArea bounds, the button is not responding. There is no problem with labels.
As on screen. There are four buttons. Each one is a separate yellowish rectangle. When mouse is over JTextArea's gray rectangle, I cannot press the button. I need JTextArea because it supports multiple lines. Is there any option to make it not intercept the mouse?
It would be ok if I could attach ActionListener to JTextArea, but I can't. It cannot have this kind of listener.
You look to be trying to use a JButton in a very non-button way, including having it hold a JTextArea, and not looking at all like a button. If you want a clickable area that is not an identifiable JButton, then consider using a MouseListener instead. You would likely have to add the same MouseListener to the container JPanel and the JTextArea.
Take a look at Concepts: Editors and Renderers to understand the difference between a renderer and an editor.
A renderer is a "rubber stamp" of the component, it is simply "painted" onto the surface of the JTable and is not a real life component
You would need to implement a custom editor, which could translate the trigger event (in your case the MouseEvent) into a local context.
Take a look at TableCellEditor#isCellEdtiable which is probably the closest you will get to the source of the event which might trigger the cell to become editable.
JButton could be seeded with a HTML String (`"This is some text"), which would be capable of supporting multiple lines and line wrapping as well
Having said all that, you might want to seriously reconsider your design...
Related
I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.
I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue
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
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.
Is it possible to place a child component inside a JButton and make it transparent to a subset of mouse events so that:
The child component receives MouseMotionEvents (so it can respond by modifying a displayed image)
Clicking still depresses the JButton "behind" the child component
If you add the child component to the button but make no other changes, clicking in the area occupied by the child does not activate the button.
I know this can be achieved by creating a new class that extends JButton but I would prefer to use a child component which has already been written.
Note: this is purely for cosmetic reasons. The child component only changes its own appearance. It does not perform any other actions in response to clicks. There is just one Action, triggered by the button in the normal way.
Yes, it is possible but probably there are better ways to change the appearance of a JButton on mouse over. You can extend a ButtonUI to do that. However, if you want to drop a component over a JButton, you should pass other mouse events (e.g clicks) to the underlying JButton.
JXLayer is just the thing you need. Check out https://jxlayer.dev.java.net/
The project site has several good articles about JXLayer's usage for many different use cases.