I don't use anything IDE.
import javax.swing.*;
public class FirstAttempt
{
public static void main(String[] args)
{
JFrame window = new JFrame("My first window");
window.setSize(100,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
It doesn't appear. Why?
Perhaps the window initializes visibility outside of your monitor's display area? Try adding
frame.setLocationRelativeTo(null);
after your setVisible() call to ensure that the window is initialized at the center of the screen.
Related
Hope you are all well!
Hope you understand my Java question... I have created a JFrame window with text to display, but it doesn't display at run-time (as it should) unless I maximise the Frame window.
Can't understand it?
Here's some code:
package test;
import javax.swing.*;
class Test{
private String x;
private Test() {
x="150";
}
public static void main(String[] args) {
Test o1 = new Test();
JTextField l = new JTextField(o1.x, JTextField.CENTER);
l.setAlignmentX(0);
l.setAlignmentY(0);
JFrame window = new JFrame("Hello World!");
window.setSize(800, 600);
window.setResizable(true);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(l);
}
}
Altering the size of the frame (by maximising it) will cause it to be repainted. The reason it needs to be repainted is that you added content to it after you already made it visible.
Instead, you could move window.setVisible(true); to the end, so you don't show the window until you've added everything to it.
Recently I have been working on a project which uses Java Swing to build the GUI. I want to print the text in a JTextArea and therefore I wrote something like
boolean printed = textArea.print();
This brings up a modal dialog. However the dialog seems to have no parent and the main frame (the one containing textArea) blocks the print dialog.
As you see, the print dialog (the thin line at the bottom) goes behind the main JFrame.
Code:
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
public class JTextAreaPrintBug {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setLocationRelativeTo(null);
frame.setAlwaysOnTop(true);
//now add JTextArea
frame.setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
frame.add(textArea, BorderLayout.CENTER);
frame.setVisible(true);
try {
textArea.print();
} catch (PrinterException ex) {}
}
});
}
}
Is it possible to bring the print dialog to front (or explicitly set the parent of the print dialog), preferably not reinventing the wheel? Thanks in advance.
Edit: I know there is a line frame.setAlwaysOnTop(true);. What I really want is to bring the print dialog to the very front even if the main frame is always on top.
Edit (2): I finally opted for a workaround which uses a WindowFocusListener and the getOppositeWindow() method in WindowEvent to obtain a reference to the print dialog. Still I resort to reflection (getting the name of the instance's class) to check whether the "opposite window" is a print dialog, or just an ordinary dialog in my application. Anyway, welcome for more elegant solutions.
It's because of this
frame.setAlwaysOnTop(true);
So change it to false to see the print window.
or
Remove that line, if you don't want your main window to block other windows. Default value is false anyway.
My Java code is below. I do not know why I keep on running into this error. I am using Java JRE 1.8. Thanks.
package Main;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
//Entry Point
JFrame window = new JFrame("Practice Game");
//Tells Java to create a window and name it "Practice Game".
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Tells Java to completely close the program one the red x is pressed
window.setResizable(false);
//Tells Java to disable the possibility to Resize the window (False = boolean)
window.pack();
//Tells Java to make the screen size the recommended size determined by the computer screen.
window.setVisible(true);
//Tells Java to either hide or show the window.
}
}
GamePanel must extend JPanel for you to set it as the content pane.
I created a JFrame and and a JWindow. My problem is that when I click on another application my JFrame passes behind the application but not my JWindow which remains always on top.
I tried to call setAlwaysOnTop(false) on my JWindow but this doesn't change anything.
I would like that the JWindow "follows" the JFrame.
Here's my test code:
public class WindowAlwaysOnTop {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 400));
final JWindow window = new JWindow(frame);
window.setAlwaysOnTop(false);
window.setSize(new Dimension(200, 200));
frame.setVisible(true);
window.setVisible(true);
}
}
This problem occurred with JRE 1.6.0_32 and is solved with JDK7.
Don't use a JWindow.
Instead use a JDialog. Just make sure you specify the frame as the parent when you create the dialog. You can use an undecorated dialog if you don't like the titlebar.
I am developing a tool for my laptop. I want to disable minimize button in the JFrame. I have already disabled maximize and close button.
Here is the code to disable maximize and close button:
JFrame frame = new JFrame();
frame.setResizable(false); //Disable the Resize Button
// Disable the Close button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Please, tell me how to disable minimize button.
Generally, you can't, what you can do is use a JDialog instead of JFrame
As #MadProgrammer said (+1 to him), this is definitely not a good idea you'd rather want to
use a JDialog and call setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); to make sure it cannot be closed.
You could also use a JWindow (+1 to #M. M.) or call setUndecorated(true); on your JFrame instance.
Alternatively you may want to add your own WindowAdapater to make the JFrame un-minimizable etc by overriding windowIconified(..) and calling setState(JFrame.NORMAL); from within the method:
//necessary imports
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test {
/**
* Default constructor for Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Test test = new Test();
}
});
}
private final JFrame frame = new JFrame();
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);
frame.addWindowListener(getWindowAdapter());
//pack frame (size JFrame to match preferred sizes of added components and set visible
frame.pack();
frame.setVisible(true);
}
private WindowAdapter getWindowAdapter() {
return new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {//overrode to show message
super.windowClosing(we);
JOptionPane.showMessageDialog(frame, "Cant Exit");
}
#Override
public void windowIconified(WindowEvent we) {
frame.setState(JFrame.NORMAL);
JOptionPane.showMessageDialog(frame, "Cant Minimize");
}
};
}
}
If you don't want to allow any user action use JWindow.
You may try to change your JFrame type to UTILITY. Then you will not see both minimize btn and maximize btn in your program.
I would recommend you to use jframe.setUndecorated(true) as you are not using any of the window events and do not want the application to be resized. Use the MotionPanel that I've made, if you would like to move the panel.