Button does not create JPanel - java

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);
}
});

Related

Components in layer 1 appearing above those of layer 0 when hovering the mouse above them

In this JFrame, I added a JLayeredPane of two panels.menu,a JPanel,is added at layer 0. mainPanel is the panel on the right side ,that is added at layer 1. Why when hovering the mouse over the button of the menu ,the radiobuttons on the mainPanel (layer1) appear above the menu(layer 0).
Any solutions?
Note that Menu.moreDetails is a jlabel with an imageIcon,the menu Panel opens/closes when clicking on this jlabel.
public class HomePage {
Dimension DimMax;
JFrame homeFrame;
CardLayout card ;
JPanel mainPanel ;
static JPanel homePanel;
public void createFrame() {
DimMax = Toolkit.getDefaultToolkit().getScreenSize();
card = new CardLayout();
homeFrame = new JFrame();
homePanel = new JPanel();
mainPanel= new JPanel(card);
Menu menu = new Menu(mainPanel, card);
JLayeredPane layer = new JLayeredPane();
mainPanel.setBackground(Color.white);
menu.setBackground(new Color(200, 200, 200));
menu.setMinimumSize(new Dimension(24, DimMax.height));
//menu.setOpaque(true);
mainPanel.setOpaque(false);
mainPanel.setBounds(24, 0, DimMax.width - 24, DimMax.height);
menu.setBounds(0, 0, 24, DimMax.height);
layer.add(mainPanel, 1);
layer.add(menu, 0, 0);
homeFrame.add(layer);
layer.addMouseListener(new MouseAdapter() {
boolean open = false;
#Override
public void mousePressed(MouseEvent me) {
if (me.getSource() == Menu.moreDetails) {
if (open) {
menu.setMaximumSize(new Dimension(24, DimMax.height));
menu.setMinimumSize(new Dimension(24, DimMax.height));
menu.setPreferredSize(new Dimension(24, DimMax.height));
menu.setBounds(0, 0, 24, DimMax.height);
menu.revalidate();
open = false;
} else {
menu.setMaximumSize(new Dimension(120, DimMax.height));
menu.setMinimumSize(new Dimension(120, DimMax.height));
menu.setPreferredSize(new Dimension(120, DimMax.height));
menu.setBounds(0, 0, 120, DimMax.height);
menu.revalidate();
HomePage.homeFrame.revalidate();
HomePage.homePanel.revalidate();
open = true;
}
}
}
});
homeFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
homeFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
homeFrame.setUndecorated(true);
homeFrame.setVisible(true);
}
}

Java swing resize ImageIcon for different Windows Scaling settings

