How to get instantly update from the textfield? - java

I want to change the input in the textfield which will instantly update to the display, instead of press ENTER button to update it.
Here is my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyProgram01 extends JFrame
{
private JTextField text1;
private JCheckBox check1;
private JCheckBox check2;
private String message;
private JLabel label1;
private JLabel label2;
private Font font;
public MyProgram01(String title)
{
super(title);
check1 = new JCheckBox("Bold");
check2 = new JCheckBox("Italics");
label1 = new JLabel("Text : ");
label2 = new JLabel("Style : ");
message = "Good Morning...";
text1 = new JTextField(message, 100);
font = new Font("Times New Roman", Font.PLAIN, 36);
setBounds(0, 0, 600, 300);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 600, 120);
panel.setBackground(Color.ORANGE);
label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label1.setBounds(15, 15, 100, 36);
panel.add(label1);
text1.setBounds(120, 15, 400, 36);
panel.add(text1);
label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label2.setBounds(15, 65, 100, 36);
panel.add(label2);
check1.setBounds(120, 65, 100, 36);
check2.setBounds(220, 65, 100, 36);
panel.add(check1);
panel.add(check2);
check1.addActionListener(new CheckBoxListener());
check2.addActionListener(new CheckBoxListener());
text1.addActionListener(new TextFieldListener());
setLayout(null);
add(panel);
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(font);
g.drawString(message, 15, 255);
}
private class CheckBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(check1.isSelected() && check2.isSelected())
{
font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);
repaint();
}
else if(check1.isSelected())
{
font = new Font("Times New Roman", Font.BOLD, 36);
repaint();
}
else if(check2.isSelected())
{
font = new Font("Times New Roman", Font.ITALIC, 36);
repaint();
}
else
{
font = new Font("Times New Roman", Font.PLAIN, 36);
repaint();
}
}
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
message = text1.getText();
repaint();
}
}
public static void main(String[] args)
{
JFrame frame = new MyProgram01("My Program 01");
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How can I change the code to instantly update to the display?
EDIT :
It's work with the keyListener, but my program will only start display after second key is pressed.
For example, I key in abc, the program will start show a when I press b, and when I pressed c, it displays ab and c is missing, unless I press ENTER.
Here the part of the code :
text1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
message = text1.getText();
repaint();
}
});

Add a KeyListener to your textfield.
You can do that like this:
textField.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e){
message = textField.getText();
repaint();
}
});
OR
Add a DocumentListener to your textfield's Document.
You can do that like this:
private JFrame getFrame(){
return this;
}
...
textField.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void removeUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void changedUpdate(DocumentEvent e) {
// on change
}
});

Instead of using ActionListener for the class TextFieldListener, use KeyListener interface and use the keyTyped(KeyEvent e) method. When ever the event arises you can use getText() of texfield and repaint it.

Related

JLabel not showing in painted background

