Swing hangs when being resized on a second display - java

Main thread calls GUI() function that adds frame, menus, menuitems, and a scrollpane with jtable (DefaultTableModel).
If i place the running application to a second display/screen it hangs as soon i switch to a different application or when i try to resize it.
public static void main(String[] args) {
GUI();
}
public static void GUI(){
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
//menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); //start
menu = new JMenu("Options");
menuBar.add(menu); //options
//top panel
JPanel top = new JPanel();
button = new JButton("Paste");
button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
start.addActionListener(new buttonActionListener("start"));
top.add(start);
//main panel with table
main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
//bottom panel
JPanel bottom = new JPanel();
Creq = new JCheckBox();
Creq.setSelected(true);
item= new JTextField("item");
comment = new JTextField("comment");
target= new JTextField("target");
req = new JTextField("requirement");
bottom.add(Creq);
bottom.add(req);
bottom.add(item);
depToUpdate.setVisible(true);
bottom.add(target);
targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}

After spending way too much time fixing the code so that it would actually run in Java, I tested the GUI on my second monitor in Windows 8. The GUI didn't do anything, but it didn't freeze either.
Here are the problems I fixed:
I moved the Swing GUI code into a regular class method (not static).
I put the Swing components on the Event Dispatch thread. You must always create and execute your Swing components on the Event Dispatch thread.
Here's the version of the code I ran.
package snippet;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class Snippet implements Runnable{
private JCheckBox creq;
private JFrame window;
private JScrollPane main;
private JTable table;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Snippet());
}
#Override
public void run() {
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
// menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
// menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
// menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
// menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
// menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); // start
menu = new JMenu("Options");
menuBar.add(menu); // options
// top panel
JPanel top = new JPanel();
button = new JButton("Paste");
// button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
// button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
// start.addActionListener(new buttonActionListener("start"));
top.add(start);
// main panel with table
// main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
main = new JScrollPane(table);
// bottom panel
JPanel bottom = new JPanel();
creq = new JCheckBox();
creq.setSelected(true);
JTextField item = new JTextField("item");
JTextField comment = new JTextField("comment");
JTextField target = new JTextField("target");
JTextField req = new JTextField("requirement");
bottom.add(creq);
bottom.add(req);
bottom.add(item);
// depToUpdate.setVisible(true);
bottom.add(target);
// targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}

Related

How to add ScrollBar on Textarea or JFrame in Java Swing?

Hi everyone ,i`m beginner in programmation and i wanted to make a text editor interface by using java Eclipse and i get problem each time i add scrollbar , the text area disappear ... How can i fix my code to continue on it ?... i saw many tutorials but nothing work>
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
public class Fenetre extends JFrame {
JMenuBar Menubar = new JMenuBar();
JMenu MenuFichier = new JMenu("Fichier");
JMenuItem MenuFichier1 = new JMenuItem("Nouveau");
JMenuItem MenuFichier2 = new JMenuItem("Ouvrir");
JMenuItem MenuFichier3 = new JMenuItem("Enregister");
JMenuItem MenuFichier4 = new JMenuItem("Enregister sous");
JMenuItem MenuFichier5 = new JMenuItem("Quitter");
JMenu MenuEdition = new JMenu("Edition");
JMenuItem MenuEdition1 = new JMenuItem("Couper");
JMenuItem MenuEdition2 = new JMenuItem("Copier");
JMenuItem MenuEdition3 = new JMenuItem("Coller");
JMenu MenuAide = new JMenu("Aide");
JMenuItem MenuAide1 = new JMenuItem("A propos");
JPanel p=new JPanel();
JScrollPane sp = new JScrollPane ();
JTextArea jt = new JTextArea (90,90);
public Fenetre() {
add(p);
setTitle("Text Editor");
setBounds(100,100, 500, 500);
//setExtendedState(JFrame.MAXIMIZED_BOTH);
sp.add(jt);
jt.setText(null);
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
jt.setVisible(true);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.add(sp);
sp.setVisible(true);
Menubar.add(MenuFichier);
Menubar.add(MenuEdition);
Menubar.add(MenuAide);
MenuFichier.add(MenuFichier1);
MenuFichier.add(MenuFichier2);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier3);
MenuFichier.add(MenuFichier4);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier5);
MenuEdition.add(MenuEdition1);
MenuEdition.add(MenuEdition2);
MenuAide.add(MenuAide1);
Menubar.setBackground(Color.PINK);
getContentPane().add(sp);
setJMenuBar(Menubar);
setVisible(true);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
and this is the main of the code
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class CreationEditor {
public static void main(String[] args) {
JFrame f = new Fenetre();
}
}

hidden labels/panels in java

Basically, i am trying to make it so there are 3 buttons on the bottom of the screen and then have a label which has words in it in the middle of the screen. However, i cant seem to have both buttons and the label in the GUI at the same time. I am a beginner and dont know much about layouts (even though i have read into them) so any help/guidance would be helpful on why i cannot see both the label and the buttons.enter code here
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class AWorldPanel implements ActionListener {
/** Declaring all the menu items within the GUI **/
private JMenuItem Fileitem1 = new JMenuItem("New configuration");
private JMenuItem Fileitem2 = new JMenuItem("Open configuration file ");
private JMenuItem Fileitem3 = new JMenuItem("Save");
private JMenuItem Fileitem4 = new JMenuItem("Save As");
private JMenuItem Fileitem5 = new JMenuItem("Exit");
private JMenuItem Viewitem1 = new JMenuItem("Display configuration");
private JMenuItem Viewitem2 = new JMenuItem("Edit configuration");
private JMenuItem Viewitem3 = new JMenuItem("Info about Bugs");
private JMenuItem Viewitem4 = new JMenuItem("Info about Map");
private JMenuItem Edititem1 = new JMenuItem("Remove");
private JMenuItem Edititem2 = new JMenuItem("Add");
private JMenuItem Simulationitem1 = new JMenuItem("Simulation");
private JMenuItem Helpitem1 = new JMenuItem("Info about application");
private JMenuItem Helpitem2 = new JMenuItem("Info about author");
private JLabel theLabel;
private JPanel thePanel;
JButton Run, Pause, Reset;
JFrame GUI = new JFrame("Graphical User Interface");
private static AWorld guiworld;
public AWorldPanel() {
/** Creating the menu **/
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu View = new JMenu("View");
JMenu Edit = new JMenu("Edit");
JMenu Help = new JMenu("Help");
/** welcome label **/
theLabel = new JLabel("Hello ", JLabel.CENTER);
theLabel.setVisible(true);
theLabel.setVerticalTextPosition(JLabel.TOP);
theLabel.setHorizontalTextPosition(JLabel.CENTER);
/** file sub menus **/
menubar.add(File);
File.add(Fileitem1);
File.add(Fileitem2);
File.add(Fileitem3);
File.add(Fileitem4);
File.add(Fileitem5);
menubar.add(View);
View.add(Viewitem1);
View.add(Viewitem2);
View.add(Viewitem3);
View.add(Viewitem4);
menubar.add(Edit);
Edit.add(Edititem1);
Edit.add(Edititem2);
menubar.add(Help);
Help.add(Helpitem1);
Help.add(Helpitem2);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.setBorder(new EmptyBorder(new Insets(300, 125, 100, 100)));
Run = new JButton("Run");
Pause = new JButton("Pause");
Reset = new JButton("Reset");
panel.add(Run);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Pause);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Reset);
GUI.add(panel);
GUI.add(theLabel);
GUI.setJMenuBar(menubar);
}
private static void createAndShowGUI() {
AWorldPanel newworld = new AWorldPanel();
// Create the container
JFrame frame = new JFrame("Graphical User Interface");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// changing the menu settings
newworld.GUI.setLocation(300, 100);
newworld.GUI.setSize(500, 500);
newworld.GUI.setVisible(true);// Now the frame will appear on screen
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The default layout manager for a JFrame is a BorderLayout. If you don't specify a constraint then the component will be added to the BorderLayout.CENTER. You can't add multiple components to the same location in the layout. Try something like:
GUI.add(panel, BorderLayout.SOUTH);
GUI.add(theLabel, BorderLayout.CENTER);
Also, learn standard Java naming conventions. Every book, tutorial or example you will read uses these standards so don't make up your own conventions. Variable names do not start with an upper case character.

Java ActionListener not listening to event

anyone can tell why the class OpenMenuListener doesnt send a feedback when i click the open button in my Gui ? The erase button works though. It sends me a feedback. I'm exausted.
import java.awt.*;
import javax.swing.*;
public class DrawingApplication extends JFrame {
JComponent drawingArea;
class EraseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked erase");
}
}
class OpenMenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked open");
}
}
public DrawingApplication() {
JPanel frame = new JPanel();
add(frame);
// panel1.add( new JButton(Figuur),BorderLayout.CENTER);
drawingArea = new JLabel();
// label1.add(drawingArea);
frame.add(drawingArea);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenu open = new JMenu("Open");
fileMenu.add(open);
fileMenu.addSeparator();
JMenu save = new JMenu("Save");
fileMenu.add(save);
fileMenu.addSeparator();
JMenu close =new JMenu("Close");
fileMenu.add(close);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
helpMenu.add(new JMenu("Info"));
JPanel panel2 = new JPanel();
add(BorderLayout.SOUTH, frame);
frame.add(new JLabel("figuurkeuze"));
frame.add(panel2);
setVisible(true);
JRadioButton rectButton = new JRadioButton("Rectangle");
JRadioButton triangleButton = new JRadioButton("Triangle");
JRadioButton circleButton = new JRadioButton("Circle");
frame.add(rectButton);
frame.add(triangleButton);
frame.add(circleButton);
JButton erase = new JButton("Erase");
frame.add(erase);
EraseButtonListener eraselistener = new EraseButtonListener();
erase.addActionListener(eraselistener);
OpenMenuListener openMenuListener = new OpenMenuListener();
open.addActionListener(openMenuListener);
}
public static void main(String[] args) {
DrawingApplication frame = new DrawingApplication();
frame.setTitle("My prgram");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
It seems a problem of name aliasing, you add the listener to the open variable which is declared as a JMenuItem, so you are adding the ActionListener to the menu item instead that to a button (that you never declare since there is no JButton open = new JButton("open") anywhere).

Align icon and JCheckbox in JPopupmenu

I have a Problem with some icons and check-boxes in a JPopupMenu. The Check-boxes and Icons are not aligned
The Items are created like:
JMenuItem deleteAttributeMenuItem = new JMenuItem(locale.getString("TREE_AttrDelete"), iconDelete);
JMenuItem primaryKeyAttributeMenuItem = new JCheckBoxMenuItem(locale.getString("TREE_AttrChkBoxPK"));
Please take a look at the picture:
Any tips?
Have a look at this, in order to achieve what you wanted, I did this,
JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
cbmi.setIconTextGap(15);
cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
helpMenu.add(cbmi);
And here is the OUTPUT of the said thingy :
right now I can't found the right way how to re_layout JCheckBoxMenuItem,
but do you agree with this standars output from Swing by using (default) Metal Look and Feel???, just to avoiding any missunderstand by using another Look and Feel(s), because there are some differencies in the API betweens Swing's Standard Look and Feels too
from tutorials code (modified and removed balasts and noises)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuLookDemo {
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
private JTextArea output;
private JScrollPane scrollPane;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JRadioButtonMenuItem rbMenuItem;
private JCheckBoxMenuItem cbMenuItem;
public JMenuBar createMenuBar() {
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
menuBar.add(menu);
menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Both text and icon", errorIcon);
menu.add(menuItem);
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one", infoIcon);
group.add(rbMenuItem);
menu.add(rbMenuItem);
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item", warnIcon);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
menu.add(cbMenuItem);
menu.addSeparator();
return menuBar;
}
public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("MenuLookDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MenuLookDemo demo = new MenuLookDemo();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
these methods talking about possitions in pixels

JButtons fail to appear in the JFrame

I have code that displays a menu and 4 JButtons in a JFrame. I tested the code last night and everything was working fine. Now the JButtons do not appear in the JFrame today in the morning. I tried doing in Eclipse and still I got the same result.
The output I am getting :
My code:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class Control {
//JFrame
JFrame main = new JFrame();
//MenuBar
JMenuBar menuBar = new JMenuBar();
//Adding the menu
JMenu fileMenu = new JMenu("File");
JMenu functionMenu = new JMenu("Function");
JMenu helpMenu = new JMenu("Help");
//Adding the Menu Item
JMenuItem addFlight = new JMenuItem("Add Flight");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem landFlight = new JMenuItem("Land Flight");
JMenuItem virtualPath = new JMenuItem("Virtual Path");
JMenuItem flightDetails = new JMenuItem("Flight Details");
JMenuItem about = new JMenuItem("About ...");
//JPanel
JPanel pnlButton = new JPanel();
//Buttons
JButton btnAddFlight = new JButton("Add Flight");
JButton btnLandFlight = new JButton("Land Flight");
JButton btnVirtualPath = new JButton("Virtual Path");
JButton btnFlightDetails = new JButton("Flight Details");
public Control() {
//Adding to the file menu
fileMenu.add(addFlight);
fileMenu.add(exit);
//Adding to the function menu
functionMenu.add(landFlight);
functionMenu.add(virtualPath);
functionMenu.add(flightDetails);
//Adding to the help menu
helpMenu.add(about);
exit.add(new JSeparator());
flightDetails.add(new JSeparator());
//Adding the Menus to the Menu Bar
menuBar.add(fileMenu);
menuBar.add(functionMenu);
menuBar.add(helpMenu);
//FlightInfo setbounds
btnAddFlight.setBounds(30, 30, 120, 30);
btnLandFlight.setBounds(30, 80, 120, 30);
btnVirtualPath.setBounds(30, 130, 120, 30);
btnFlightDetails.setBounds(30, 180, 120, 30);
//JPanel bounds
pnlButton.setLayout(null);
//Adding to JFrame
pnlButton.add(btnAddFlight);
pnlButton.add(btnLandFlight);
pnlButton.add(btnVirtualPath);
pnlButton.add(btnFlightDetails);
main.add(pnlButton);
// JFrame properties
main.setJMenuBar(menuBar);
main.setLayout(null);
main.setBackground(Color.red);
main.setSize(800, 300);
main.setTitle("Air Traffic Control");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
//Adding the actionlistener
//btnAddFlight.addActionListener(new AddFlight());
//btnLandFlight.addActionListener(new LandFlight());
}
public static void main(String[] args) {
new Control();
}
}
I want to make the JButtons appear on the JFrame.
Many Thanks
You shouldn't add widgets (pnlButton) directly to the JFrame, you need to add them to a sub panel that is automatically created for you called the content pane. To get the content pane do
Container cp = main.getContentPane();
so then do
cp.add(pnlButton);
It's typically a bad idea to use a null layout with absolute positioning, btw.
Use GridBagLayout instead of null layout.
visit the following links for reference
http://www.java2s.com/Code/Java/Swing-JFC/WorkGridBagConstraints3.htm
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/gridbag.html

Categories

Resources