I'm using inteliJ to write a file transfer program and I'm working on the GUI right now using Swing. I've made plenty of programs in swing, but for some reason I can't figure out why my GUI isn't showing up when I run the program. Everything compiles just fine.
public class stage {
JPanel mainContainer = new JPanel();
JPanel window = new JPanel();
JButton loadButton = new JButton();
JButton saveButton = new JButton();
JTextPane cmdOut = new JTextPane();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu();
JMenuItem exitButton = new JMenuItem("exit");
public void display(){
mainContainer.setVisible(true);
mainContainer.add(window);
mainContainer.add(menuBar);
menuBar.add(menu);
menu.add(exitButton);
window.setLayout(new GridBagLayout());
window.add(loadButton);
window.add(saveButton);
window.add(cmdOut);
cmdOut.setText("TEST");
window.setVisible(true);
}
}
Here is my main method in another class.
public static void main(String[] args) throws IOException {
stage stage = new stage();
stage.display();
}
why don't you put your main panel into a JFrame, so that your JFrame will contain your main panel, and your main panel hold everything else?
Related
I have just started learning Java Swing and I am making a application form sort of project and I want to add more components like buttons,text areas and other in specific tab but I am not able to.
The code is given below:
import javax.swing.*;
public class TabbedPaneExample {
JFrame f;
TabbedPaneExample(){
f=new JFrame("Hotel Appication Form");
JTextArea ta=new JTextArea(400,400);
JPanel p1=new JPanel();
p1.add(ta);
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JTabbedPane tp=new JTabbedPane();
tp.setBounds(39,20,500,500);
tp.add("form",p1);
tp.add("preferences",p2);
tp.add("FaQ's",p3);
f.add(tp);
f.setSize(600,600);
f.setVisible(true);
//JButton
JButton b = new JButton("Submit");
b.setBounds(50,50,30,20);
f.add(b);
//JLabel
}
public static void main(String[] args) {
new TabbedPaneExample();
}
}
The screenshot of the output is attached here
In this code example simple frame with tabbed panel and simple components in each tab.
Your problem was incorrect adding components to JPanel.
Hope that helps you!
output1 output2
import java.awt.GridLayout;
import javax.swing.*;
public class test {
private static void createAndShowGUI() {
// Create and set up the window.
final JFrame frame = new JFrame("Split Pane Example");
// Display the window.
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set grid layout for the frame
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton button = new JButton("Button");
JLabel label = new JLabel("Label");
JTextField textField = new JTextField("TextField");
panel.add(button);
panel.add(label);
panel2.add(textField);
tabbedPane.addTab("Tab1", panel);
tabbedPane.addTab("Tab2", panel2);
frame.getContentPane().add(tabbedPane);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The problem seems simple, but I can't seem to get around it.
I am using GlassPane in my frame (also the ContentPane). So when I add JMenuBar to the frame it doesn't show up. If/when I am using GlassPane at other times, everything works absolutely fine. I did some research, what I understand is that JMenuBar is shown on RootPane and I believe GlassPane is somehow hiding it.
I need to know if there is any way to get JMenuBar while using glassPane?
Thanks
UPDATE:
I am setting glassPane.setOpaque(false)
UPDATE:
The actual lines of code are much more but here are the ones that are relative to the problem.
(mainPanel and notificationPanel are self constructed classes extending from JPanel)
public class Demo extends JFrame {
/////////////////////////////////////////////////////////////////////////
// JMenuBar
private final JMenuBar mainMenuBar;
private final JMenu fileMenu;
private final JMenuItem exitFileMenu;
/////////////////////////////////////////////////////////////////////////
// CONTENT PANE & COMPONENTS
private final JPanel contentPanel;
private final JPanel buttonPanel;
private final JButton button1;
/////////////////////////////////////////////////////////////////////////
// GLASSPANE AND COMPONENTS
private final JPanel glassPanel;
private final JPanel buttonPanel2;
private final JButton button2;
public Demo() {
super();
this.mainMenuBar = new JMenuBar();
this.fileMenu = new JMenu("File");
this.exitFileMenu = new JMenuItem("EXIT");
this.contentPanel = new JPanel(new BorderLayout());
this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.button1 = new JButton("Button 1");
this.glassPanel = new JPanel(new BorderLayout());
this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
this.button2 = new JButton("Button 2");
}
public void initGUI() {
this.fileMenu.add(this.exitFileMenu);
this.mainMenuBar.add(this.fileMenu);
this.buttonPanel.add(this.button1);
this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH);
this.buttonPanel2.add(this.button2);
this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH);
super.setContentPane(this.contentPanel);
super.setGlassPane(this.glassPanel);
this.glassPanel.setOpaque(false);
this.glassPanel.setVisible(true);
super.setExtendedState(JFrame.MAXIMIZED_BOTH);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setJMenuBar(mainMenuBar);
super.setVisible(true);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.initGUI();
}
}
You are using BorderLayout and the option BorderLayout.NORTH for your glassPanel. It takes the entire space in the north and it overlaps your entire menu. Therefore you don't see anything anymore. Change for example your panel creation into:
this.glassPanel = new JPanel();
Then your panel will be resized to only fit your button and you will see your menu behind. You can play around with some layouts and see which one fits. But just remember that the glass pane is always on top of everything. Just a little side note: When you add your button directly to 'glassPanel' (without using 'buttonPanel2') you can remove the little "borders". Otherwise you could resize it to perfectly fit your button. Both is possible, but if you only want to have a single component (like your button) then I would add it directly.
Alright guys I accidentally found the solution to the problem. It was in-fact simple, if you are using nested Panels within the 'glassPane' then simply set each nested panel's opacity to false. If you don't the nested panels will show its background to each of its bounds and overlap any underlying layer(s).
Here's the working code of the above Demo.
public class Demo extends JFrame {
/////////////////////////////////////////////////////////////////////////
// JMenuBar
private final JMenuBar mainMenuBar;
private final JMenu fileMenu;
private final JMenuItem exitFileMenu;
/////////////////////////////////////////////////////////////////////////
// CONTENT PANE & COMPONENTS
private final JPanel contentPanel;
private final JPanel buttonPanel;
private final JButton button1;
/////////////////////////////////////////////////////////////////////////
// GLASSPANE AND COMPONENTS
private final JPanel glassPanel;
private final JPanel buttonPanel2;
private final JButton button2;
public Demo() {
super();
this.mainMenuBar = new JMenuBar();
this.fileMenu = new JMenu("File");
this.exitFileMenu = new JMenuItem("EXIT");
this.contentPanel = new JPanel(new BorderLayout());
this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.button1 = new JButton("Button 1");
this.glassPanel = new JPanel(new BorderLayout());
this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
this.button2 = new JButton("Button 2");
}
public void initGUI() {
this.fileMenu.add(this.exitFileMenu);
this.mainMenuBar.add(this.fileMenu);
this.buttonPanel.add(this.button1);
this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH);
this.buttonPanel2.add(this.button2);
this.buttonPanel2.setOpaque(false);
this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH);
super.setContentPane(this.contentPanel);
super.setGlassPane(this.glassPanel);
this.glassPanel.setOpaque(false);
this.glassPanel.setVisible(true);
super.setExtendedState(JFrame.MAXIMIZED_BOTH);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setJMenuBar(mainMenuBar);
super.setVisible(true);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.initGUI();
}
}
Now Also remember always to set the glassPane's Opacity after you call 'setGlassPane(JPanel)' Otherwise the GlassPane remain Opaque. (The nested Panels you may set before or after calling the said method)
I have a JPanel which is in a box layout but I am unsure how to align the JPanel to center of the window (and stay centered even if window is resized) I've tried looking for a solution but all questions seem over complicated compared to what it is that I'm looking for.
import java.awt.*;
import javax.swing.*;
public class Stacker extends JFrame {
public Stacker() {
super("Stacker");
setSize(430, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create top panel
JPanel commandPane = new JPanel();
BoxLayout vertical = new BoxLayout(commandPane,
BoxLayout.Y_AXIS);
commandPane.setLayout(vertical);
JButton subscribe = new JButton("Subscribe");
JButton unsubscribe = new JButton("Unsubscribe");
JButton refresh = new JButton("Refresh");
JButton save = new JButton("Save");
commandPane.add(subscribe);
commandPane.add(unsubscribe);
commandPane.add(refresh);
commandPane.add(save);
JMenuItem j1 = new JMenuItem("File");
JMenuItem j2 = new JMenuItem("Open");
JMenuItem j3 = new JMenuItem("Close");
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Feeds");
menu.add(j1);
menu.add(j2);
menu.add(j3);
menubar.add(menu);
setJMenuBar(menubar);
// create bottom panel
/*JPanel textPane = new JPanel();
JTextArea text = new JTextArea(4, 70);
JScrollPane scrollPane = new JScrollPane(text);
// put them together
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(commandPane);
add(scrollPane); */
setJMenuBar(menubar);
add(commandPane);
setVisible(true);
}
public static void main(String[] arguments) {
Stacker st = new Stacker();
}
}
You say you're using a BoxLayout, but is the JPanel with the BoxLayout the JPanel you want to center, or does it contain the JPanel you want to center?
If it contains the JPanel you want to center, then you can add a glue on either side of the JPanel to be centered. If it is the JPanel you want to center, then you can use GridBagLayout or BoxLayout to achieve the effect you're talking about.
Googling something like "Java center component" will give you a ton of results.
for this idea (still not clear from your description) use GridBagLayout without set for GridBagConstraints
.
.
.
import java.awt.*;
import javax.swing.*;
public class CenteredJPanel {
private JFrame frame = new JFrame("Test");
private JPanel panel = new JPanel();
private JButton subscribe = new JButton("Subscribe");
private JButton unsubscribe = new JButton("Unsubscribe");
private JButton refresh = new JButton("Refresh");
private JButton save = new JButton("Save");
public CenteredJPanel() {
panel.setLayout(new GridBagLayout());
panel.add(subscribe);
panel.add(unsubscribe);
panel.add(refresh);
panel.add(save);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CenteredJPanel centeredJLabel = new CenteredJPanel();
}
});
}
}
I'm a java newbie and I'm currently working on a simple application with a menu, scrollpane and textarea.
So far I've gotten everything I wanted on the form but when I fire up my application the scrollpane/textarea won't show up until I rezise the window.
I've tried using the repaint method as suggested on other forums for similar problems but it didn't work, perhaps I'm not using it correctly :S
Here's my class:
public class FenetreEditeur {
public static void main(String[] args){
FenetreEditeur f = new FenetreEditeur();
}
public FenetreEditeur(){
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
initMenuBar(frame);
JTextArea areaMain = new JTextArea();
JScrollPane scrollPane = new JScrollPane(areaMain);
frame.add(scrollPane);
}
private void initMenuBar(JFrame frame){
JMenuBar menu = new JMenuBar();
JMenu revision = new JMenu("Revision");
JMenuItem statistiques = new JMenu("Statistiques");
JMenuItem grammaire = new JMenu("Grammaire et orthographe");
JMenuItem analyse = new JMenu("Analyse Automatique");
menu.add(revision);
revision.add(statistiques);
revision.add(grammaire);
revision.add(analyse);
frame.setJMenuBar(menu);
}}
Any help/tip would be greatly appreciated.
Thanks!
Call scrollPanel.revalidate() after adding it, or better, move frame.setVisible(true) to the end:
public FenetreEditeur(){
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
initMenuBar(frame);
JTextArea areaMain = new JTextArea();
JScrollPane scrollPane = new JScrollPane(areaMain);
frame.add(scrollPane);
frame.setVisible(true);
}
Im not sure how to reference to JPanel when it was declared like this.
This is the coding of the entire program: Everything works but the layout is not how I want it. adding BorderLayouts to it doesnt seem to work.
class FrameDemo
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("CIT Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(350, 250));
frame.add(new JPanel()
{{
String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5,10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{{
addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
frame.dispose();
JOptionPane.showMessageDialog(frame,"IT WORKS!");
}
});
}});
}});
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Not sure how to reference to JPanel to use BorderLayout.
How would I go about doing this?
If you add a panel to a JFrame (using add() as you are doing here) , you can assume that it is being added as the contentPanel. However, it is much better to be explicit. Instead of this:
frame.add(new JPanel()
{}
use this:
JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);
EDIT:
after looking at your edit, what is clear is that you are initializing an anonymous class. This is not generally bad practice, but here you are putting a lot of initialization code. The code you are putting in is in a double-braced block, which in essence puts it into a static initializer. It seems like a normal anonymous class, but it really isn't. With that much code, it deserves its own class. If you went so far as to code a new class, you should be fine. I would suggest that you then define it as an extension of JPanel and in your own constructor pass a new BorderLayout() to the super.
EDIT 2:
if you create a brand new file/class named Bar and you coded it like this:
public class Bar extends JPanel {
public Bar(final JFrame frame) {
String[] tests = { "A+ Certification", "Network+ Certification",
"Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select") {
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
All you would need to do to use it would be to call
JPanel panel = new Bar(frame);
however, the goal here is to use a BorderLayout, so I would suggest that you put this call in to start:
public Bar(final JFrame frame) {
super(new BorderLayout());
.... everything else
}
On top of all answers already given... Your program has fundamental flaw. All manipulation with Swing components has to be done on EDT thread. So your code should be slightly different
class FrameDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
void run() {
/// your code here
}
});
}
}
Otherwise what happens is unpredictable. You can read more about it at http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html
Adding BorderLayout to it doesn't seem to work.
The default layout of a new JPanel is FlowLayout, as can be seen by resizing the frame. To see the difference, replace
frame.add(new JPanel()
with
frame.add(new JPanel(new BorderLayout())
As #akf suggests, lengthy static initialization using double braces can be obscure.