I have put a background image using "paint" and as I've tried to add a Jlabel it is not showing upon running. Here's my code and screenshots.
Output
public class startingPage extends JFrame {
private JPanel contentPane;
private String audioFileName = "bg-music.wav";
private String imagePath = "truck_850x300_mining_island.png";
private String logo = "explore_with_mine.png";
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
startingPage frame = new startingPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public startingPage() {
initComponents();
createEvents();
playAudio();
}
public void playAudio() {
// TODO Auto-generated method stub
try
{
File audioPath = new File(audioFileName);
programManager.stream = AudioSystem.getAudioInputStream(audioPath);
programManager.clip = AudioSystem.getClip();
programManager.clip.open(programManager.stream);
programManager.clip.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void createEvents() {
}
public void initComponents() {
setTitle("Explore With Mine");
setIconImage(Toolkit.getDefaultToolkit().getImage(startingPage.class.getResource("/exploreWithMine/resources/logo.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(400, 200, 720, 480);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("START");
btnNewButton.setBounds(262, 299, 177, 58);
btnNewButton.setBackground(Color.LIGHT_GRAY);
btnNewButton.setForeground(new Color(165, 42, 42));
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 42));
contentPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("TESTING");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(10, 10, 146, 43);
contentPane.add(lblNewLabel);
}
public void paint(Graphics g)
{
g.drawImage(new ImageIcon(imagePath).getImage(),
-70,90, null);
}
}
OUTPUT
JLABEL not Showing (Upper Left)
I have tried removing the painted background image and it worked however I also need that background image to be there.
OUTPUT

Java Swing Application Window - Second Form showing empty

The first class or JFrame that displays just fine
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Window;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
public class ATM_Display extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JPasswordField jpass_passfield;
char[] password = new char[5]; // field for our password
private StringBuilder tempPin = new StringBuilder(); // temporary pin placeholder
private static List<Account> listOfAccounts = new ArrayList<Account>(); // list of the banks accounts
private static List<JButton> btnList = new ArrayList<JButton>();
private static OptionsMenu frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ATM_Display window = new ATM_Display();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ATM_Display() {
initialize();
frame2 = new OptionsMenu();
InstantiateAccList(listOfAccounts); // instantiate our list of accounts including clients names/pin/balance
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 508, 481);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
jpass_passfield = new JPasswordField();
jpass_passfield.setHorizontalAlignment(SwingConstants.CENTER);
jpass_passfield.setFont(new Font("Tahoma", Font.PLAIN, 30));
jpass_passfield.setToolTipText("please enter a valid 4 digit pin number using the numeric buttons below");
jpass_passfield.setEditable(false);
jpass_passfield.setBounds(114, 113, 233, 41);
frame.getContentPane().add(jpass_passfield);
//button 1
JButton btn_1 = new JButton("1");
btn_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("1"); // add to temporary placeholder
jpass_passfield.setText(GetPin()); // set the actual text of the password field
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_1.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_1.setBounds(22, 256, 89, 23);
frame.getContentPane().add(btn_1);
//button 2
JButton btn_2 = new JButton("2");
btn_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("2");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_2.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_2.setBounds(128, 256, 89, 23);
frame.getContentPane().add(btn_2);
//button 3
JButton btn_3 = new JButton("3");
btn_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("3");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_3.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_3.setBounds(229, 256, 89, 23);
frame.getContentPane().add(btn_3);
//button 4
JButton btn_4 = new JButton("4");
btn_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("4");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_4.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_4.setBounds(22, 301, 89, 23);
frame.getContentPane().add(btn_4);
//button 5
JButton btn_5 = new JButton("5");
btn_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("5");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_5.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_5.setBounds(128, 301, 89, 23);
frame.getContentPane().add(btn_5);
//button 6
JButton btn_6 = new JButton("6");
btn_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("6");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_6.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_6.setBounds(229, 301, 89, 23);
frame.getContentPane().add(btn_6);
//button 7
JButton btn_7 = new JButton("7");
btn_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("7");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_7.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_7.setBounds(22, 347, 89, 23);
frame.getContentPane().add(btn_7);
//button 8
JButton btn_8 = new JButton("8");
btn_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("8");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_8.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_8.setBounds(128, 347, 89, 23);
frame.getContentPane().add(btn_8);
//button 9
JButton btn_9 = new JButton("9");
btn_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("9");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_9.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_9.setBounds(229, 347, 89, 23);
frame.getContentPane().add(btn_9);
//button 0
JButton btn_0 = new JButton("0");
btn_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddToPassword("1");
jpass_passfield.setText(GetPin());
CheckPinLength(); // check the length of the pin SB
if(CheckPinLength()) { // check the length of the pin SB if >= 5
DisableKeypad(btnList); //disable buttons
}
}
});
btn_0.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_0.setBounds(128, 389, 89, 23);
frame.getContentPane().add(btn_0);
JButton btn_cancel = new JButton("CANCEL");
btn_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
btn_cancel.setBackground(new Color(255, 0, 0));
btn_cancel.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_cancel.setBounds(328, 256, 127, 23);
frame.getContentPane().add(btn_cancel);
JButton btn_clear = new JButton("CLEAR");
btn_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClearTempPin(); // clear our temporary pin
jpass_passfield.setText(""); // clear the password field
EnableKeypad(btnList);
}
});
btn_clear.setBackground(Color.YELLOW);
btn_clear.setForeground(Color.BLACK);
btn_clear.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_clear.setBounds(328, 301, 127, 23);
frame.getContentPane().add(btn_clear);
JButton btn_enter = new JButton("ENTER");
btn_enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isPinValid()) {
frame.setVisible(false);
frame2.setVisible(true);
}
}
});
btn_enter.setBackground(Color.GREEN);
btn_enter.setFont(new Font("Tahoma", Font.BOLD, 16));
btn_enter.setBounds(328, 347, 127, 23);
frame.getContentPane().add(btn_enter);
JLabel lblNewLabel = new JLabel("Enter Pin");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
lblNewLabel.setBounds(179, 79, 114, 23);
frame.getContentPane().add(lblNewLabel);
JLabel lblWelcomePlease = new JLabel("INVESCO ATM");
lblWelcomePlease.setForeground(new Color(0, 128, 0));
lblWelcomePlease.setFont(new Font("Tahoma", Font.BOLD, 25));
lblWelcomePlease.setBounds(144, 11, 189, 23);
frame.getContentPane().add(lblWelcomePlease);
//add buttons to our list
AddBtnsToList(btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_0);
}
// method for accessing private jpass_passfield
/*
* public void AddValToPassField(int i) { this.jpass_passfield. }
*/
// method to instantiate list of accounts
public static void InstantiateAccList(List<Account> x) {
final Account a = new Account("Henry", "Ford", 10000, 12345);
final Account b = new Account("Martha", "Stewart", 3454453, 22345);
final Account c = new Account("Cletus", "Shines", 199, 32345);
final Account d = new Account("Warren", "Scruffet", 28488, 42345);
x.add(a);
x.add(b);
x.add(c);
x.add(d);
}
//method to validate client PIN
// if pin is correct return true
// otherwise return false
public boolean isPinValid() {
for(Account y : listOfAccounts) {
if(y.getPin() == Integer.parseInt(GetPin())) {
return true;
}
}
return false;
}
// Method for adding buttons to the list of buttons
public static void AddBtnsToList(JButton a, JButton b, JButton c, JButton d, JButton e,
JButton f, JButton g, JButton h, JButton i, JButton j) {
btnList.add(a);
btnList.add(b);
btnList.add(c);
btnList.add(d);
btnList.add(e);
btnList.add(f);
btnList.add(g);
btnList.add(h);
btnList.add(i);
btnList.add(j);
}
// method for adding string to the temporary pin string we will be comparing to
public void AddToPassword(String x) {
StringBuilder s = new StringBuilder(x);
this.tempPin.append(s);
}
// gets the current pin string
public String GetPin() {
return this.tempPin.toString();
}
// clears the temporary pin placeholder
public void ClearTempPin() {
this.tempPin.setLength(0);
}
// checks to see if pin is at 5 characters
public boolean CheckPinLength() {
String temp = this.tempPin.toString();
if (temp.length() == 5) {
return true;
}
else { return false;}
}
//method to disable keypad if length is at 5
public void DisableKeypad(List<JButton> x) {
for(JButton y : x) {
y.setEnabled(false);
}
}
//method to Enable under a particular condition
public void EnableKeypad(List<JButton> x) {
for(JButton y : x) {
y.setEnabled(true);
}
}
}
And now my second class.. I'm sorry I'm a complete noob, when I run the debugger it looks like Options Menu is constructing correctly, but then it just displays as the minimize, full screen and exit button and nothing else. It will also run perfectly fine independently if I run it on it's own page. I'm really confused at this point and would appreciate any help. I have seen a lot of examples on here where the JFrame isn't set to isVisible or instantiated, but that isn't the case here. Thank you so much for any help!
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class OptionsMenu extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JTextField tb_deposit;
/**
* Launch the application.
*/
/*
* public static void main(String[] args) { EventQueue.invokeLater(new
* Runnable() { public void run() { try { OptionsMenu window = new
* OptionsMenu(); window.frame.setVisible(true); } catch (Exception e) {
* e.printStackTrace(); } } }); }
*/
/**
* Create the application.
*/
public OptionsMenu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 490, 461);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btn_balance = new JButton("1");
btn_balance.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_balance.setBounds(207, 267, 64, 31);
frame.getContentPane().add(btn_balance);
JButton btn_Withdrawal = new JButton("2");
btn_Withdrawal.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_Withdrawal.setBounds(207, 301, 64, 31);
frame.getContentPane().add(btn_Withdrawal);
JButton btn_deposit = new JButton("3");
btn_deposit.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_deposit.setBounds(207, 335, 64, 31);
frame.getContentPane().add(btn_deposit);
/// exit button
JButton btn_exit = new JButton("4");
btn_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(frame,
"Do you want to Exit ?", "Exit Confirmation : ",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.dispose();
else if (result == JOptionPane.NO_OPTION)
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
btn_exit.setFont(new Font("Tahoma", Font.BOLD, 25));
btn_exit.setBounds(207, 369, 64, 31);
frame.getContentPane().add(btn_exit);
JLabel lblNewLabel = new JLabel("BALANCE INQUIRY:");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel.setBounds(37, 267, 160, 31);
frame.getContentPane().add(lblNewLabel);
JLabel lblWithdrawal = new JLabel("WITHDRAWAL:");
lblWithdrawal.setFont(new Font("Tahoma", Font.BOLD, 15));
lblWithdrawal.setBounds(72, 309, 116, 19);
frame.getContentPane().add(lblWithdrawal);
JLabel lblDeposit = new JLabel("DEPOSIT:");
lblDeposit.setFont(new Font("Tahoma", Font.BOLD, 15));
lblDeposit.setBounds(114, 339, 74, 19);
frame.getContentPane().add(lblDeposit);
JLabel lblExit = new JLabel("EXIT:");
lblExit.setFont(new Font("Tahoma", Font.BOLD, 15));
lblExit.setBounds(140, 369, 48, 14);
frame.getContentPane().add(lblExit);
JButton btn_get20 = new JButton("$20");
btn_get20.setBounds(10, 34, 89, 23);
frame.getContentPane().add(btn_get20);
JButton btn_get40 = new JButton("$40");
btn_get40.setBounds(10, 80, 89, 23);
frame.getContentPane().add(btn_get40);
JButton btn_get60 = new JButton("$60");
btn_get60.setBounds(10, 127, 89, 23);
frame.getContentPane().add(btn_get60);
JButton btn_get100 = new JButton("$100");
btn_get100.setBounds(338, 34, 89, 23);
frame.getContentPane().add(btn_get100);
JButton btn_get200 = new JButton("$200");
btn_get200.setBounds(338, 80, 89, 23);
frame.getContentPane().add(btn_get200);
JButton btn_cancel = new JButton("CANCEL");
btn_cancel.setBounds(338, 127, 89, 23);
frame.getContentPane().add(btn_cancel);
tb_deposit = new JTextField();
tb_deposit.setBounds(157, 128, 96, 20);
frame.getContentPane().add(tb_deposit);
tb_deposit.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Enter Deposit Amount");
lblNewLabel_1.setBounds(157, 103, 114, 14);
frame.getContentPane().add(lblNewLabel_1);
}
public JFrame GetFrame2() {
return this.frame;
}
}
The problem is that your classes (ATM_Display and OptionsMenu) extend JFrame but are never really used as JFrames (you create a separate JFrame instance in each).
That means that when you make the OptionsMenu visible there is nothing to show (because you add all elements to the OptionsMenu.frame).
You should drop the extends clause:
public class ATM_Display {
// ...
}
and
public class OptionsMenu {
// ...
}
Now you will have a compile error in ATM_Display on the following line because the OptionsMenu doesn't have a setVisible() method:
frame2.setVisible(true);
To make the code work again the OptionsMenu needs this setVisible() method to make its frame visible:
public class OptionsMenu {
// ...
public void setVisible(boolean b) {
frame.setVisible(b);
}
}