I am trying to create a small swing application using Zulu JDK 11. In the bottom right part of it I am having an image (logo). My problem is that the logo is displayed poorly when the user changes the scaling to a value greater than 100% (125%, 150%, 175% etc.)
Here is my source code.
Main class:
public class Main {
public static void main(String[] args) {
FrameServer frameServer = new FrameServer();
frameServer.setVisible(true);
}}
FrameServer class:
public class FrameServer extends JFrame {
private JPanel contentPane;
private JPanel centerPane = new JPanel();
private JPanel southPane = new JPanel();
private JPanel rightPane = new JPanel();
private JButton btnOK = new JButton("OK");
private JTabbedPane jTabbedPane1 = new JTabbedPane();
private Tab1 tab1Page = new Tab1();
private Tab2 tab2Page = new Tab2();
public FrameServer() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
jbInit();
pack();
centerFrame();
this.repaint();
}
private void jbInit() {
setTitle("Test Scaling App");
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
centerPane.setLayout(new BorderLayout());
centerPane.add(jTabbedPane1, BorderLayout.CENTER);
southPane.setLayout(new BorderLayout());
rightPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
rightPane.add(btnOK);
CustomStatusBar statusBar = new CustomStatusBar();
southPane.add(statusBar, BorderLayout.CENTER);
southPane.add(rightPane, BorderLayout.EAST);
jTabbedPane1.addTab("Tab 1", tab1Page);
jTabbedPane1.addTab("Tab 2", tab2Page);
contentPane.add(centerPane, BorderLayout.CENTER);
contentPane.add(southPane, BorderLayout.SOUTH);
}
private void centerFrame() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
class Tab2 extends JScrollPane {
public Tab2() {
super(new JTextArea(10, 60));
super.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
super.getViewport().setPreferredSize(new Dimension(500, 300));
}
}
class Tab1 extends JPanel {
public Tab1() {
contentPane = this;
JScrollPane centerScrollPane = new JScrollPane(new JTable());
centerScrollPane.getViewport().setBackground(Color.white);
JPanel eastPane = new JPanel();
eastPane.setLayout(new FlowLayout());
JPanel topPane = new JPanel();
topPane.setLayout(new BoxLayout(topPane, BoxLayout.Y_AXIS));
eastPane.add(topPane, FlowLayout.LEFT);
contentPane.setLayout(new BorderLayout());
contentPane.add(centerScrollPane, BorderLayout.CENTER);
contentPane.add(eastPane, BorderLayout.EAST);
}
}
class CustomStatusBar extends JPanel {
JLabel label0 = new JLabel("", SwingConstants.CENTER);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel("", SwingConstants.CENTER);
JLabel labelLogo = new JLabel();
private static final int HEIGHT = 21;
public CustomStatusBar() {
setCommonFeatures(label0, 40, "Stuff 0");
setCommonFeatures(label1, 120, "Stuff 1");
setCommonFeatures(label2, 120, "Stuff 2");
setCommonFeatures(label3, 90, "Stuff 3");
labelLogo.setFont(new java.awt.Font("Dialog", 3, 12));
labelLogo.setForeground(Color.black);
labelLogo.setBorder(BorderFactory.createLoweredBevelBorder());
ImageIcon image = new ImageIcon("small.png");
labelLogo.setIcon(image);
labelLogo.setPreferredSize(new Dimension(40, HEIGHT));
labelLogo.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new GridBagLayout());
setMaximumSize(new Dimension(800, 40));
add(label0, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label1, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label2, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label3, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(labelLogo, new GridBagConstraints(4, 0, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
}
private void setCommonFeatures(JLabel label, int width, String msg) {
label.setBackground(UIManager.getColor("ScrollBar.track"));
label.setForeground(Color.black);
label.setBorder(BorderFactory.createLoweredBevelBorder());
label.setPreferredSize(new Dimension(width, HEIGHT));
label.setText(" " + msg);
}
}}
Small.png image file:
When opened at scaling 100%, it looks good:
But when changing the scaling to 175%:
the pixels start to show up (open link to the picture to see better)
Now, what I have done is to replace the logo with one with a higher resolution:
Now, I am struck as I do not know how to make it fit in the space available for it and display properly when scaled. If i redimension the image from the java code as seen here, it will display properly for 100%, but for 175% scaling the pixels become visible again, like using the small.png file.
Which is the correct way of solving this?
Thank you.
The workaround is the following (only tested on Windows 10):
Locate the javaw.exe that the application is using (it very important to apply the following to javaw.exe, not java.exe)
Right click -> Properties -> Compatibility tab
Click button Change high DPI settings
Check Override high DPI scaling behavior. Scaling performed by: Select System in the combobox.
Press OK twice and restart the application

JLabel & JButton setPreferredSize not working properly

I'm building a Java GUI calculator. UI is complex and I'm doing it using Layouts instead of drag and drop. Everything is working fine except
1.mode_error_label.setPreferredSize(new Dimension(46, 55)); 2.backButton.setPreferredSize(new Dimension(45, 55));
Why is height not set to 55?
What am I missing?
Look at the top left and top right corner of the image.
My Result:
Desired Result:
public class CalculatorViewController extends JPanel {
private JTextField display1;
private JTextField display2;
private JLabel mode_error_label;
private JButton dotButton;
public CalculatorViewController() {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLACK));
mode_error_label = new JLabel("F", JLabel.CENTER);
mode_error_label.setPreferredSize(new Dimension(46, 55));
mode_error_label.setBackground(Color.YELLOW);
mode_error_label.setOpaque(true);
mode_error_label.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));
JButton backButton = new JButton(Character.toString('\u21DA'));
backButton.setPreferredSize(new Dimension(45, 55));
backButton.setBackground(Color.YELLOW);
backButton.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
backButton.setToolTipText("Backspace (Alt-B)");
display1 = new JTextField(16);
display1.setEditable(false);
display1.setHorizontalAlignment(JTextField.RIGHT);
display1.setBackground(Color.WHITE);
display1.setBorder(BorderFactory.createEmptyBorder());
display2 = new JTextField(16);
display2.setEditable(false);
display2.setHorizontalAlignment(JTextField.RIGHT);
display2.setBackground(Color.WHITE);
display2.setBorder(BorderFactory.createEmptyBorder());
display2.setText("0.0");
Box displayBox = Box.createVerticalBox();
displayBox.add(display1);
displayBox.add(display2);
Box upperBox = Box.createHorizontalBox();
upperBox.add(mode_error_label);
upperBox.add(displayBox);
upperBox.add(backButton);
upperBox.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
JCheckBox modeCheckBox = new JCheckBox("int");
modeCheckBox.setPreferredSize(new Dimension(40, 0));
modeCheckBox.setBackground(Color.green);
JRadioButton _0RadioButton = new JRadioButton(".0", false);
_0RadioButton.setBackground(Color.YELLOW);
JRadioButton _00RadioButton = new JRadioButton(".00", true);
_00RadioButton.setBackground(Color.YELLOW);
JRadioButton sciRadioButton = new JRadioButton("Sci", false);
sciRadioButton.setBackground(Color.YELLOW);
ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(_0RadioButton);
radioButtonGroup.add(_00RadioButton);
radioButtonGroup.add(sciRadioButton);
Box lowerBox = Box.createHorizontalBox();
lowerBox.add(modeCheckBox);
lowerBox.add(Box.createGlue());
lowerBox.add(_0RadioButton);
lowerBox.add(_00RadioButton);
lowerBox.add(sciRadioButton);
lowerBox.setBackground(Color.BLACK);
lowerBox.setOpaque(true);
lowerBox.setBorder(BorderFactory.createMatteBorder(5, 0, 5, 0, Color.black));
JPanel lowerPanel = new JPanel();
lowerPanel.add(lowerBox);
lowerPanel.setBackground(Color.BLACK);
lowerPanel.setOpaque(true);
lowerPanel.setBorder(BorderFactory.createEmptyBorder());
Box superBox = Box.createVerticalBox();
superBox.add(upperBox);
superBox.add(lowerBox);
this.add(superBox, BorderLayout.PAGE_START);
}
}
I searched for it but found no luck. Any help would be appreciated. Thanks
For the top bar where you have the back button and label, set the container layout a border layout. Border layout should resize the component to its container. Or you can post a code that shows the problem and we can try to solve your problem. Good luck.
public class CalculatorViewController extends JPanel {
private JTextField display1;
private JTextField display2;
private JLabel mode_error_label;
private JButton dotButton;
private JPanel topPanel;
public CalculatorViewController() {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.BLACK));
mode_error_label = new JLabel("F", JLabel.CENTER);
mode_error_label.setPreferredSize(new Dimension(46, 55));
mode_error_label.setBackground(Color.YELLOW);
mode_error_label.setOpaque(true);
mode_error_label.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));
JButton backButton = new JButton(Character.toString('\u21DA'));
backButton.setPreferredSize(new Dimension(45, 55));
backButton.setBackground(Color.YELLOW);
backButton.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
backButton.setToolTipText("Backspace (Alt-B)");
display1 = new JTextField(16);
display1.setEditable(false);
display1.setHorizontalAlignment(JTextField.RIGHT);
display1.setBackground(Color.WHITE);
display1.setBorder(BorderFactory.createEmptyBorder());
display2 = new JTextField(16);
display2.setEditable(false);
display2.setHorizontalAlignment(JTextField.RIGHT);
display2.setBackground(Color.WHITE);
display2.setBorder(BorderFactory.createEmptyBorder());
display2.setText("0.0");
Box displayBox = Box.createVerticalBox();
displayBox.add(display1);
displayBox.add(display2);
Box upperBox = Box.createHorizontalBox();
topPanel.add(mode_error_label, BorderLayout.WEST);
topPanel.add(displayBox, BorderLayout.CENTER);
topPanel.add(backButton, BorderLayout.EAST);
upperBox.add(topPanel);
upperBox.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
JCheckBox modeCheckBox = new JCheckBox("int");
modeCheckBox.setPreferredSize(new Dimension(40, 0));
modeCheckBox.setBackground(Color.green);
JRadioButton _0RadioButton = new JRadioButton(".0", false);
_0RadioButton.setBackground(Color.YELLOW);
JRadioButton _00RadioButton = new JRadioButton(".00", true);
_00RadioButton.setBackground(Color.YELLOW);
JRadioButton sciRadioButton = new JRadioButton("Sci", false);
sciRadioButton.setBackground(Color.YELLOW);
ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(_0RadioButton);
radioButtonGroup.add(_00RadioButton);
radioButtonGroup.add(sciRadioButton);
Box lowerBox = Box.createHorizontalBox();
lowerBox.add(modeCheckBox);
lowerBox.add(Box.createGlue());
lowerBox.add(_0RadioButton);
lowerBox.add(_00RadioButton);
lowerBox.add(sciRadioButton);
lowerBox.setBackground(Color.BLACK);
lowerBox.setOpaque(true);
lowerBox.setBorder(BorderFactory.createMatteBorder(5, 0, 5, 0, Color.black));
JPanel lowerPanel = new JPanel();
lowerPanel.add(lowerBox);
lowerPanel.setBackground(Color.BLACK);
lowerPanel.setOpaque(true);
lowerPanel.setBorder(BorderFactory.createEmptyBorder());
Box superBox = Box.createVerticalBox();
superBox.add(upperBox);
superBox.add(lowerBox);
this.add(superBox, BorderLayout.PAGE_START);
}
}

