JButtons not appearing on JPanel - java

I am working on a menu that should come up with 2 buttons, "resume" and "Exit to main menu". The problem is that the JPanel is showing without any problems but the JButtons are not there, even though I have added them. The following part of code is the handling of the graphical side of the menu.
if(secMenuFlag){
JPanel menu = new JPanel();
JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
menu.setLayout(null);
menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
menu.setSize(menuSize[0], menuSize[1]);
menu.setBackground(new Color(0, 0, 0));
resume.addActionListener(this);
resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
resume.setBackground(Color.white);
resume.setLocation(100, 100);
exit.addActionListener(this);
exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
exit.setBackground(Color.white);
exit.setLocation(200, 100);
menu.add(resume);
menu.add(exit);
super.add(menu, 0);
}

set the bounds of the buttons. I have done for a resume, please follow the same procedure for the exit.
JPanel menu = new JPanel();
JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
menu.setLayout(null);
JFrame frame;
frame = new JFrame("check");
frame.setLayout(null);
frame.setSize(300, 300);
int[] menuSize = new int[2];
menuSize[0] = 200;
menuSize[1] = 300;
menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
menu.setSize(menuSize[0], menuSize[1]);
menu.setBackground(new Color(255, 255, 255));
// resume.addActionListener((ActionListener) this);
resume.setBounds(20, 20, 100, 100);
resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
resume.setBackground(Color.BLACK);
resume.setLocation(100, 100);
resume.setVisible(true);
// exit.addActionListener((ActionListener) this);
exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
exit.setBackground(Color.BLACK);
exit.setLocation(200, 100);
exit.setVisible(true);
menu.add(resume);
menu.add(exit);
frame.add(menu);
frame.setVisible(true);

Related

JTextField and JComboBox make my JPanel disappear from the screen

whenever I uncomment my JTextField or JComboBox they make all my swing elements disappear, I've tried resizing them and manipulating them in any way but I have no clue as to why the stuff is disappearing. They weren't on a previous program I wrote and they're wrote the exact same way. They elements even disappear if the two objects aren't even added to the panel.
Here's the code I have for my program. I don't really want to separate the 2 panels into their own Classes.
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// Create window
JFrame window = new Window();
String[] optionsToChoose = {"Car Parts", "Fast Food", "Groceries", "Clothing", "Gas", "Store", "Entertainment"};
// Create first screen
JPanel startPanel = new JPanel();
startPanel.setLayout(null);
startPanel.setBounds(230, 230, 500, 250);
startPanel.setBackground(new Color(177, 177, 177));
JLabel header = new JLabel("CHOOSE AN ACTION");
header.setForeground(Color.white);
header.setFont(new Font("Verdana", Font.BOLD, 20));
header.setHorizontalAlignment(JLabel.CENTER);
header.setBounds(0, -25, 500, 150);
JButton add = new JButton("ADD");
add.setBackground(new Color(0, 109, 91));
add.setForeground(Color.white);
add.setFont(new Font("Verdana", Font.BOLD, 15));
add.setBounds(90, 125, 150, 50);
JButton view = new JButton("VIEW REPORT");
view.setBackground(new Color(167, 199, 231));
view.setForeground(Color.white);
view.setFont(new Font("Verdana", Font.BOLD, 15));
view.setBounds(270, 125, 150, 50);
startPanel.add(view);
startPanel.add(add);
startPanel.add(header);
startPanel.setVisible(false);
// Create second screen
JPanel addPanel = new JPanel();
addPanel.setBounds(230, 220, 500, 300);
addPanel.setLayout(null);
addPanel.setBackground(Color.black);
JLabel header2 = new JLabel("INPUT DATA BELOW:");
header2.setBounds(0, -25, 500, 150);
header2.setFont(new Font("Verdana", Font.BOLD, 20));
header2.setHorizontalAlignment(JLabel.CENTER);
header2.setForeground(Color.white);
JLabel costHeader = new JLabel("COST:");
costHeader.setForeground(Color.white);
costHeader.setFont(new Font("Verdana", Font.BOLD, 20));
costHeader.setBounds(90, 75, 150, 50);
JLabel typeHeader = new JLabel("TYPE:");
typeHeader.setBounds(300, 75, 150, 50);
typeHeader.setForeground(Color.WHITE);
typeHeader.setFont(new Font("Verdana", Font.BOLD, 20));
JTextField costInput = new JTextField("$0", 10);
costInput.setFont(new Font("Verdana", Font.PLAIN, 20));
costInput.setBounds(90, 150, 150, 50);
costInput.setForeground(Color.white);
JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
jComboBox.setFont(new Font("Verdana", Font.PLAIN, 20));
jComboBox.setBounds(0, 0, 150, 50);
addPanel.add(jComboBox);
addPanel.add(costInput);
addPanel.add(typeHeader);
addPanel.add(header2);
addPanel.add(costHeader);
addPanel.setVisible(true);
// Add components
window.add(startPanel);
window.add(addPanel);
}
}
Here's the Window class as well:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
this.setTitle("Finance Tracker");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
//this.setLocationRelativeTo(null);
this.setSize(1000, 800);
this.setVisible(true);
this.getContentPane().setBackground(new Color(177, 177, 177));
}
}
If anyone has any idea as to why this could be happening, any help is appreciated. Thanks!