Can't seem to be able to move my objects using java

I seem to be unable to move any objects I bring into my class, my other classes seem to be functional when I set bounds, but not this one.
I'm wondering why it isn't working and functionally like my other classes.
Some of the bounds are extreme as that's me making sure I can notice if any of the objects are moving; which they are not.
package student.information.in.java;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class CreatedBy extends JFrame {
JButton btnJoelHogg, btnAndrewCameron, btnConorElliman, btnTiarnanByrne, btnproject;
JLabel lblJoel, lblAndrew, lblConor, lblTiarnan, lbl_title, lbl_Info;
public CreatedBy() {
super("Project team and information");
setLayout(null);
lbl_title = new JLabel("Information about the system & its creators", JLabel.CENTER);
add(lbl_title);
lbl_title.setBounds(200, 200, 200, 200);
lbl_title.setFont(new Font("Calibri", Font.PLAIN, 30));
lbl_title.setForeground(Color.BLACK);
lblJoel = new JLabel("Joel Hogg");
add(lblJoel);
lblJoel.setBounds(30, 140, 200, 30);
lblJoel.setFont(new Font("Calibri", Font.PLAIN, 14));
lblJoel.setForeground(Color.BLACK);
btnJoelHogg = new JButton("About Joel");
btnJoelHogg.setFont(new Font("Calibri", Font.PLAIN, 14));
btnJoelHogg.setBackground(Color.LIGHT_GRAY);
btnJoelHogg.setForeground(Color.BLACK);
lblAndrew = new JLabel("Andrew Cameron");
add(lblAndrew);
lblAndrew.setBounds(80, 50, 400, 30);
lblAndrew.setFont(new Font("Calibri", Font.PLAIN, 14));
lblAndrew.setForeground(Color.BLACK);
btnAndrewCameron = new JButton("About Andrew");
btnAndrewCameron.setFont(new Font("Calibri", Font.PLAIN, 14));
btnAndrewCameron.setBackground(Color.LIGHT_GRAY);
btnAndrewCameron.setForeground(Color.BLACK);
lblJoel = new JLabel("Conor Elliman");
add(lblJoel);
lblJoel.setBounds(20, 540, 170, 380);
lblJoel.setFont(new Font("Calibri", Font.PLAIN, 14));
lblJoel.setForeground(Color.BLACK);
btnConorElliman = new JButton("About Conor");
btnConorElliman.setFont(new Font("Calibri", Font.PLAIN, 14));
btnConorElliman.setBackground(Color.LIGHT_GRAY);
btnConorElliman.setForeground(Color.BLACK);
lblTiarnan = new JLabel("TiarnĂ¡n");
add(lblTiarnan);
lblTiarnan.setBounds(202, 506, 170, 30);
lblTiarnan.setFont(new Font("Calibri", Font.PLAIN, 14));
lblTiarnan.setForeground(Color.BLACK);
btnTiarnanByrne = new JButton("About TiarnĂ¡n");
btnTiarnanByrne.setFont(new Font("Calibri", Font.PLAIN, 14));
btnTiarnanByrne.setBackground(Color.LIGHT_GRAY);
btnTiarnanByrne.setForeground(Color.BLACK);
Container c = getContentPane();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
c.setLayout(fl);
c.add(btnJoelHogg);
c.add(btnAndrewCameron);
c.add(btnConorElliman);
c.add(btnTiarnanByrne);
ButtonHandler handler = new ButtonHandler();
btnJoelHogg.addActionListener(handler);
btnAndrewCameron.addActionListener(handler);
btnConorElliman.addActionListener(handler);
btnTiarnanByrne.addActionListener(handler);
setSize(600, 400);
show();
}
public static void main(String[] args) {
// Make frame
CreatedBy f = new CreatedBy();
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Exit via windowClosing.");
System.exit(0);
}
}
);
} // end of main
// inner class for button event handling
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnJoelHogg) {
System.out.println(
"Joel Hogg button pressed");
}
if (e.getSource() == btnAndrewCameron) {
System.out.println(
"Andrew Cameron button pressed");
}
if (e.getSource() == btnTiarnanByrne) {
System.out.println("Tiarnan Byrne button pressed");
}
if (e.getSource() == btnConorElliman) {
System.out.println("Conor Elliman button pressed");
}
}
} // end of inner class
} // end of outer class

