Java GUI JMenuBar - java

Ok I have a GUI with and JMenuBar and when I load it sometimes it won't show, but if I minimize it and click back on it, the JMenuBar Shows. Where is my problem? and how can I fix it? oh and if I re size it to, the JMenuBar appears.
Here is my code
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("TwitchBot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(700, 500));
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.pack();
frame.setVisible(true);
KeyGetter.LoadKeys();
try {
Config.loadConfig();
} catch (Exception e) {
e.printStackTrace();
}
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
mb.add(file);
JMenu edit = new JMenu("Edit");
mb.add(edit);
JMenuItem options = new JMenuItem("Options");
options.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Config.openConfig(frame);
}
});
frame.setJMenuBar(mb);
edit.add(options);
}
}

Make sure you're creating/modifying your UI only from within the context of the Event Dispatching Thread. See Initial Threads for more details
Call pack and setVisible only after you have established the base UI.

NEVER USE pack() ..it has conner cases.. some times it fails. From experimentation I have learnt to always call repaint() right before calling setVisible(true) this never fails. Call repaint() right before you call setVisible(true).--THIS NEVER FAILS.
repaint();
setVisible(true); // assured NEVER FAILS.

Related

Java - My action event doesn't work

I'm learning Java and Swing, but my JButton doesn't work.
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Programma {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e){
e.printStackTrace();
}
JFrame frame = new JFrame("DIG");
JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setBounds(100, 100, 130, 35);
panel.add(button);
frame.add(panel);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JLabel label = new JLabel("Hello World");
label.setVisible(true);
panel.add(label);
}
});
}
}
The frame and button are visible, nut when I click it, the label doesn't appear. How can I fix this?
Do I write this before the other component like JPanel, JButton, etc., or do I write this at the end of code:
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
What is the difference ?
By the way, button.setBounds(100, 100, 130, 35) doesn't work, either.
I see some issues in your code:
button.setBounds(100, 100, 130, 35); that line will be ignored and you shouldn't be manually be determining the position of the components. See Null layout is evil and Why is it frowned upon to use a null layout in swing? altough you're not using null layout, there is explained why you shouldn't be manually determining the positions of the components.
You're running everything in your program in the main method, that will be hard to maintain later.
You're calling frame.setVisible(true) before you've added all your elements to it, that will cause you random issues.
You're not running your program on the Event Dispatch Thread (EDT), you can solve this by starting your program with the following code, which places it in the EDT. It's recommended as Swing is not thread safe.
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Your constructor here
}
});
}
You're setting the size of the JFrame with setSize(...), instead call frame.pack() and override the getPreferredSize() method of the JPanel.
After all the above has been said, you need to call revalidate() and repaint() on your ActionListener so your program paints its new state.
This program follows all the above recommendations and produces the following outputs (before clicking and after clicking the button 3 times), I on purpose to not make the images so large, made the GUI shorter (200 x 200 instead of 400 x 400)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Programma {
private JFrame frame;
private JPanel panel;
private JButton button;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Programma().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame("DIG");
panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
button = new JButton("Click Me");
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel label = new JLabel("Hello World");
panel.add(label);
panel.revalidate();
panel.repaint();
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Do i write this before the other componente like JPanel,JButton... or do i write this at the end of code ?
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
What is the difference ?
Altough I answered this on the recommendations, the difference is that if you call setVisible before adding all your elements to the frame, then you'll find yourself with some random issues where the components are not all visible until you pass your mouse over them (or where they should be). frame.pack() and setVisible should be the last ones to be called in your program, and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); can be at the start or the end, it doesn't affects, but I prefer to have it at the end too.
button.setBounds(100, 100, 130, 35); doesn't work too.
Well, that's because of you're using a layout manager (and that's the right way to do your GUIs) instead of a null-layout (which you shouldn't be using anyway) (See point #1).
Edit
What is the difference between frame.setSize(); and frame.setpack() ?
If you read the docs for pack():
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
So, it will calculate the minimum size for your JFrame where all the elements are visible and in their preferred size while setSize will only set the window size, but if you place a JScrollBar inside it for example this will reduce the window size, because of that, that's why you should override the getPreferredSize(...) method of your container, so it will calculate its preferred size including the width of the JScrollBar or some other elements that could modify its size. See Should I avoid the use of setPreferred|Maximum|MinimumSize in Swing? (the general consensus says yes)
When you add components dynamically to panel, you need to repain it.
Do this
panel.revalidate();
after
panel.add(label);

JMenuBar visibility confusion

I'm learning about GUI in Java.
I'm slightly confused here. When I place window.setVisible(true); like this, I only see JMenuBar if I resize it (it doesn't show without some sort of interaction).
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame("My App");
window.setSize(500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JMenuBar bar = new JMenuBar();
window.setJMenuBar(bar);
JMenu menu = new JMenu("File");
bar.add(menu);
}
}
But when I place it at the very bottom, it shows as expected. Why is this?
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame("My App");
window.setSize(500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
window.setJMenuBar(bar);
JMenu menu = new JMenu("File");
bar.add(menu);
window.setVisible(true);
}
}
Here it is explained that it must be called at the end, but what is the reasoning behind this?
java JMenuBar not visible?Why?
After adding a component you would have to repaint the container.
So if you add menubar after window is visible, it will popup after next repaint, in your example, after resize. If menubar is added prior setting window to be visible, it will be drawn at first drawing.
This is common behaviour for Swing components.
See Component javadoc
If you add or remove component:
If the container has already been displayed, the hierarchy must be
validated thereafter in order to display the added component.

