swing

Java: GUIs must be initialized in the EDT thread?

Hi, I'm Jason. I'm having a bit of a problem with the Substance look and feel (https://substance.dev.java.net/). My problem is more general. I've already got my GUI written and it works fine, but when I use a Substance Look-and-feel, it requires all GUI initialization to take place in the EDT thread (Event Dispatching Thread or somethin...

Detecting keyboard direction keys using keyPressed

G'day all, After learning on here that the Java console doesn't support keyboard input a great deal, I switched to Swing and AWT. My problem now is how to detect when the keyboard direction keys (non-numeric keypad) are used. My code (below) does not do more than print a "small box" character when pressing the down, up, left, right key...

Difficulty removing all components from a Jpanel

G'day all, I am coding a main menu for a project. The menu displays properly. I have also set up ActionListeners for the three buttons on the menu. What I wish to do is reuse the JPanel for a new set of radio buttons when the user chooses "Start a New Game". However, coding ActionPerformed to remove the existing components from the...

Why does JDialog 'jump' on setLocationRelativeTo()?

Have a class that extends JDialog. It's initialized entirely on EDT (just in case). Now, public PropsDialog (JFrame parentFrame) { super(parentFrame); // boring pack(); setLocationRelativeTo(getParent()); setVisible(true); results in dialog appearing in the leftmost corner and then jumping to it's intended position...

JTextPane keeps throwing BadLocation

I have a JFrame that contains a JTextPane. The purpose of this JTextPane is to highlight words as I type them, something along the lines of a programmer's text editor. To accomplish this, I extended JTextPane, I implemented the KeyListener interface, and I had it setup as a key listener to self. The method that does some important work i...

Constructing a DataFlavor for drag and drop of an array of java objects

I want to implement drag and drop between two components within the same JVM. I'm passing an array of objects which are not serializable, so I'm trying to find the correct incantation of javaJVMLocalObjectMimeType to pass in. However, I keep getting an illegal argument exception. As an example, if i have ExampleClass Appending class p...

How can I make it so that a JPanel updates itself once the window scrolls?

I'm new to Java so this is probably pretty simple, but I'm not sure how to make it work. Basically, I have a class which I use to draw a grid, which extends JPanel. I put this class in a window, inside a JScrollPane. The scroll works the way I want it to, except the drawing inside my class doesn't update itself once the window scrolls. ...

Calling this from inside a nested Java ActionListener.

Suppose I have this: class external { JFrame myFrame; ... class internal implements ActionListener { public void actionPerformed(ActionEvent e) { ... myFrame.setContentPane(this.createContentPane()); } } ... } createContentPane returns a Container. Now, if I wa...

What event would I need to be able to tell that the mouse was moved over a JMenuItem?

I'd like to handle the case where the mouse goes over any of the JMenuItems. I don't need the user to click one of the JMenuItems; simply to move the mouse over any of them. Basically, he has to click the JMenuBar to show the menus, but he doesn't have to click any menu item. I wanted to use an actionPerformed listener on the JMenuBar -...

In Java, is there a way to obtain the component where an event is being handled?

Suppose I have 4 squares colored blue, white, red and green (myComponent) associated with the mouse press event. At one point, the mouse is pressed over one of them - say, the yellow one - and the event is activated. Now, the control flux is inside the event handling function. How do I get the MyComponent - the yellow square - that caus...

Java newbie: Swing and displaying ASCII files

G'day all, I have an application which needs to display an ASCII file of 15 lines in a Swing component that cannot be edited by the user. Below is my code which reads the ASCII file byte by byte. My thanks to the helpful commenters who explained that a JTextArea.setEditable(false) would be appropriate in Swing. However, my code mer...

How to get some interactivity with a JTable.

Hi, I Have a JTable where the data model contains information from a sql query. Want to get the added ability to take me to a new jpanel by double-clicking a row in the jtabel. Thnx ...

Why do my Swing application windows intermittently not close when the application exits?

Why are my Swing applications not closing when using Netbeans 6.5 on Ubuntu 8.10 with Sun JDK 6. Even the Netbeans tutorials downloaded from the Netbeans website do the same. When I close the application I'm left with a dead window. I then have to manually kill the process. It comes and goes, but I can't figure out what triggers this beh...

How can I determine which menu item called an ActionListener?

I have a Java program where I have a JMenu with an arbitrary number of items (in this case, there is one menu item for each dynamic library currently loaded in a different program). I'm running a loop to add JCheckBoxMenuItem s to the menu, since I don't know how many there will be. How can I set up an action listener for these menu ite...

Java/Swing stack overflow in preferenceChanged method

I'm currently working on a Java project and was having a problem with a stack overflow error. What happens is first the program reads in a file of about 1,500,000 words and adds it to an array. It then reads in a small file of about 600 words and adds it to an array. It checks how many words in the 600 word file occur in the other file. ...

Getting the removed text from a Java text component

A JTextComponent allows you to add a DocumentListener to its Document, which will tell you when text was added or removed, and the offset and length of the change. It will not, however, tell you what the text of the change was. This is not much of a problem for text addition, as you can use the offset and the length to find the added t...

Swing GUI : Scrolling not updated when a JTable gets bigger

Hi, I got a Java Swing GUI and got a problem with a JTable in a JScrollPane. For some reason, when the rows of the table model are increased during the program execution, the JScrollPane isn't updated - that is, if the rows are increased so that the height of the table is over the height of the scroll view, the scroll panes aren't updat...

How to make JTextField expand with a BorderLayout (Java)

I have a Java program, in which, I'm using a JTextField, but if i don't specify a default size, it'll have the width 0. I'm inserting it in a BorderLayout, so how do I make it expand to fill the whole container? ...

What am I doing wrong in Java to get this IllegalArgumentException?

I occasionally get an exception in a JTextArea that I'm updating with JTextArea.append() ... java.lang.IllegalArgumentException: Invalid remove at javax.swing.JTextArea.replaceRange(Unknown Source) at sun.plugin.ConsoleWindow$24.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.E...

adjust selected File to FileFilter in a JFileChooser

I'm writing a diagram editor in java. This app has the option to export to various standard image formats such as .jpg, .png etc. When the user clicks File->Export, you get a JFileChooser which has a number of FileFilters in it, for .jpg, .png etc. Now here is my question: Is there a way to have the extension of the default adjust to t...