Setting background color for a JFrame
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color. Example: myJFrame.getContentPane().setBackground( desiredColor );
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color. Example: myJFrame.getContentPane().setBackground( desiredColor );
As other answers have said, it executes your Runnable on the AWT event-dispatching thread. But why would you want to do that? Because the Swing data structures aren’t thread-safe, so to provide programmers with an easily-achievable way of preventing concurrent access to them, the Swing designers laid down the rule that all code that accesses … Read more
So basically what I need is to get new window dimension into the move() method in the Rect class. Don’t know if it is the best design but I pass the “panel” as a parameter to the “move()” method so its width/height can be used. Here is some old code I have lying around that … Read more
N.B. There is a clipping bug in this code, which is fixed in the accepted answer to paintComponent() is drawing on other components. This should only be considered as a solution if the ‘clipping bug fix’ is incorporated. // Paint the BG color of the parent, everywhere outside the clip // of the text bubble. … Read more
That’s a very difficult question to answer with Swing, as Swing is not a pure MVC implementation, the view and controller are mixed. Technically, a model and controller should be able to interact and the controller and view should be able to interact, but the view and model should never interact, which clearly isn’t how … Read more
I’ve had some luck extending JLabel and implementing Icon to get a translucent component working the way I want. You can see the result of various rule combinations in this AlphaCompositeDemo. The example below is 100% white atop 50% black. Addendum: Note how this example composites opaque text on a clear offscreen background over the … Read more
Do I have to use each time I need to update the GUI components? No, not if you’re already on the event dispatch thread (EDT) which is always the case when responding to user initiated events such as clicks and selections. (The actionPerformed methods etc, are always called by the EDT.) If you’re not on … Read more
The event dispatch thread is a special thread that is managed by AWT. Basically, it is a thread that runs in an infinite loop, processing events. The java.awt.EventQueue.invokeLater and javax.swing.SwingUtilities.invokeLater methods are a way to provide code that will run on the event queue. Writing a UI framework that is safe in a multithreading environment … Read more
From JButtons JButton#setName(String); JBUtton#setActionCommand(String); JBUtton#setAction(Action); from/to Container SwingUtilities#convert… SwingUtilities#getDeepestComponentAt
I would tend to have each button for the numbers, as well as each button for operands, add text to a text field that is the ‘Input/Output’. Also have a button = (equals). When the = button is activated, call the ScriptEngine to evaluate the content of the I/O text field and write the result … Read more