I was able to hide the the maximize icon by setting setResizable(false), how can achieve the same for the minimize icon?
Here is the way, how to remove min/max buttons from JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JFrameTest extends JDialog {
public JFrameTest(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
JFrameTest frame = new JFrameTest(new JFrame(), "Title");
JPanel panel = new JPanel();
panel.setSize(200, 200);
JLabel lbl = new JLabel("JFrame without max/min");
panel.add(lbl);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
But it's really nasty to users and could be walk arounded by key shortcuts etc.
Check the example Specifying Window Decorations
Related
On button click, I am creating new JFrame, adding a JButton inside it and setting it visible. JFrame is visible but the JButton is not visible.
I tried finding answers on stackoverflow but everyone says to set the JFrame visible after adding the components. I did that also but still, the issue is not solved
below is button code
#Override
public void actionPerformed(ActionEvent arg0) {
popup.showPopup();
}
I have made a class named "popup" with a show popup method.
Below is the code of popup.java class
package justin;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class popupFrame {
private JFrame f = new JFrame("Please wait...");
public void showPopup() {
System.out.println("Showing Popup");
f.setSize(300, 150);
f.setLayout(new FlowLayout());
f.add(new JButton("Test"));
f.setVisible(true);
}
}
It should show the JFrame with items added in it on the click of button.
Please check the below link for my complete code:
https://github.com/jamesfdz/Justin-code
Since I can't see the rest of your code, I have posted an example instead.
public class ControlInterface{
public ControlInterface() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.add(new JButton("Test"));
frame.setVisible(true);
}
}
And the calling class:
public class BusinessLogic{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300, 300);
JButton button = new JButton("Popup");
frame.add(button);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
new ControlInterface();
}
}
});
}
}
I have a problem with JFrame. All I want to do is to create a JFrame for Login with a Button, and when the Button is pressed: it close the Login Frame and opens the Program Frame.
This is my Login Frame:
public static void main(String[] args) {
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LoginPanel primary = new LoginPanel(frame);
frame.setPreferredSize (new Dimension (650, 500));
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
Which opens the Login Panel by passing the Frame in the constructor, the Login Panel:
public class LoginPanel extends JPanel {
JFrame fr;
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
fr.setVisible(false);
fr.dispose();
}
}
public LoginPanel(JFrame frame) {
fr = frame;
JButton submit = new JButton("Button Login");
submit.addActionListener(new submitButton());
add(submit);
}
This is the problem:
When I click on the Button "Button Login" of the LoginPanel, it succesfully opens the new ProgramFrame but it doesn't close at all the old frame (LoginFrame). The LoginFrame becomes smaller, very little, but remains:
Thanks in advance for the help! :)
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
this.dispose(); //changed line
}
}
well your panel is closed bt the jframe is still opened without the jpanel
i have made some changes to your code now both will disposed at the same time
You first initialise JFrame completely, so after inside JButton click event first hide JFrame later dispose it.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HideLoginPage{
public static void main(String[] args){
HideLoginPage loginPage = new HideLoginPage();
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 200, 200, 100);
loginPage.setPane(frame);
frame.setVisible(true);
}
public void setPane(final JFrame frame){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton submit = new JButton("Login");
submit.setSize(100, 30);
panel.add(submit);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JFrame newFrame = new JFrame();
newFrame.setBounds(400, 200, 400, 400);
newFrame.setVisible(true);
frame.setVisible(false);
frame.dispose();
}
});
frame.getContentPane().add(panel);
}
}
I have got a frame in which sometimes a dialog is opened. I would like this dialog to be attached to the existing frame, so for example when I drag that frame the opened dialog follows it. I have heard that this might be possible to achieve using GlassPane but I need some hints. Right now, when I open a new dialog and set its location relative to frame it looks like this:
I would like the "testDialog" to appear next to the frame attached to its upper-right corner.
When I drag the "test" frame, the "testDialog" follows it.
Here is a working example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
showGUI();
}
});
}
public static void showGUI() {
final JFrame frame=new JFrame("test");
JButton open=new JButton("Open new dialog");
open.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("test");
JDialog dialog=new JDialog((Frame)null,"testdialog");
dialog.setPreferredSize(new Dimension(200,300));
dialog.getContentPane().add(new JLabel("testlabel"));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
});
frame.setLayout(new FlowLayout());
frame.getContentPane().add(open);
frame.getContentPane().add(new JLabel("test"));
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(400, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
I am trying to make a popup over my JFrame in Swing. I have made it so that the popup will be layered over the old JFrame and disable the old one by passing in the JFrame and then .disable(). However, i am also trying to make the frame behind darken to show that it is disabled.
I found this:
stackoverflow - Change brightness of JFrame
But how do i use it to lower the brightness of the JFrame that i have as a parameter just before i disable it? Something like darken(frame) and it lowers it using the function darken(JFrame frame). Thanks!
In fact, I'm going to make my comment an answer:
To show a window over another window, and disable the lower window, make the upper window a modal JDialog, and pass the lower window in as its parent.
One way to dim a top-level window is to get its glass pane, set it visible, and draw a semi-opaque grey color over it.
Here's my test of concept code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Dialog.ModalityType;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class DimView {
protected static final Color GP_COLOR = new Color(0, 0, 0, 30);
private static void createAndShowGui() {
final JFrame frame = new JFrame("DimView");
final JPanel glassPanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(GP_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());
};
};
glassPanel.setOpaque(false);
frame.setGlassPane(glassPanel);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(400, 400));
mainPanel.setBackground(Color.pink);
mainPanel.add(new JButton(new AbstractAction("Push Me") {
#Override
public void actionPerformed(ActionEvent evt) {
glassPanel.setVisible(true);
JDialog dialog = new JDialog(frame, "Dialog",
ModalityType.APPLICATION_MODAL);
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
glassPanel.setVisible(false);
}
}));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
The problem is when I run the code below, I see a white splash on my screen for about maybe 100ms which is extremely annoying:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
public MyPanel() {
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
add(jtext);
add(new JButton("some button"));
// TODO: gui elements
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("MyPanel");
frame.getContentPane().add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
});
}
}
My entire work setup is in dark theme. Equivilant code written in C++ with Qt does not show any white splashs on the same system. I'm using Ubuntu 12.04 and Intel HD Graphics 3000, java version "1.7.0_45".
Please let me know how this can be fixed.
I googled this problem but all I found was: "how to create splash screen in java?" which what I'm trying to avoid.
update: solution: I'm not sure why this works:
I extended MyPanel to JFrame and put most of the functions there. then in the run() invoked it and set visible to true.
here is the code:
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JFrame {
public MyPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
getContentPane().add(jtext);
// TODO: gui elements
setExtendedState(JFrame.MAXIMIZED_BOTH);
setSize(350, 250);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyPanel mypanel = new MyPanel();
mypanel.setVisible(true);
}
});
}
}
I did not see the 'white flash' here, but try this altered code that calls pack() on the frame.
import java.awt.*;
import javax.swing.*;
public class MyPanel extends JPanel {
public MyPanel() {
this.setBackground(Color.black);
JTextArea jtext = new JTextArea(10, 20);
jtext.setBackground(Color.black);
jtext.setForeground(Color.white);
jtext.setText("some stuff here");
add(jtext);
add(new JButton("some button"));
// TODO: gui elements
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("MyPanel");
frame.getContentPane().add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
});
}
}
Although it looks normal to me on Ubuntu 12.04 / Intel Iris Pro 5200 / Java 6, I can see the effect by running in a slowed emulator. Setting the frame's background to black before setVisible() seems to help.
frame.setBackground(Color.black);