swing

Java Thread Start-Stop-Start on same button click

巧了我就是萌 提交于 2021-02-07 10:33:40
问题 I am creating a simple java program with a GUI built with the help of window builder. The GUI consists of just a button. On button click,start a thread that will print to the random number infinitely until it is stopped by clicking the same button again. Here is my code LoopTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoopTest extends JFrame implements ActionListener {//****** private JButton startB, stopB; private JTextArea oa; Start sta; public

How to limit selection to a maximum of two out of three radio buttons in Swing?

99封情书 提交于 2021-02-07 10:31:27
问题 Adding radio buttons to a button group limits the user to selecting any one radio button (or none). Is there a way of limiting the user to selecting up to n radio buttons, where n is more than 1 but less than the total number of buttons? In my current case, I am converting a 2D point on a drawing into the theoretical 3D space that the 2D drawing describes. Any one 2D point might represent multiple points in 3D space, so the user has the option to constrain the 3D point by specifying the value

How to limit selection to a maximum of two out of three radio buttons in Swing?

人走茶凉 提交于 2021-02-07 10:31:17
问题 Adding radio buttons to a button group limits the user to selecting any one radio button (or none). Is there a way of limiting the user to selecting up to n radio buttons, where n is more than 1 but less than the total number of buttons? In my current case, I am converting a 2D point on a drawing into the theoretical 3D space that the 2D drawing describes. Any one 2D point might represent multiple points in 3D space, so the user has the option to constrain the 3D point by specifying the value

JFilechooser appearance

荒凉一梦 提交于 2021-02-07 09:14:53
问题 In my swing application, I have set the UI Look and Feel as: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); And it works well on Windows. Inside, the application, the user has to select files using the JFileChooser . The JFileChooser appearance on windows is again the native one. But not on Mac. The screenshot of the JFileChooser Panel: But instead, I prefer something like this: (This one is taken from upload option in gmail) What should I change the UIManager to or

FileTreeModel Multiple Roots

依然范特西╮ 提交于 2021-02-07 08:57:20
问题 I use this code here to create a file tree model. I want it to browse files. How can I add multiple roots? E.g. list C:/, D:/, E:/. 回答1: A TreeModel can only have one root node, but you can use JTree.setRootVisible() to hide the root node when displaying the tree. Modify your model to have a virtual root node that contains each filesystem root (C:\, D:\, E:\, etc.), and call JTree.setRootVisible(false) on your JTree. 来源: https://stackoverflow.com/questions/10501410/filetreemodel-multiple

Swing refresh cycle

一个人想着一个人 提交于 2021-02-07 07:45:17
问题 I'm trying to understand when to use revalidate/repaint/pack. Surprisingly I haven't found much detailed under-the-hood documentation (feel free to link). So far I have understood that this is all the responsibility of the RepaintManager. paint/repaint refer to what sees as dirty/clean pack/validate/revalidate refer to what is valid This article on Oracle explains that calling a repaint enqueues a job on the Event Dispatcher Thread that will in turn call paintImmediately() on the component

Draw Graphics2D to another Graphics2D

…衆ロ難τιáo~ 提交于 2021-02-07 07:26:20
问题 It is possible to draw from one Graphics2D to another Graphics2D ? Let me explain. I have printing issues, when i display a JTextArea or JTextPanel in screen, internaly its used sun.java2d.SunGraphics2D , but when im printing its used sun.print.PeekGraphics and sun.awt.windows.WPathGraphics . The problem is with some kind of Fonts, like Arial. In some sizes lines are cut. I have tryed a lot of ways to render the text in printing, Graphics2D.drawString , SwingUtilities2.drawString , TextLayout

How to get the top level container of a JComponent?

扶醉桌前 提交于 2021-02-07 06:41:02
问题 Is there a way to get the top level container of a component? For example I have a JToolbar and I want to know at one monent the top level container of that JToolbar is my JFrame or is its own window, a JDialog. 回答1: SwingUtilities.windowForComponent(...); 回答2: If the component has been added to the hierarchy, you can look up the top-level container by recursively calling getParent : Container c = toolbar; while ( c.getParent() != null ) { c = c.getParent(); } if ( c instanceof JFrame ) { //.

Is there an elegant way to make JTable stop editing when user closes the application window?

南楼画角 提交于 2021-02-07 04:18:51
问题 I am trying to find out an elegant way to make JTable stop cell editing (cancel it actually) when user closes the main application window. I know something like this can be done using the WindowAdapter but for this to work properly I need a reference to the window. Problem is I sometimes do not have it. 回答1: An abrupt exit may need to be handled at several levels. Assuming that the user must navigate out of the table to click on an Exit control, you should get the desired result by setting

Disable default ALT key action in JFrame under Windows

て烟熏妆下的殇ゞ 提交于 2021-02-07 03:36:21
问题 I would like to let my JFrame under Windows not act upon an ALT key press. To clarify, when you execute the following snippet of code: import javax.swing.*; public class FrameTest { public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } and press the ALT key and then the arrow down key, you get a menu in the upper left corner in which you can choose to