I have a Java app and a JDateChooser. The problem comes when I start my app.
The JDateChooser remains invisible and appears only after I iconify my app.
What I should do?
dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(48, 68, 102, 20);
JTextFieldDateEditor editor = (JTextFieldDateEditor) dateChooser.getDateEditor();
editor.setEditable(false);
frmCodfiscextractor.getContentPane().add(dateChooser);
Also, I put this at the end of the class :
frmCodfiscextractor.repaint();
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.alee.laf.WebLookAndFeel;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class demo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
demo window = new demo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public demo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(74, 193, 181, 30);
JTextFieldDateEditor editor=(JTextFieldDateEditor)dateChooser.getDateEditor();
editor.setEditable(false);
frame.getContentPane().add(dateChooser);
frame.repaint();
}
}
Use latest version of JCalender. It is correct working in my case.
Related
I am a newbie in Java GUI development and I am stuck in the following code.
I am open to suggestions. I am actually trying to create a simple login that gives OK if the password is matched to the number 3124, and otherwise shows the error message. Please help me.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testing window = new testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
final int num;
num=Integer.parseInt(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(num==3124)
{JOptionPane.showMessageDialog(null, "correct");}
else
{JOptionPane.showMessageDialog(null, "wrong");}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}
You were checking the password even before the user has had the chance to enter anything into the text box. You need to get and check the value of num in the event listener code i.e. in actionPerformed. Also, don't convert the password to int (someone may enter some non-numeric string).
This code below works better.
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class Testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Testing window = new Testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String num;
num = (password.getText());
if (num.equalsIgnoreCase("3124")) {
JOptionPane.showMessageDialog(null, "correct");
} else {
JOptionPane.showMessageDialog(null, "wrong");
}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}
I am try to run the progress bar in my frame but it is not working. I am tried to display the visible in my second java class but set visible(true) displaying it as error.
Hope you guys can help me to solve my problem/error
Displaying error in my second java class:
"Exception in thread "main"
java.lang.Error: Unresolved compilation problem: The method
setvisible(boolean) is undefined for the type mgfinancewindow"
First java class: mgfinancewindow.java
package mgfinance;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class mgfinancewindow {
private JFrame frame;
public JProgressBar progressBar;
public JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mgfinancewindow window = new mgfinancewindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mgfinancewindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("MG Finances");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1362, 705);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.DARK_GRAY);
panel_1.setBounds(0, 646, 1362, 59);
panel.add(panel_1);
panel_1.setLayout(null);
lblNewLabel = new JLabel("Loading...");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setBounds(1139, 0, 114, 34);
panel_1.add(lblNewLabel);
progressBar = new JProgressBar();
progressBar.setBackground(new Color(0, 51, 51));
progressBar.setBounds(0, 34, 1362, 14);
panel_1.add(progressBar);
JLabel lblMgFinance = new JLabel("MG Finance");
lblMgFinance.setHorizontalAlignment(SwingConstants.CENTER);
lblMgFinance.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblMgFinance.setForeground(Color.BLUE);
lblMgFinance.setBounds(0, 11, 1362, 635);
panel.add(lblMgFinance);
}
}
second java class: progressbar.java
package mgfinance;
public class progress {
public static void main(String[] args) throws InterruptedException{
mgfinancewindow load = new mgfinancewindow();
for(int i=0; i<=100; i++){
Thread.sleep(150);
load.setvisible(true);
load.lblNewLabel.setText("Loading..."+ i);
load.progressBar.setValue(i);
}
}
}
Your mgfinancewindow class is not a JComponent to support the setVisible() method itself. The JFrame inside it has the setVisible method.
In the way you wrote the code, to solve your problem quickly you must write the frame.setVisible(true); at the end (last statement) of initialize() method in mgfinancewindow class and remove load.setvisible(true); from the main method of progress class:
public class progress {
public static void main(String[] args) throws InterruptedException {
mgfinancewindow load = new mgfinancewindow();
for (int i = 0; i <= 100; i++) {
Thread.sleep(150);
//load.setvisible(true);
load.lblNewLabel.setText("Loading..." + i);
load.progressBar.setValue(i);
}
}
}
and
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
public class mgfinancewindow {
private JFrame frame;
public JProgressBar progressBar;
public JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mgfinancewindow window = new mgfinancewindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mgfinancewindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("MG Finances");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1362, 705);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.DARK_GRAY);
panel_1.setBounds(0, 646, 1362, 59);
panel.add(panel_1);
panel_1.setLayout(null);
lblNewLabel = new JLabel("Loading...");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setBounds(1139, 0, 114, 34);
panel_1.add(lblNewLabel);
progressBar = new JProgressBar();
progressBar.setBackground(new Color(0, 51, 51));
progressBar.setBounds(0, 34, 1362, 14);
panel_1.add(progressBar);
JLabel lblMgFinance = new JLabel("MG Finance");
lblMgFinance.setHorizontalAlignment(SwingConstants.CENTER);
lblMgFinance.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblMgFinance.setForeground(Color.BLUE);
lblMgFinance.setBounds(0, 11, 1362, 635);
panel.add(lblMgFinance);
frame.setVisible(true);
}
}
But:
When you are creating a component like mgfinancewindow which creates a JFrame inside, most of the times it's better to extend from JFrame and then you can create an object from it and call the setVisible method on it in the main method of your program or so. It's better not to call setVisible inside that component, because in another classes sometimes you want to create and initialize the mgfinancewindow but you don't want to make it visible immediately.
Another hints:
According to Java Coding Conventions:
Your class names must follow the CamelCase style.
Variable names must follow the camelCase (first letter in lower-case) style.
You may want to take a look at other java coding conventions here.
i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
package asdf;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JLabel;
public class asd extends JFrame implements ActionListener {
int a=0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
***JLabel Jtable = new JLabel();
Jtable.setBounds(0, 25, 127, 58);
Jtable.setText("" + a);
panel.add(Jtable);***
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
initialize();
}
}
i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
You shouldn't be initializing a new frame and all the components on every action performed, you should only be updating the text of the label. You can do this via JLabel.setText https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setText(java.lang.String)
int a = 0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private JLabel label;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
label = new JLabel();
label.setBounds(0, 25, 127, 58);
label.setText("" + a);
panel.add(label);
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
label.setText("" + a);
}
I built a Java-Swing GUI using the WindowBuilder in Eclipse. But when I try to add new components using the .add() and .revalidate() nothing happens.
If someone could help to fix this issue I realy would apreciate it.
package Frame;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.add(new JButton("BTN"));
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}
Try following:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = new JButton("BTN");
btn.setSize(btn.getPreferredSize());
btn.setLocation(new Point(1, 1));
frame.add(btn);
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}
Try to learn Layout Managers. When you use an appropriate layout manager you shouldn't set the component's size/location.
I want to add a vertical scroll-bar on my JFrame with null layout.
Is it possible or not? please help!
Just set the JScrollPane as ContentPane for JFrame as it is described here:
public class TabbedPaneTest {
public static void main(String [] a) {
final JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.setContentPane(pane);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
}
}
In eclipse IDE you can use following code
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane jsp = new JScrollPane(container);
container.setPreferredSize(new Dimension(500, 250));
container.setLayout(null);
JLabel lblHelloWorld = new JLabel("Hello World");
lblHelloWorld.setBounds(10, 11, 101, 14);
container.add(lblHelloWorld);
frame.getContentPane().add(jsp);
}
}