JButtons not appearing on JPanel

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);

JPasswordField not working properly

I don't know what's wrong with my code but the password does not turn into dots. Please help me. >.<
Here is my UI code:
private final JPanel contentPanel = new JPanel();
public JPasswordField password_id;
public JFormattedTextField user_ID;
public JButton btnAccept;
private JFormattedTextField previouslyFocusedTextBox = user_ID;
public login(final JFrame parent, boolean modal) {
super(parent, modal);
setBounds(100, 100, 469, 428);
getContentPane().setLayout(new BorderLayout());
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - getWidth()) / 2;
final int y = (screenSize.height - getHeight()) / 2;
setLocation(x, y);
setResizable(false);
FlowLayout flow_2 = new FlowLayout(0,0,0);
final JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(128, 0, 128));
panel_1.setPreferredSize(new Dimension(10, 90));
panel_1.setBounds(new Rectangle(0, 0, 100, 100));
panel_1.setBorder(new EmptyBorder(50, 90, 0, 0));
panel_1.setLayout(flow_2);
header_Login.add(panel_1, BorderLayout.SOUTH);
user_ID = new JFormattedTextField();
user_ID.setFocusTraversalKeysEnabled(true);
user_ID.setFocusable(true);
user_ID.addFocusListener(this);
user_ID.setBounds(0, 0, 423, 45);
panel_1.add(user_ID);
user_ID.setMinimumSize(new Dimension(8, 32));
user_ID.setUI(new JTextFieldHintUI("Enter ID Here", Color.gray));
user_ID.setHorizontalAlignment(SwingConstants.LEFT);
user_ID.setFont(new Font("Calibri", Font.BOLD, 35));
user_ID.setPreferredSize( new Dimension(10, 40) );
user_ID.setColumns(20);
FlowLayout flow_3 = new FlowLayout(0,0,0);
JPanel body_Login = new JPanel();
body_Login.setBackground(new Color(128, 0, 128));
body_Login.setBorder(new EmptyBorder(0, 0, 0, 0));
getContentPane().add(body_Login, BorderLayout.CENTER);
body_Login.setLayout(flow_3);
password_id = new JPasswordField ();
password_id.setFocusTraversalKeysEnabled(true);
password_id.setFocusable(true);
password_id.addFocusListener(this);
body_Login.add(password_id);
password_id.setMinimumSize(new Dimension(8, 32));
password_id.setUI(new JTextFieldHintUI("Enter PASSWORD Here", Color.gray));
password_id.setHorizontalAlignment(SwingConstants.LEFT);
password_id.setFont(new Font("Calibri", Font.BOLD, 35));
password_id.setPreferredSize( new Dimension(100, 40) );
password_id.setColumns(20);
And at the end of the code I got this:
public void focusGained(FocusEvent ev) {
//System.out.println("CHANGE");
if(ev.getSource() instanceof JFormattedTextField) {
previouslyFocusedTextBox = (JFormattedTextField) ev.getSource();
}
}
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
I know its a bit messy and I'm still new in programming the UI manually so that's why I don't know my way around. Anyway, my JPasswordField is does not turn the text into dots. I already made it work a while ago but I revert it back again thinking it wasn't working. Anyway, I can't make it work again and I really don't know what I will do to make it work. The original code for the password is:
public JFormattedTextField password_id;
password_id = new JFormattedTextField((Format) null);
When I changed my code into JPasswordField the field does not focus on the password field maybe because of the FormattedTextField in previouslyFocusedTextBox. I really don't know what I should do to make the JPasswordField to make work. This project was not really made by me that's why I don't understand some of its uses. Please help me out.
Use JPaswordField instead of JTextFormated Field

Categories