jframe

How to count number of JCheckboxes checked?

痴心易碎 提交于 2019-11-28 08:49:23
问题 I have 11 different checkboxes in my JFrame and want to be able to get a number whenever one is checked for how many total are checked. I know how to set up an ItemListener and see if one is checked, but I am not sure how I could check all of them.. EDIT: cblist is an ArrayList containing 11 JCheckBoxes. I gave every JCheckBox an item listener and hereis the class used when the checkboxes are clicked... private class CheckClass implements ItemListener{ public void itemStateChanged(ItemEvent

Working with 2 or more frames

主宰稳场 提交于 2019-11-28 08:49:18
I have about 3 frames in my java swing application. What it the correct way how to handle with these frames? I mean some pattern or something else. Now I have always one class which represent frame and one class for panel which is main in this frame. Now I have defined frames as static variable and when I wanna hide them I call classname.frameName.setVisible(false); is this the correct solution? Andrew Thompson Besides the (excellent) suggestions of a CardLayout or JFrame with multiple JDialog instances, here are some other strategies which might work singly or in combination, to collapse a

How to set an icon to a JFrame when using createAndShowGUI() method?

风格不统一 提交于 2019-11-28 08:49:14
问题 I am using a createAndShowGUI() method to create a JFrame . I am trying to set an icon, but when I run it in NetBeans, it doesn't show. However, when I run a .jar file (with the image in the same folder), then it works without a flaw. private static void createAndShowGUI() { //Create and set up the window. JFrame game = new JFrame(); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setSize(198, 409); game.setResizable(false); game.setTitle("Frame Title"); ImageIcon img = new

Get Any/All Active JFrames in Java Application?

放肆的年华 提交于 2019-11-28 08:21:47
Is there a way from within a Java application to list all of the currently open/active (I'm not sure the terminology here) JFrames that are visible on screen? Thanks for your help. Frame.getFrames() returns an array of all frames. Alternately as mentioned by @mKorbel, Window.getWindows() will return all windows - since Frame (& JFrame ) extend Window that will provide all frames, and then some. It will be necessary to iterate them to discover which ones are currently visible. mike rodent I agree with Stefan Reich's comment. A very useful method is Window.getOwnedWindows() ... and one context

MouseListener on JFrame

我们两清 提交于 2019-11-28 08:06:45
问题 I want to be notified of mouse events (specifically the mouse entered and exited events) on my JFrame. But when i add a mouselistener to it i get the events on the borders of the frame not the entire frame with it's contents. Any ideas as to why? EDIT : Or at least do you have an alternative? I want a "gloabal" way to catch mouse events on the JFrame. Maybe a mouselistener is not the answer. 回答1: You can get all events and check if their source is a component in the JFrame. See Toolkit

Remove default JFrame icon

ⅰ亾dé卋堺 提交于 2019-11-28 07:41:33
问题 In my JFrame i have the default coffee icon. I want to remove it. But when i do setIconImage(null) it does't work. Can anyone tell me the solution as to how to completely remove the icon 回答1: Create icon that consists of one pixel (better transparent) and use it. If you need such icon contact me. I will send you. 回答2: It's always good to keep a copy of the Java source code around. The code for java.awt.Window (a superclass of JFrame) has the following code for setIconImage : public void

Swing: Setting a function key (F2) as an accelerator

廉价感情. 提交于 2019-11-28 07:36:38
问题 I have a menu item, "rename", for which F2 is set as an accelerator. Indeed when the menu is displayed there a little "F2" indication next to "rename". Sadly, this does not work. This accelerator triggers no response. When I change the accelerator to CTRL+F2 - it works. It seems that I should use an InpoutMpa/ActionMap. The problem with that is that I want this to work everywhere in the app so I am trying to associate it with the top-level JFrame. But, JFrame does not have a getInputMap()

How to remove all components from a JFrame in Java?

自闭症网瘾萝莉.ら 提交于 2019-11-28 06:48:34
I'm writing a program where I have a JFrame and I want to remove all components from it, then add just one component to it and repaint the frame. What I have so far is something like the code below (called in an object that implements JFrame, where StartPanel implements JPanel): removeAll(); startPanel = new StartPanel(); startPanel.setVisible(true); add(startPanel); revalidate(); repaint(); However, when I run the code it shows an empty window (not the startPanel) and when I minimize/resize the window, the window turns black. If I leave out the removeAll() and there are not elements already

Change contents of BufferedImage, then update JFrame to reflect it

牧云@^-^@ 提交于 2019-11-28 06:37:32
问题 I am trying to make a Mandelbrot Set renderer with a GUI where you can click and drag to zoom into a specific area. When run, it will do the initial calculations and rendering fine, but when you try to click and drag to zoom in, the console says it is doing the calculations, but the content of the JFrame is not updated. However, I'm not even positive that it is recalculating, because the initial calculation takes about 8 seconds but when you click/drag to zoom it takes about 6 ms. I have

How to change the dimension of a component in a JFrame

这一生的挚爱 提交于 2019-11-28 05:54:37
问题 Suppose I have a JPanel in a JFrame. When I invoke a method that changes the preferred size of that JPanel, it does not change. The code looks something like this: public class SomePanel extends JPanel{ public SomePanel(){ setPreferredSize( new Dimension( 390, 40 ) ); setBackground( Color.BLACK ); } public void expand(){ setPreferredSize( new Dimension( 390, 200 ) ); } public static void main( String args[] ){ JFrame frame = new JFrame(); frame.setSize( 450, 500 ); frame.setLayout( new