java resizing methods - java

Could you tell me what methods are called upon a JFrame/JDialog after you resize it manually?
( after you resize it using the mouse cursor, while the frame is visible on the screen). I noticed that my JDialog is not valid eventhough I call validate() or revalidate() upon it, but after I resize it a bit, my frame becomes valid.

I think it is java.awt.event.ComponentListener
The listener interface for receiving component events. When the
component's size, location, or visibility changes, the relevant method
in the listener object is invoked, and the ComponentEvent is passed to
it.
For example:
public class MyFrame extends JFrame implements ComponentListener {
#Override
public void componentResized(ComponentEvent e) {
// re compute?
repaint();
}
}

With this code I am able to resize the width, height of 3 tables.
container.addControlListener(new ControlAdapter() {
#Override
public void controlResized(ControlEvent e) {
tableJobs.setBounds(20, 143,container.getBounds().width-40, container.getBounds().height-20);
tableMessages.setBounds(20, 143,container.getBounds().width-40, container.getBounds().height-20);
tableSplfs.setBounds(20, 143,container.getBounds().width-40, container.getBounds().height-20);
}
});

Related

How do I test for a change in the size of a JFrame?

I am trying to make everything in my window change size when the window size is changed. I am making a custom UI and need to set the layout to null, but I also don't want the window to have a never-changing size. My solution to this is to figure out when the size has changed then update the components of the frame to match that size. How can I do that?
You can define a componentResized method in a ComponentListener (or ComponentAdapter).
This was explained before:
Window resize event?
If I understand, you can use an componentListener, adding it to JFrame.
I use it as:
addComponentListener(new ComponentListener() {
#Override
public void componentResized(ComponentEvent e){
e.getComponent().getWidth();
e.getComponent().getHeight();
}
#Override
public void componentMoved(ComponentEvent e){
}
#Override
public void componentShown(ComponentEvent e){
}
#Override
public void componentHidden(ComponentEvent e){
}
});

Allow parent to handle mouse event

I added a JPanel to a JRadioButton - so that I may display whatever I want in the radio button.
This all worked as expected. But to allow for text wrapping, I used a JTextArea and added it to the JPanel contained within the radio button.
Now I have an issue where, if the user clicks on the JTextArea, then the JTextArea consumes the mouseEvent and as a result there is no response from the radio button (it doesn't 'select').
Is there a way get the JTextArea to ignore the mouse click, so that the parent may handle it instead?
I tried add the JTextArea's listeners to the radioButton instead.
I also tried to remove its listeners completely, but both these attempts failed.
Anyone have any suggestions?
Strong beware
Most JSomething are not meant to be used as containers even though it's possible - the outcome of doing it anyway is more or less visually and behaviourally undetermined!
That said, did it recently, to implement something similar to a Windows task dialog. If the requirement includes keeping the button clickable (and why else would you mis-use it as a container :-) the main problem (layout apart) is to make all added components completely mouse-transparent. Which is more difficult than can be expected. The minimum is to not allow adding of mouseListeners and disable the acceptance of mouseEvents:
final JTextArea area = new JTextArea("replacement ..") {
#Override
public synchronized void addMouseListener(MouseListener l) {
LOG.info("adding here ...?");
}
#Override
public synchronized void addMouseMotionListener(
MouseMotionListener l) {
}
#Override
public synchronized void addMouseWheelListener(
MouseWheelListener l) {
}
#Override
public void addNotify() {
disableEvents(AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.MOUSE_WHEEL_EVENT_MASK);
super.addNotify();
}
};
Plus make sure it's not focusable
area.setEditable(false);
area.setFocusable(false);
area.setRequestFocusEnabled(false);
Plus unregister dragging and tooltips
ToolTipManager.sharedInstance().unregisterComponent(area);
area.setDragEnabled(false);
Nevertheless, there might still be surprises ahead, f.i. call the following twice (that is disable and enable again), which will internally re-enable mouseEvent:
area.setAutoscrolls(!area.getAutoscrolls());
So at the end of the day, we might get away with it - but never be entirely certain that we succeeded.
What about this? Create and add your own MouseListener to TextArea
JPanel p = new JPanel();
JTextArea t = new JTextArea("line \n line");
t.addMouseListener(new MyMouseListener());
p.add(t);
jRadioButton1.add(p);
jRadioButton1.addMouseListener(new MyRadioButtonMouseListener());
And in the MyMouseListener Dispatch event
private class MyMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
Component source = (Component) e.getSource();
source.getParent().getParent().dispatchEvent(e); // 2x getParent() because JTextArea->JPanel->JRadio
}
.
.
.
}
And finally RadioButtonMouseListener
private class MyRadioButtonMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("CLICK ON RADIOBUTTON !!");
}
.
.
.
}

java - how do jcomponents draw?

