jframe

How to capture a JFrame's close button click event?

痴心易碎 提交于 2019-11-26 15:58:40
I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked. How can I capture that event? I'd also like to prevent the window from closing if the user chooses not to proceed. Ravindra Gullapalli import javax.swing.JOptionPane; import javax.swing.JFrame; /*Some piece of code*/ frame.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent windowEvent) { if (JOptionPane.showConfirmDialog(frame, "Are you sure you want to close this window?", "Close Window?", JOptionPane.YES_NO_OPTION,

JPanel vs JFrame in Java

落爺英雄遲暮 提交于 2019-11-26 15:31:56
问题 I am learning Java gui. The way I learnt to create a window is to inherit or Extend JFrame class and it is good to use it, as JFrame contains all the properties of a Window. Now If I want to add something to this window, I need to use add() method. But Today I came across JPanel which also creates a windows and we can add stuff by jpanelObjec.add() . What is the difference between the two methods? Are they somehow related? 回答1: You should not extend the JFrame class unnecessarily (only if you

FocusEvent doesn't get the last value of JFormattedTextField, How I can get it?

一曲冷凌霜 提交于 2019-11-26 14:43:46
问题 I have two JFormattedTextField objects on my JFrame object. I want a basic Math (addition) by the values of these JFormattedTextField objects. I want it happen when focus lost either the first or the second textfield. But when " focusLost() ", event doesn't get the last value, it gets the previous value. For example; tf1 has 0 and tf2 has 0 at first. I write 2 to tf1 , and when focusLost() , result ( tf1+tf2 ) become still 0. when I change any of them, the result becomes 2 (the previous value

Running a JFrame with a JProgressBar

让人想犯罪 __ 提交于 2019-11-26 14:39:30
问题 public void myMethod { MyProgessBarFrame progFrame = new MyProgressBarFrame(); // this is a JFrame progFrame.setVisible(true); // show my JFrame loading // do some processing here while the progress bar is running // ..... progFrame.setvisible(false); // hide my progress bar JFrame } // end method myMethod I have the code above. But when I run it, the do some processing section does not process until I close the progress bar JFrame. How will I show my progress bar and tell Java to continue in

Get a Swing component by name

馋奶兔 提交于 2019-11-26 14:38:11
I have in a JFrame some components that I want to refer into another JFrame and I want to get them by name and not do public get/set methods for each. Is there a way from Swing to get a component reference by its name like do c#? e.g. form.Controls["text"] Thanks I know this is an old question, but I found myself asking it just now. I wanted an easy way to get components by name so I didn't have to write some convoluted code each time to access different components. For example, having a JButton access the text in a text field or a selection in a List. The easiest solution is to make all of

Limiting the number of characters in a JTextField

送分小仙女□ 提交于 2019-11-26 13:05:37
I want to set the maximum length of a JTextField , so that you can't enter more characters than the limit. This is the code I have so far... textField = new JTextField(); textField.setBounds(40, 39, 105, 20); contentPane.add(textField); textField.setColumns(10); Is there any simple way to put a limit on the number of characters? npinti You can do something like this (taken from here ): import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax

Show JFrame in a specific screen in dual monitor configuration

余生颓废 提交于 2019-11-26 13:02:21
I have a dual monitor config and I want to run my GUI in a specific monitor if it is found. I tried to create my JFrame window passing a GraphicConfiguration object of my screen device, but it doesn't work, frame still display on the main screen. How can I set the screen where my frame must be displayed? public static void showOnScreen( int screen, JFrame frame ) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); if( screen > -1 && screen < gs.length ) { gs[screen].setFullScreenWindow( frame ); } else if( gs.length > 0 )

What is the relation between ContentPane and JPanel?

半腔热情 提交于 2019-11-26 12:56:47
问题 I found one example in which buttons are added to panels (instances of JPanel ) then panels are added to the the containers (instances generated by getContentPane() ) and then containers are, by the construction, included into the JFrame (the windows). I tried two things: I got rid of the containers. In more details, I added buttons to a panel (instance of JPanel ) and then I added the panel to the windows (instance of JFrame ). It worked fine. I got rid of the panels. In more details, I

Absolute Positioning Graphic JPanel Inside JFrame Blocked by Blank Sections

雨燕双飞 提交于 2019-11-26 12:32:52
I'm trying to improve my understanding of Java, particularly Java GUI, by making a puzzle program. Currently the user selects an image, which is cut up into a specified number of pieces. The pieces are drawn randomly to the screen but they seem to be covered by blank portions of other pieces, and not all of them show up, but I can print out all the coordinates. I am using absolute positioning because a LayoutManager didn't seem to work. I briefly tried layeredPanes but they confused me and didn't seem to solve the problem. I would really appreciate some help. Here are the 2 relevant classes:

How to use KeyListener with JFrame?

北城以北 提交于 2019-11-26 12:32:47
问题 So, I was trying to make a rectangle move with a KeyEvent ( KeyListener ) and whenever I try to hit the key, the rectangle doesn\'t move. The rectangle is drawn, but whenever I hit the left and right keys, nothing happens. I have two classes, one is my main class with the keyEvents and the frame and the other, draws the rectangle and holds the function to move the rectangle. Here is my code: import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; public