Related
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!
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
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'.)
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);
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