Why after I added a JComboBox into one of the JPanels, all JPanels were not rendered

When the JComboBox, cmbox was not added to a JPanel, two panels, p1 & p2 could be rendered. You may comment out the combo box portion to see the result. But after I added the combo box into one of the panels, all panels were not rendered.
My code is like the following:
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setVisible(true);
frame.setBounds(0, 0, 1368, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
rb1.setBounds(450, 180, 50, 50);
rb2.setBounds(500, 180, 50, 50);
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btPlay.setBounds(100, 20, 100, 20);//x axis, y axis, width, height
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btStop.setBounds(140, 20, 100, 20);//x axis, y axis, width, height
//p1.add(cmbox);
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
}
}
Implementing the changes as detailed below the code, solves the problem.
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
btPlay.setBounds(100, 20, 100, 20); Do not set the bounds of components. Let the layouts (padding and borders) do their job.
frame.setBounds(0, 0, 1368, 500); That's just a guess, and if it's the right guess on one OS, it will be the wrong guess on others. Instead pack() the window after components are added.
Sidebar: GUIs should be started on the EDT. (Not implemented above: 'batteries not included'.)

JTextField not visible until mouse over

I am trying to make a simple calculator program with Java. When I added a JTextField however it made all the button and the field itself invisible until I hover over it. If I comment out the text field everything goes back to normal and all the button are visible.
Here is my code:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame {
// Numbers
JButton btn_zero;
JButton btn_one;
JButton btn_two;
JButton btn_three;
JButton btn_four;
JButton btn_five;
JButton btn_six;
JButton btn_seven;
JButton btn_eight;
JButton btn_nine;
// Operators
JButton btn_add;
JButton btn_subtract;
JButton btn_multiply;
JButton btn_divide;
JButton btn_equals;
JButton btn_decimal;
JButton btn_pm;
JButton btn_clear;
// Panel
JPanel buttonPanel;
// Dimensions
final int WIDTH = 340;
final int HEIGHT = 500;
public Calculator() {
// Characteristics of frame
super("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Insets frameInsets = getInsets();
int frameWidth = WIDTH + (frameInsets.left + frameInsets.right);
int frameHeight = HEIGHT + (frameInsets.top + frameInsets.bottom);
setPreferredSize(new Dimension(frameWidth, frameHeight));
//setLayout(null);
pack();
setVisible(true);
// Add values to all buttons
btn_zero = new JButton("0");
btn_one = new JButton("1");
btn_two = new JButton("2");
btn_three = new JButton("3");
btn_four = new JButton("4");
btn_five = new JButton("5");
btn_six = new JButton("6");
btn_seven = new JButton("7");
btn_eight = new JButton("8");
btn_nine = new JButton("9");
btn_add = new JButton("+");
btn_subtract = new JButton("-");
btn_multiply = new JButton("×");
btn_divide = new JButton("÷");
btn_equals = new JButton("=");
btn_decimal = new JButton(".");
btn_pm = new JButton("±");
btn_clear = new JButton("C");
// Adds the panel
buttonPanel = new JPanel();
buttonPanel.setSize(new Dimension(frameWidth, frameHeight));
buttonPanel.setLayout(null);
// Textfield
JTextField AnswerBox = new JTextField ("");
AnswerBox.setBounds(0, 0, 320, 70);
buttonPanel.add(AnswerBox);
//Buttons
btn_decimal.setBounds(70, 100, 50, 50);
buttonPanel.add(btn_decimal);
btn_pm.setBounds(130, 100, 50, 50);
buttonPanel.add(btn_pm);
btn_clear.setBounds(190, 100, 50, 50);
buttonPanel.add(btn_clear);
btn_add.setBounds(250, 100, 50, 50);
buttonPanel.add(btn_add);
btn_subtract.setBounds(250, 160, 50, 50);
buttonPanel.add(btn_subtract);
btn_multiply.setBounds(250, 220, 50, 50);
buttonPanel.add(btn_multiply);
btn_divide.setBounds(250, 280, 50, 50);
buttonPanel.add(btn_divide);
btn_equals.setBounds(10, 350, 290, 50);
buttonPanel.add(btn_equals);
btn_zero.setBounds(190, 160, 50, 170);
buttonPanel.add(btn_zero);
btn_one.setBounds(10, 160, 50, 50);
buttonPanel.add(btn_one);
btn_two.setBounds(70, 160 , 50, 50);
buttonPanel.add(btn_two);
btn_three.setBounds(130, 160, 50, 50);
buttonPanel.add(btn_three);
btn_four.setBounds(10, 220, 50, 50);
buttonPanel.add(btn_four);
btn_five.setBounds(70, 220, 50, 50);
buttonPanel.add(btn_five);
btn_six.setBounds(130, 220, 50, 50);
buttonPanel.add(btn_six);
btn_seven.setBounds(10, 280, 50, 50);
buttonPanel.add(btn_seven);
btn_eight.setBounds(70, 280, 50, 50);
buttonPanel.add(btn_eight);
btn_nine.setBounds(130, 280, 50, 50);
buttonPanel.add(btn_nine);
buttonPanel.setVisible(true);
add(buttonPanel);
}
}
Also the code is also right now just showing the buttons of the calculator, and not actually doing anything, because I want to focus on fixing this bug.
Call revalidate() after you add all the widgets to recalculate the layout of the container (in your case a JFrame):
buttonPanel.setVisible(true);
add(buttonPanel);
revalidate();
Calling revalidate() at the end would work or you can simply put
setVisible(true)
to the end of your constructor.

SplashScreen/Jdialog is not in center anymore in java9

I just switched to java 9 from java 8. I am using following code to build a splash screen. Splash screen comes in the center of the monitor if I use java 8, but with java 9 its not in center. Any idea, what can I do?
public void showSplashScreen() throws MalformedURLException
{
dialog = new JDialog();
dialog.setModal(false);
dialog.setUndecorated(true);
dialog.getRootPane().setOpaque(false);
dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
dialog.setBackground(new Color(0, 0, 0, 0));
URL imageURL = SomeClass.class.getClassLoader().getResource("resources/" + fImage);
JLabel background = new JLabel(new ImageIcon(imageURL));
background.setLayout(new BorderLayout());
dialog.add(background);
String versionInfo = readVersion();
JLabel text = new JLabel(versionInfo);
text.setFont(new Font("Courier New", Font.PLAIN, 14));
text.setForeground(Color.WHITE);
text.setBorder(BorderFactory.createEmptyBorder(240, 272, 150, 50));
background.add(text);
progressBar.setMaximum(PROGBAR_MAX);
dialog.add(progressBar, BorderLayout.SOUTH);
startProgressBar();
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}

Button does not create JPanel

I am new in Java and am doing some window application for my college.
I am trying to do some kind of menu with three buttons in start, and when one of buttons is clicked it's supposed to create a JPanel with two more buttons, but my code doesn't work.
Here is the code:
import java.awt.*;
public class mainScreen extends JFrame {
private JPanel contentPane;
public mainScreen() {
super("Aplikacija za atletska natjecanja");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, screenSize.width, screenSize.height);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.info);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JPanel top = new JPanel();
top.setBounds(200, 11, screenSize.width - 400, screenSize.height - (screenSize.height - 100));
contentPane.add(top);
JPanel mainMenu = new JPanel();
mainMenu.setBounds(200, 110, screenSize.width - 400, screenSize.height - (screenSize.height - 30));
contentPane.add(mainMenu);
mainMenu.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnTrkacke = new JButton("Trka\u010Dke");
btnTrkacke.setBackground(SystemColor.text);
mainMenu.add(btnTrkacke);
btnTrkacke.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel panTrk = new JPanel();
panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panTrk);
panTrk.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnTrkAtl = new JButton("Atleti\u010Dari");
btnTrkAtl.setBackground(SystemColor.text);
panTrk.add(btnTrkAtl);
JButton btnTrkDisc = new JButton("Discipline");
btnTrkDisc.setBackground(SystemColor.text);
panTrk.add(btnTrkDisc);
}
});
JButton btnSkakacke = new JButton("Skaka\u010Dke");
btnSkakacke.setBackground(SystemColor.text);
mainMenu.add(btnSkakacke);
JButton btnBacacke = new JButton("Baca\u010Dke");
btnBacacke.setBackground(SystemColor.text);
mainMenu.add(btnBacacke);
}
}
That same panel should also be created when I click on the other two buttons, but on other the position... Is it better to create class for that pane and then call it when button is clicked?
You're forgetting to call revalidate and repaint on the contentPane after changing its components:
contentPane.revalidate();
contentPane.repaint();
e.g.,
#Override
public void actionPerformed(ActionEvent e) {
JPanel panTrk = new JPanel();
panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panTrk);
panTrk.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnTrkAtl = new JButton("Atleti\u010Dari");
btnTrkAtl.setBackground(SystemColor.text);
panTrk.add(btnTrkAtl);
JButton btnTrkDisc = new JButton("Discipline");
btnTrkDisc.setBackground(SystemColor.text);
panTrk.add(btnTrkDisc);
contentPane.revalidate(); // ***** added *****
contentPane.repaint(); // ***** added *****
}
revalidate tells the container to re-lay out its components.
repaint requests to the paint manager that the component and any children should be re-drawn.
As an aside: you're using null layout manager and absolute positioning with setBounds(...), and you really don't want to do this. While to a newbie this seems the best way to create complex GUI's, the more you deal with Swing GUI creation, the more you will find that doing this will put your GUI in a straight-jacket, painting it in a very tight corner and making it very hard to extend or enhance. Just don't do this.
will ask here, coz its same project
so it shuld work like this.... 3 buttons, and under each button is hiden panel with two button, so whan some button is clicked under him shuld show panel, whan some ather button is clicked under him shuld show panel, but ather two panels shuld hide.... and this works great until some button is clicked 2 time in a row, after that whan i click some ather button panel under that button dont wont to hide..
here is the code
JPanel mainMenu = new JPanel();
mainMenu.setBounds(200, 110, screenSize.width - 400, screenSize.height - (screenSize.height - 30));
contentPane.add(mainMenu);
mainMenu.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnTrkacke = new JButton("Trka\u010Dke");
btnTrkacke.setBackground(SystemColor.text);
mainMenu.add(btnTrkacke);
btnTrkacke.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panTrk = new JPanel();
panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panTrk);
panTrk.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnTrkAtl = new JButton("Atleti\u010Dari");
btnTrkAtl.setBackground(SystemColor.text);
panTrk.add(btnTrkAtl);
JButton btnTrkDisc = new JButton("Discipline");
btnTrkDisc.setBackground(SystemColor.text);
panTrk.add(btnTrkDisc);
contentPane.revalidate();
contentPane.repaint();
panSka.setVisible(false);
panBac.setVisible(false);
}
});
JButton btnSkakacke = new JButton("Skaka\u010Dke");
btnSkakacke.setBackground(SystemColor.text);
mainMenu.add(btnSkakacke);
btnSkakacke.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panSka = new JPanel();
panSka.setBounds(201 + (screenSize.width - 400) / 3, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panSka);
panSka.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnSkaAtl = new JButton("Atleti\u010Dari");
btnSkaAtl.setBackground(SystemColor.text);
panSka.add(btnSkaAtl);
JButton btnSkaDisc = new JButton("Discipline");
btnSkaDisc.setBackground(SystemColor.text);
panSka.add(btnSkaDisc);
contentPane.revalidate();
contentPane.repaint();
panTrk.setVisible(false);
panBac.setVisible(false);
}
});
JButton btnBacacke = new JButton("Baca\u010Dke");
btnBacacke.setBackground(SystemColor.text);
mainMenu.add(btnBacacke);
btnBacacke.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panBac = new JPanel();
panBac.setBounds(201 + (screenSize.width - 400) / 3 * 2, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panBac);
panBac.setLayout(new GridLayout(1, 0, 0, 0));
JButton btnBacAtl = new JButton("Atleti\u010Dari");
btnBacAtl.setBackground(SystemColor.text);
panBac.add(btnBacAtl);
JButton btnBacDisc = new JButton("Discipline");
btnBacDisc.setBackground(SystemColor.text);
panBac.add(btnBacDisc);
contentPane.revalidate();
contentPane.repaint();
panSka.setVisible(false);
panTrk.setVisible(false);
}
});

Categories