im wondering how a jcomponent gets painted on the screen, is it painted inside paintComponent() from Graphics? Or is it painted separately. Im asking this because its weird how a jbutton changes color on mousehover even though repaint() is never called.
Thanks for your time.
Components are painted with their paint method. repaint is just a useful method that will call paint at some point in the near future on the Event Dispatch Thread.
When the mouse enters a JButton, the following method is called (for JButtons with a default UI):
public void mouseEntered(MouseEvent e) {
AbstractButton b = (AbstractButton) e.getSource();
ButtonModel model = b.getModel();
if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
model.setRollover(true);
}
if (model.isPressed())
model.setArmed(true);
}
ButtonModel.setRollover will fire a ChangeEvent, which is handled by AbstractButton in the following way:
public void stateChanged(ChangeEvent e) {
Object source = e.getSource();
updateMnemonicProperties();
if (isEnabled() != model.isEnabled()) {
setEnabled(model.isEnabled());
}
fireStateChanged();
repaint();
}
So repaint is called when the mouse enters a JButton.
..a jbutton changes color on mousehover even though repaint() is never called.
Sure it is. And this code is evidence of that. Of course, it is not evidence on a Kindle Fire that most probably has no JRE, but then, a Kindle Fire is an entirely inappropriate tool to be using to communicate to a Q&A site while discussing technical points of a programming language that does not run on the device.
import javax.swing.*;
public class ButtonRepaint {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
JButton b = new JButton("Hover Over Me!") {
#Override
public void repaint() {
super.repaint();
System.out.println("Repaint");
}
};
JOptionPane.showMessageDialog(null, b);
}
};
SwingUtilities.invokeLater(r);
}
}
Note that the paint() method that gets called belongs to the button's UI delegate, typically derived from BasicButtonUI. There's an example here using MetalButtonUI.

JList MouseMoved and MousePressed

I've extended a JList to provide two separate functionalities, toolTipText for items, and right-click options. Both work separately, but when I try to use them together, the MouseMoved events aren't being recognized? Below are the guts of my new listener methods. How should I be negotiating these various events?
public class JListTT extends javax.swing.JList {
public JListTT() {
super();
addMouseListener(new ttListener());
...
class ttListener extends MouseAdapter {
public void mouseMoved(MouseEvent e) {
String nodeID = bldItemNodeID();
theList.setToolTipText(nodeID);
}
public void mousePressed(MouseEvent ev) {check(ev); }
public void mouseReleased(MouseEvent ev) {check(ev); }
public void mouseClicked(MouseEvent ev) {check(ev); }
public void check(MouseEvent ev) {
if (ev.isPopupTrigger()) {
theList.setSelectedIndex(theList.locationToIndex(ev.getPoint()));
menu.show(theList, ev.getX(), ev.getY());
}
}
}
You add the ttListener object as a MouseListener, but I don't see you adding the ttListener object as a MouseMotionListener. For example:
ttListener myMouseadapter = new ttListener();
addMouseListener(myMouseadapter);
addMouseMotionListener(myMouseadapter);
I did not test this myself, but looking at the javadoc of JList the tooltip functionality is available out of the box. The javadoc of JList#getTooltipText clearly states
Overrides JComponent's getToolTipText method in order to allow the
renderer's tips to be used if it has text set.
So if your ListCellRenderer returns a Component in the getListCellRendererComponent method which has a tooltip it will be displayed by the JList without the need of a listener.
there's not necessarily a need for a low-level approach as a custom mouse-/motionListener:
as to a per-cell tooltip, see #Robin's answer
as to a context menu, JComonent has a property componentPopupMenu: using that will cope with opening the menu on keyboard short-cut automatically
"not necessarily" because you seem to rely on the cell being selected on right click. If so, you would still need a MouseListener to trigger the selection (after decade long debates, Swing doesn't - which seems to be unusual in current native apps ;-)
You can achieve it by using mouseDragged
YourClass extends JPanel implements MouseListener{
......
#Override
public void mouseDragged(MouseEvent e) {
//code go here
}
}

Swing ComponentListener.componentShown is not fired

I would like to get the (Graphics g) when the WorldPanel is shown. Thanks to stackoverflow I saw the answer where should I use the getGraphics method (ComponentListener.componentShown), but somehow my listener couldn't catch the componentShown.
What did I wrong ? At the bottom of the code snippet the "hello" can't not be displayed.
public class MainPanel extends javax.swing.JPanel implements ComponentListener {
private CWorldPanel WorldPanel; // extends JPanel
private void initGUI() {
try {
...
this.setLayout(thisLayout);
{
WorldPanel = new CWorldPanel();
WorldPanel.addComponentListener(this);
...
#Override
public void componentShown(ComponentEvent e) {
System.out.println("hello");
}
See my explanation and thanks at the bottom of the page (cscsaba)
If you want to get the Graphics object in order to paint on your component as soon as it is visible, don't. Simply override the paintComponent method of the object to be shown, and paint in there.
(I have no idea why your ComponentListener does not work, and can't try since your example is incomplete.)

Categories

Resources