swing

Remove last character/line

Hello, I have a snippet of code that prints text from a file to a JTextArea called textArea. Unfortunately the method I'm using goes line by line (not ideal) and so I have to append each line with a \n This is fine for now but a new line is created at the end. The code I have is as follows: class menuOpen implements ActionListener {...

What are your best Swing design patterns and tips?

I'm writing a GUI for an application using Swing, and in the interests of code maintenance and readability, I want to follow a consistent pattern throughout the whole system. Most of the articles and books (or at least book sections) that I've read appear to provide plenty of examples on how to create and arrange various components, but...

Return to a specific dialog

I'm writing a very simple text editor and have run into a somewhat minor problem. Code below Saving a file, when a file exists, the user will be prompted to overwrite, cancel, or not overwrite (having the option to try again). So I have a JFileChooser that will prompt the user to overwrite: yes, no, cancel In the case of no being sele...

Swing JLayeredPane.getLayer() - Documentation error or actual side effect?

I'm trying to figure something out about JLayeredPane in Swing. If anyone has used this class, feedback would be appreciated. The documentation for getLayer(JComponent c) states: Gets the layer property for a JComponent, it does not cause any side effects like setLayer(). (painting, add/remove, etc) Normally you should use t...

BufferedImage's getSubimage performance

I'm working on a Java 2D rendering program (running on 1.6.0_11), which uses external images for its UI rendering. These large images contain several UI graphics parts at the same time, which I extract now with the BufferedImage.getSubimage(). Assuming an average desktop system (with our without enabling DirectX/OpenGL acceleration), my ...

Java Paint Method Doesn't Paint?

I'm working on a simple little swing component, and I'm tearing out my hair trying to figure out why my paint method isn't working. The idea behind this component is that it's a small JPanel with a label. The background (behind the label) is supposed to be white, with a colored rectangle on the left-hand side indicating the ratio of two...

Is it possible to have a MouseMotionListener listen to all system mouse motion events?

My boilerplate listener: class MyMouseMotionListener implements MouseMotionListener { public void mouseDragged(MouseEvent e) { System.out.println("Dragged..."); } public void mouseMoved(MouseEvent e) { System.out.println("Moved..."); }} Simple enough, but what do I add it to in order to listen to system-wide events? I've been resea...

Fastest way to create a Java message dialog (swing/awt/other)?

I'm creating a Java application that will do some processing then needs to display a message to give the user feedback. However, it appears to be incredibly slow - taking over two seconds to return. I stripped the source down to the apparent culprit, and here is the code used: package SwingPlay; import javax.swing.JFrame; public cla...

can't run swing from the command line.

i use the command line in windows to compile and then execute my java programs. i've gone to http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html and tried compiling the HelloWorldSwing.java class. it worked, but when i try "java HelloWorldSwing" it gives me a bunch of erros and says something along the lines of Exception i...

Why do we need JavaFX though we have Swing?

Swing is good in many ways, then why do we need JavaFX? ...

Progress Dialog in Swing

How can I make a modal JDialog without buttons appear for the duration it takes a Runnable instance to complete and have that instance update a progress bar/message on that Dialog. Clearly spaghetti code might be this working, but I'm looking for a clean design if one exists. Thanks. ...

How do I disable the Cancel Button when using javax.swing.ProgressMonitor?

I'd like to make use of ProgressMonitor's functionality but I don't want to give the user the option of canceling the action. I know if I don't call isCanceled() then the button press has no effect, but I'd prefer not to have the user believe the program is unresponsive. How shall I go about doing this? ...

Good source for AWT and Swing principles

Hi. I am starting to work with AWT and Swing in Java. I get some trouble sometimes, with things I don't understand. Latest example: JPanel.getGraphics() returns null in some cases. I would like a source where I can learn the principles and design of these API's, instead of just solving each problem at a time. Any recommendations? Tha...

Override default behavior of TAB in JTextPane

I am implementing a JTextPane-based text editor. Currently, when I have a piece of selected text, pressing the TAB key deletes the selected text. I would like to change this behavior such that TAB will indent the selected text. How to go about it? ...

Java: Image as toggle button

Hi guys, How can I create in a Swing interface a toggle image button? I have two images, imageon.jpg and imageoff.jpg,and I basically want a clickable element that toggles the images and fires an event. Update: Is there a way to override the usual 'chrome' around the image? I would prefer a simple image to a button with an image inside...

Find the classname of a UI control

Hi, I need a tool which tells the classname of the UI control on which my mouse pointer is. My GUI is in Swing. Does such a tool exist? ...

JSlider question: Position after leftclick

Whenever I click a JSlider it gets positioned one majorTick in the direction of the click instead of jumping to the spot I actually click. (If slider is at point 47 and I click 5 it'll jump to 37 instead of 5). Is there any way to change this while using JSliders, or do I have to use another datastructure? ...

Applying the MVP pattern to JDialogs

I'm writing a Swing application, and further to my previous question, have settled on using the Model-View-Presenter pattern to separate the user interface from the business logic. When my application starts up, it executes the following code: Model model = new BasicModel(); Presenter presenter = new Presenter(model); View view = new S...

Swing Reuse

I'm writing a Java Desktop App using NetBeans 6.5 and a Swing Patterns that keeps repeating. Each and every one of my dialogs has a "Save Button" and "Close Button" with different mostly different behaviour behind the Save Button and identical code behind the close button. How can I reuse this code without copying and pasting and at th...

Event handling with Jython & Swing

I'm making a GUI by using Swing from Jython. Event handling seems to be particularly elegant from Jython, just set JButton("Push me", actionPerformed = nameOfFunctionToCall) However, trying same thing inside a class gets difficult. Naively trying JButton("Push me", actionPerformed = nameOfMethodToCall) or JButton("Push me", action...