How to remove minimize button and keep maximize and close button in JFrame

I want to remove only minimize button from JFrame
but want maximize and close button in JFrame title bar.
Here I am talking about removing not disabling.
I don't think removing the minimize button is a good thing. But may be you can use the setUndecorated() method to remove the title bar and window edges. And you'll have to add your own close and maximize buttons to perfom those action.
Here is an example :
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
public class Example {
public Example() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);//<---- this will disable the frame decorations
JPanel panel = new JPanel();
panel.add(new JLabel("titlebar"));
//Add button maximize
JButton button_max=new JButton("Maximize");
button_max.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(frame.getExtendedState() == JFrame.NORMAL) {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} else {
frame.setExtendedState(JFrame.NORMAL);
}
}
});
panel.add(button_max);
//Add button close
JButton button_close = new JButton(new AbstractAction("Close") {
private static final long serialVersionUID = -4901571960357967734L;
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(button_close);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
Edit :
As #peeskillet states in the comment, even with this method the window still can be resized and draged by the user. The ComponentResizer class allows to do that.
Here is a an SO post which gives a good example to use this class with Jframe.
It's a very big hack, which works only with Synthetica L&F because it provides a painted title bar. Note: this L&F is not free to use. So if you use it you must by a license.
When you use this L&F you can iterate over all component starting from root pane to find an instance of SyntheticaTitlePane. On success you can try to access the field iconifyAction using Reflection Framework and use the method Action.setEnabled(false) on it.
I have no idea how to access the standard title bar because it's native. Probably it's impossible.

Redrawing a new JFrame in an existing one?

I'm trying to display a different JFrame after the user does something in the same window they are using, similar to a login feature. Haven't been able to figure out how to do that.
The workaround I have now is to just hide the current JFrame and then open a new one, which simulates a similar effect. But ideally I want it to just display the next JFrame in the same existing window.
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.JLabel;
import javax.swing.JTextField;
public class Login extends JFrame {
private static int x = 0;
static JTextField txtInput = new JTextField(10);
static JButton btnSwitch = new JButton("Log on");
public Login(){
setLayout(new FlowLayout());
//add button and register
add(new JLabel("Enter password:"));
add(txtInput);
add(btnSwitch);
}
public static void main(String[] args) {
final JFrame frame = new Login();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 150);
frame.setVisible(true);
btnSwitch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(txtInput.getText().equals("123")){
//frame = new GUIHelloWorld(); this doesn't work because "The final local variable frame cannot be assigned, since it is defined in an enclosing type"
//so I went with the below workaround
GUIHelloWorld frame = new GUIHelloWorld();
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
});
}
}
Once the user get pass the first part of the GUI, I want to show em something else like this:
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUIHelloWorld extends JFrame {
public GUIHelloWorld(){
setLayout(new GridLayout(0,1));
add(new JLabel("Hello World"));
add(new JLabel("Welcome to the 2nd part of the GUI"));
}
public static void main(String[] args) {
JFrame frame = new GUIHelloWorld();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Can someone give me an idea of how to display a new JFrame into the existing window that the user is using?
Don't extend from JFrame, especially in the case, frame's can't be added to other frames. Instead, based you individual UI views on something like JPanel
Create a single instance of a JFrame, set it's layout manager to use a CardLayout.
Add each of your view's to the frame, naming each view appropriately
Use CardLayout to switch between the view as needed
You could also consider using a JDialog for the login window, but the basic advice remains; create windows, extend components...

JFrame and visibility: issue with fading out and getting a screenshot

In an action (i.e. a method) executed by pressing a button deployed into a JFrame, I want to hide the java app and then to get a screenshot. Finally, once the screenshot is taken, I need to make the JFrame visible.
The method is the following:
public void myButtonPressedAction(){
//Hiding the JFrame
this.setVisible(false);
//Now I use Robot to get a screenshot using another method
//not reported for simplicity
myMethodToGetScreenshot();
//Making the JFrame visible
this.setVisible(true);
}
What happens is that, once visibility is set to false, the app starts to become invisible and immediately I get the screenshot: unfortunately, the screenshot also capture the JFrame while fading out (i.e., it is going to become invisible, the isVisible method returns true, but the JFrame is not completely invisible).
One possible solution is to insert a timer to put a delay between the call to setVisible(false) and the one to myMethodToGetScreenshot(). However, suppose that the system is busy, the delay could be undervalued; on the contrary, a larger delay will make my application slow!
How can I get the exact time instant such that the JFrame has been completely fade out, i.e. it is really invisible?
EDIT
This are initialized in the constructor:
String myPath= ...;//here I have a String that represent a path to a folder.
JPEGImageWriteParam JPEG_PARAMS_BEST_QUALITY = new JPEGImageWriteParam(null);
JPEG_PARAMS_BEST_QUALITY.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
JPEG_PARAMS_BEST_QUALITY.setCompressionQuality(1f);
This is the code for myMethodToGetScreenshot():
public void myMethodToGetScreenshot(){
BufferedImage capture = new Robot().createScreenCapture(screenArea);
ImageWriter writer = writerService.getWriter();
writer.setOutput(new FileImageOutputStream(new File(myPath+"screenshot.jpg")));
writer.write(null, new IIOImage(capture, null, null), JPEG_PARAMS_BEST_QUALITY);
}
This is the screenshot I get. You can see the JFrame fading out...
Then put some delay time. You can use Swing timer.
Here is a small demo:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ScreenshotDemo {
JFrame frame = new JFrame();
JButton button = new JButton("Catch the screenshot");
Timer timer;
Robot robot;
JLabel label = new JLabel();
public ScreenshotDemo() {
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
// Keeps frame disposed for 3 seconds
timer = new Timer(3000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Rectangle size = new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize());
Image image = robot.createScreenCapture(size);
label.setIcon(new ImageIcon(image));
frame.setVisible(true);
}
});
timer.setRepeats(false);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
timer.start();
}
});
frame.add(button, BorderLayout.NORTH);
frame.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// frame.pack();
frame.setSize(1024, 768);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ScreenshotDemo();
}
});
}
}
Basically, you will hide your frame for some time (3 seconds in this demo). While frame is hidden you will take a snapshot.
Instead of setvisible you can use setExtendedState
//Minimize the JFrame
this.setExtendedState(JFrame.ICONIFIED);
//Now I use Robot to get a screenshot using another method
//not reported for simplicity
myMethodToGetScreenshot();
//Restore the JFrame
this.setExtendedState(JFrame.NORMAL);
You have to add a reasonable delay after hiding the frame and before taking the screenshot. Easiest way is to insert a call to robot.delay() before robot.createScreenCapture().
I'd give the ComponentListener a try (assuming this code goes to a member of a JFrame-extending class):
final Container contentPane = getContentPane();
ComponentAdapter componentAdapter = new ComponentAdapter() {
#Override
public void componentHidden(ComponentEvent arg0) {
myMethodToGetScreenshot();
contentPane.removeComponentListener(this);
}
};
contentPane.addComponentListener(componentAdapter);

Categories

Resources