no content in Jframe java

i have two jframes in my app, Addnmember.java and Frame1.java
Frame1 is the main Jframe,
i want when someone presses the Add button, the Addmember will be shown, i have done this with this code, and every thing is fine:
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
But in the new Jframe, i want to come back again when Done Bt is selected, i have done this whit this code:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
but this doesent work! mean the Jframe Loads but there is no content in it! look:
why this happens? here is my codes:
AddMember.java:
public class AddMember extends JFrame {
private JFrame frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JButton btnNewButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddMember frame = new AddMember();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddMember() {
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 331);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u0646\u0627\u0645:");
label.setFont(new Font("Tahoma", Font.BOLD, 11));
label.setBounds(332, 60, 46, 14);
contentPane.add(label);
textField = new JTextField();
textField.setToolTipText("\u0646\u0627\u0645");
textField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
textField.setText("");
}
});
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText("\u0646\u0627\u0645");
textField.setBounds(236, 57, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("\u0634.\u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC:");
label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
label_1.setBounds(328, 103, 85, 14);
contentPane.add(label_1);
textField_1 = new JTextField();
textField_1.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_1.setText("");
}
});
textField_1.setText("\u0634\u0645\u0627\u0631\u0647 \u062F\u0627\u0646\u0634\u062C\u0648\u06CC\u06CC");
textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
textField_1.setColumns(10);
textField_1.setBounds(236, 100, 86, 20);
contentPane.add(textField_1);
JLabel label_2 = new JLabel("\u0631\u0634\u062A\u0647:");
label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
label_2.setBounds(332, 145, 46, 14);
contentPane.add(label_2);
textField_2 = new JTextField();
textField_2.setToolTipText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_2.setText("");
}
});
textField_2.setText("\u0631\u0634\u062A\u0647 \u062A\u062D\u0635\u06CC\u0644\u06CC");
textField_2.setHorizontalAlignment(SwingConstants.RIGHT);
textField_2.setColumns(10);
textField_2.setBounds(236, 142, 86, 20);
contentPane.add(textField_2);
JLabel label_3 = new JLabel("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC:");
label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
label_3.setBounds(144, 61, 82, 14);
contentPane.add(label_3);
textField_3 = new JTextField();
textField_3.setToolTipText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_3.setText("");
}
});
textField_3.setText("\u0646\u0627\u0645 \u062E\u0627\u0646\u0648\u0627\u062F\u06AF\u06CC");
textField_3.setHorizontalAlignment(SwingConstants.RIGHT);
textField_3.setColumns(10);
textField_3.setBounds(48, 58, 86, 20);
contentPane.add(textField_3);
JLabel label_4 = new JLabel("\u0634. \u062A\u0645\u0627\u0633:");
label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
label_4.setBounds(144, 104, 69, 14);
contentPane.add(label_4);
textField_4 = new JTextField();
textField_4.setToolTipText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_4.setText("");
}
});
textField_4.setText("\u0634\u0645\u0627\u0631\u0647 \u062A\u0645\u0627\u0633");
textField_4.setHorizontalAlignment(SwingConstants.RIGHT);
textField_4.setColumns(10);
textField_4.setBounds(48, 101, 86, 20);
contentPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setToolTipText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_5.setText("");
}
});
textField_5.setText("\u0633\u0627\u0644 \u0648\u0631\u0648\u062F");
textField_5.setHorizontalAlignment(SwingConstants.RIGHT);
textField_5.setColumns(10);
textField_5.setBounds(48, 140, 86, 20);
contentPane.add(textField_5);
JLabel label_5 = new JLabel("\u0648\u0631\u0648\u062F\u06CC:");
label_5.setFont(new Font("Tahoma", Font.BOLD, 11));
label_5.setBounds(144, 143, 46, 14);
contentPane.add(label_5);
textField_6 = new JTextField();
textField_6.setToolTipText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textField_6.setText("");
}
});
textField_6.setHorizontalAlignment(SwingConstants.RIGHT);
textField_6.setText("\u0636\u0631\u0648\u0631\u06CC \u0646\u06CC\u0633\u062A");
textField_6.setColumns(10);
textField_6.setBounds(131, 187, 86, 20);
contentPane.add(textField_6);
JLabel label_6 = new JLabel("\u0627\u06CC\u0645\u06CC\u0644:");
label_6.setFont(new Font("Tahoma", Font.BOLD, 11));
label_6.setBounds(227, 190, 46, 14);
contentPane.add(label_6);
btnNewButton = new JButton("Done");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//dispose();
Frame1 addmem = new Frame1();
addmem.setVisible(true);
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(171, 227, 89, 23);
contentPane.add(btnNewButton);
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
And Frame1.java:
public class Frame1 extends JFrame {
Connection connection = null;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
private JTable table;
public Frame1() {
JOptionPane.showMessageDialog(null, "sazande :|");
this.initialize();
connection = DataBConect.dbConnect();
try {
String query = "select * from Users";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JOptionPane.showMessageDialog(null, "intialize :|");
frame.setTitle("App");
frame.setBounds(100, 100, 725, 465);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton AddB = new JButton("Add");
AddB.setBackground(Color.WHITE);
AddB.setFont(new Font("Tahoma", Font.BOLD, 11));
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
AddMember addmem = new AddMember();
addmem.setVisible(true);
}
});
AddB.setBounds(302, 392, 135, 23);
frame.getContentPane().add(AddB);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 26, 658, 359);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
}
}
thanks.
As to why your second Frame1 shows no data, I'm not 100% sure, but perhaps it has something to do with your failure to close your database connection. Regardless, it seems wasteful and even potentially dangerous to want to gather the database data twice when you've already obtained it once, and to create your Frame1 GUI twice when you've already created it once.
You should re-design your GUI. The main JFrame GUI should remain visible, and the Frame1 window should be a modal JDialog. As a modal dialog, it will prevent users from being able to interact with the parent JFrame until the dialog is no longer visible.
Note that to get started requires only a few changes:
in Frame1.java
AddB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// frame.dispose();
AddMember addmem = new AddMember(Frame1.this); // !!
addmem.setVisible(true);
}
});
in AddMember.java
class AddMember extends JDialog {
public AddMember(JFrame frame) {
super(frame, "", ModalityType.APPLICATION_MODAL);
setTitle("\u0627\u0641\u0632\u0648\u062F\u0646 \u0639\u0636\u0648 \u062C\u062F\u06CC\u062F");
// !! setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dispose();
dispose(); // !!
// !! Frame1 addmem = new Frame1();
// addmem.setVisible(true);
}
});
Also, you'll want to avoid null layouts and setBounds(...) as this will lead to very rigid, hard to debug and improve programs.

Calling JPanel from another JPanel

I know this is ill advised but I am using absolute layout and trying to go from panel to panel and was curious how to do this.
public class Mainscreen extends JFrame {
private JPanel Home;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
final Dataentrylog DEL = new Dataentrylog();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home = new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setContentPane(Home);
Home.setLayout(null);
JButton btnNewButton = new JButton("Data Entry login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
setLayout(null);
}
});
btnNewButton.setBounds(44, 214, 204, 61);
Home.add(btnNewButton);
}
}
Calling this JPanel which works
import java.awt.EventQueue;
public class Dataentrylog extends JPanel {
public Dataentrylog() {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
DEadmin DEA = new Deadmin();
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
While this works if I try an call another JPanel from Dataentry log the JFrame is blank. What can I do to call another JPanel? Any help would be great. Also, I know that layoutmanagers are the norm but for what I had to do I wasn't able to find anything that worked so I choose to use null despite my best judgement. THANKS!
Intialization of Dataentrylog should be like this
final Dataentrylog DEL = new Dataentrylog(this);
we have to pass the parent jframe.
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent ) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JPanel panel = new JPanel();
panel.add(new JButton("Solved"));
parent.setContentPane(panel);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}

Categories

Resources