天道酬勤,学无止境

swing

SwingWorker 的原理是什么?(What is the rationale of SwingWorker?)

问题 据我所知,它用于在 Swing 应用程序中调度一个新线程来执行一些“后台”工作,但是使用它而不是“普通”线程有什么好处? 使用新线程是不一样的,当它完成时使用 SwingUtilities.invokeLater 调用一些 GUI 方法吗?... 我在这里缺少什么? http://en.wikipedia.org/wiki/SwingWorker http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html 回答1 是的,您可以使用普通线程 + invokeLater 完成 SwingWorker 所做的事情。 SwingWorker 提供了一种可预测的集成方式来在后台线程上完成任务并在 EDT 上报告结果。 SwingWorker 还增加了对中间结果的支持。 同样,您可以自己完成所有这些工作,但有时使用集成且可预测的解决方案很容易,尤其是在并发性方面。 回答2 一个代码示例: import org.jdesktop.swingx.util.SwingWorker; // This one is from swingx // another one is built in // since JDK 1.6 AFAIK? public class SwingWorkerTest { public

2021-06-21 12:30:11    分类:技术分享    java   multithreading   swing   swingworker

JavaFX 和 SwingNode - 部分黑色窗口(JavaFX and SwingNode - partial black Window)

问题 我尝试将 JavaFX 中的 Swing 组件与 SwingNode 一起使用: public class MyTest extends Application { @Override public void start(Stage stage) { final SwingNode swingNode = new SwingNode(); FlowPane pane = new FlowPane(); Button btn = new Button("1"); btn.setVisible(false); pane.getChildren().add(btn); createAndSetSwingContent(swingNode); pane.getChildren().add(swingNode); stage.setScene(new Scene(pane, 100, 50)); stage.show(); btn.setVisible(true); } private void createAndSetSwingContent(final SwingNode swingNode) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { swingNode

2021-06-21 12:27:40    分类:技术分享    java   swing   javafx-8

Can I modify JComboBox popup background color of an existing object?

I have an existing JComboBox object. I can modify many of its properties using the internal methods. However, I could not find similar methods to customize the popup's appearance - specifically, the popup's background color. I have an existing object, so I wish to use its existing methods/properties, not to write a dedicated class. Is this possible? Note: this question is NOT the same as the linked question above (which incorrectly states that this question already has an answer): that question asked about the selected item's bgcolor (in the combobox's editbox); I am asking about the popup box

2021-06-21 12:23:44    分类:问答    java   swing   popup   jcombobox

JEditorPane inside JScrollPane not resizing as needed

I am implementing a Comment box facility in my application which user can resize using mouse. This comment box contains a scrollpane which instead contains a JEditorPane in which user can insert comment. I have added the editor pane inside a scroll pane for the following reason: auto scolling of jeditorpane When the user resizes the comment box, I am setting the desired size for JScrollPane and the JEditorPane. When the user is increasing the size of the comment box, the size of these components are increasing as desired but when the size of the comment box is decreased, the size of the

2021-06-21 12:22:54    分类:问答    java   swing   jscrollpane   resize   jeditorpane

Java JTextArea 动态列和行编号(Java JTextArea dynamic column and row numbering)

问题 我有一个快速的谷歌,我似乎无法找到一个好的解决方案,主要是因为我不知道如何描述它。 本质上,我需要在JTextArea显示任意数量的十六进制字符,我希望它们均匀分布,并且字符的位置显示在字符的顶部和左侧。 这是我想要实现的示例,这是十六进制查看器 WinHex。 我一直在尝试将字节数组转换为字符串,然后对其进行文本包装,但我得到了一些奇怪的结果。 任何有关如何实现类似目标的建议将不胜感激。 我考虑过的另一个选择是使用 JTable,但我想知道这是否会使问题稍微复杂化。 也许。 谢谢 回答1 这应该让您开始,使用AbstractTableModel一个非常简单的实现。 我只用了 15 分钟就写完了(作为对“使问题过于复杂”的回应)。 import java.awt.BorderLayout; import java.awt.EventQueue; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; public class

2021-06-21 12:20:27    分类:技术分享    java   string   swing   hex   jtextarea

Swing BoxLayout problem with JComboBox without using setXXXSize

here's an SSCCE: import java.awt.Color; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class BoxLayoutTest extends JFrame { public BoxLayoutTest(){ JPanel main = new JPanel(); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); main.setBackground(Color.red); this.add(main); JPanel northPanel = new JPanel(); JPanel middle = new JPanel(); middle.setLayout(new BoxLayout(middle, BoxLayout.X_AXIS)); middle.add(new JButton("FOO")); middle.add(Box

2021-06-21 12:13:12    分类:问答    java   swing   jcombobox   layout-manager   boxlayout

Swing: GlassPane prevents mouse pointer from changing

I have a JTabbedPane with some tabs and a lot of unused extra space next to the tabs. So I'm trying to use it and place some buttons there (like in Eclipse). I put the buttons on a GlassPane: JPanel glasspane = getPanelWithButtons(); // panel with FlowLayout.RIGHT frame.setGlassPane(glasspane); glasspane.setOpaque(false); glasspane.setVisible(true); This works, and I still can click through on the other elements of my gui (most search results I found are about how to prevent this). The only problem so far is that the mouse pointer doesn't change to that double-ended horizontal arrow when it

2021-06-21 11:59:21    分类:问答    java   swing   pointers   mouse   glasspane

与 Java 6 相比,Java 8 的 GUI 性能较差(Java 8 poor GUI performance compared to Java 6)

问题 在 Java 6 下面的代码按预期运行,但在 Java 8 中需要更多时间。 有趣的部分是组件使用相同的方法setEnable()来启用和禁用组件,但是禁用调用比启用调用花费的时间长得多,几乎是启用调用的两倍。 在 Java 8 中禁用比在 Java 1.6 中花费的时间要长得多。 问题是为什么会发生这种情况? 这是 Java 8 的性能问题吗? 以下是 Java 6 的结果: Sun Microsystems Inc. 1.6.0_45 Initializing GUI GUI initialized in 1105 ms Disabling Disabled in 687 ms Enabling Enabled in 375 ms 以下是 Java 8 的结果: Oracle Corporation 1.8.0_25 Initializing GUI GUI initialized in 604 ms Disabling Disabled in 6341 ms Enabling Enabled in 370 ms 编码: import java.awt.Component; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import

2021-06-21 11:50:58    分类:技术分享    java   performance   swing   user-interface   java-8

How to draw thin line with no gap while dragging the cursor?

I have this following class, which refresh a jpeg file in layer 0 and layer 1 is used to draw/paint/sketch up anything related to smash things. But in my drawing when I want to do a thin line, it breaks. Because the mouse cursor movement needs to be slower. How to resolve on fast mouse move, that the line remains joined? Annotation.java package test; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel

2021-06-21 11:50:17    分类:问答    java   image   swing   graphics2d

Moving JScrollPane horizontally results in blured text

I have a TextArea inside JScrollPane inside standard JPanel. JPanel panelMain = new JPanel(); panelMain.setBorder(titledBorder1); panelMain.setBounds(new Rectangle(2, 5, 970, 700)); panelMain.setLayout(null); JTextArea fieldBody = new JTextArea(); JScrollPane fieldBodyScrollPane = new JScrollPane(fieldBody); fieldBodyScrollPane.setBounds(70, 140, 790, 500); panelMain.add(fieldBodyScrollPane); When I type enough text in a single row the horizontal knob appears - so far good. But when I start moving the knob left and right, the text gets blured (see image). Interestingly, nothing weird happens

2021-06-21 11:20:29    分类:问答    java   swing   jscrollpane   ubuntu-12.04   visual-artifacts