Calling JPanel from another JPanel - java

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

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

How to change theme by FlatLaf on days buttons in JDateChooser (JCalendar)?

I'm trying to change the theme using FlatLaf (2.0.1) on a JDateChooser (JCalendar 1.4) object. I don't know why the day buttons don't refresh.
Here is my code:
public class Test extends JFrame {
private JPanel contentPane;
private JDateChooser dateChooser;
private JToggleButton bChangeTheme;
public static void main(String[] args) {
FlatArcDarkOrangeIJTheme.setup();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
setFont(new Font("Verdana", Font.PLAIN, 12));
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 95);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[][grow]", "[grow]"));
bChangeTheme = new JToggleButton("Change theme");
bChangeTheme.setFont(new Font("Verdana", Font.PLAIN, 12));
bChangeTheme.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(bChangeTheme.isSelected()) {
FlatArcOrangeIJTheme.setup();
FlatLaf.updateUI();
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
} else {
FlatArcDarkOrangeIJTheme.setup();
FlatLaf.updateUI();
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
}
}
});
contentPane.add(bChangeTheme, "cell 0 0,growy");
dateChooser = new JDateChooser();
dateChooser.setDateFormatString("yyyy-MM-dd");
dateChooser.setFont(new Font("Verdana", Font.PLAIN, 12));
dateChooser.getCalendarButton().setFont(new Font("Verdana", Font.PLAIN, 12));
dateChooser.getJCalendar().getDayChooser().setDecorationBackgroundVisible(false);
dateChooser.getJCalendar().getDayChooser().setSundayForeground(Color.decode("#f57900"));
dateChooser.getJCalendar().getDayChooser().setWeekdayForeground(Color.decode("#f57900"));
dateChooser.getJCalendar().getYearChooser().setFont(dateChooser.getFont());
dateChooser.getJCalendar().getDayChooser().setForeground(dateChooser.getJCalendar().getMonthChooser().getForeground());
dateChooser.getJCalendar().setFont(dateChooser.getFont());
contentPane.add(dateChooser, "cell 1 0,grow");
}
}
I've been trying to get to the buttons like this:
dcDo.getJCalendar().getDayChooser().getDayPanel().getComponents();
Failed to change the background of the buttons in a loop.
I also tried to find a similar trick to the one used to set the font.
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
But no success...
I will be grateful for all suggestions.

How to switch between two JPanels without CardLayout?

I build a game in swing and I have two JPanels for menu and the game itself. I want to switch from the menu panel to the game panel and from the game to the menu using JButtons. but every time I switch from the menu to the game, I want to start over the game and send the name of the player from some textField in the menu panel. so I need to make now instance of the game panel. How can I do this?
Here's the menu panel:
public class MenuPanel extends JPanel {
private JPanel contentPane;
private JTextField txtName;
public MenuPanel(JPanel panel) {
setLayout(null);
contentPane = panel;
JButton btnStart = new JButton("Click to start the game");
btnStart.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Game Panel");
}
});
btnStart.setFont(new Font("Baskerville Old Face", btnStart.getFont().getStyle(), 25));
btnStart.setBounds(290, 510, 290, 70);
add(btnStart);
txtName = new JTextField();
txtName.setBounds(450, 460, 160, 20);
add(txtName);
txtName.setColumns(10);
JLabel lblName = new JLabel("Enter your name:");
lblName.setForeground(SystemColor.text);
lblName.setFont(new Font("Bradley Hand ITC", Font.BOLD, 20));
lblName.setBounds(260, 460, 180, 20);
add(lblName);
JLabel lblGame = new JLabel("RUMMIKUB GAME");
lblGame.setForeground(new Color(0, 0, 0));
lblGame.setHorizontalAlignment(SwingConstants.CENTER);
lblGame.setFont(new Font("Goudy Old Style", lblGame.getFont().getStyle(), 50));
lblGame.setBounds(180, 100, 500, 140);
add(lblGame);
}
}
Here's the game panel:
public class GamePanel extends JPanel {
private JPanel contentPane;
private JButton btnReturnToMenu;
private TileButton[][] buttons = new TileButton[Constants.ROWS_IN_PLAYER_BOARD][Constants.COLUMNS_IN_PLAYER_BOARD];
private JLabel lblHello;
private TileButton btnThrownByPlayer;
private TileButton btnThrownByOpponent;
private Point clickedButton;
private JButton btnPullFromPool;
private JButton btnFinish;
private Game game;
public GamePanel(String firstPlayerName, String secondPlayerName, JPanel panel) {
setLayout(null);
contentPane = panel;
clickedButton = null;
game = new Game(firstPlayerName, secondPlayerName);
lblHello = new JLabel("hello " + firstPlayerName + " and " + secondPlayerName);
lblHello.setBounds(10, 10, 250, 40);
add(lblHello);
//initiate exit button.
btnReturnToMenu = new JButton("Return To Menu");
btnReturnToMenu.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Menu Panel");
}
});
btnReturnToMenu.setFont(new Font("Mongolian Baiti", Font.BOLD, 16));
btnReturnToMenu.setBounds(10, 70, 200, 40);
add(btnReturnToMenu);
// initiate finish button.
btnFinish = new JButton("Finish");
btnFinish.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (btnFinish.isEnabled()) {
if (game.finish()) {
JOptionPane.showMessageDialog(null,
"You finish the game.\nCongratulations, you are the winner!!!");
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, "Finish Panel");
} else {
btnFinish.setEnabled(false);
JOptionPane.showMessageDialog(null, "You didn't finish the game yet");
}
}
}
});
btnFinish.setFont(new Font("Mongolian Baiti", Font.BOLD, 16));
btnFinish.setBounds(750, 50, 100, 40);
add(btnFinish);
}
}
Here's the frame:
public class MainFrame extends JFrame {
private JPanel contentPane;
private GamePanel gamePanel;
private MenuPanel menuPanel;
private FinishPanel finishPanel;
private MainFrame() {
//super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(233, 55, 900, 627);
setResizable(false);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new CardLayout());
menuPanel = new MenuPanel(contentPane);
gamePanel = new GamePanel("", "", contentPane);
finishPanel = new FinishPanel(contentPane);
contentPane.add(menuPanel, "Menu Panel");
contentPane.add(gamePanel, "Game Panel");
contentPane.add(finishPanel, "Finish Panel");
getContentPane().add(contentPane, BorderLayout.CENTER);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
Thanks for the help!

Position Not Moving Despite of Key Listener

I made the Card Layout Working completely fine but what happened now is I added a keylistener to the object character which is a JLabel so whenever the person presses up the character should move up but it does absolutely nothing!
I also tried replacing it with a button which moves it when clicked and it worked completely fine. Also I tried changing the event meaning I changed that if they press up then the image of the town map would change but still no effect so it seems there is something wrong with my KeyListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import javax.swing.*;
#SuppressWarnings({ "unused", "serial" })
public class FinalBlowzXC extends JFrame implements KeyListener{
public static JPanel game=new JPanel();
public static JPanel mainmenu=new JPanel(null);
public static JPanel loginpanel=new JPanel(null);
public static JPanel tutorial=new JPanel(null);
public static JPanel registration=new JPanel(null);
public static JPanel town_map=new JPanel(null);
public JTextField username= new JTextField("Username");
public JTextField password=new JTextField("Password");
public JLabel bglogin=new JLabel();
public JLabel character=new JLabel();
public JButton log_in=new JButton();
public JButton register=new JButton();
CardLayout page= new CardLayout();
public String level="";
public int charx=350;
public int chary=435;
public static void main(String []args)
{
new FinalBlowzXC().setVisible(true);
}
public FinalBlowzXC()
{
super("Final Blowz Xchanged");
setSize(640,510);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setLayout(page);
game.add(mainmenu, "1");
game.add(loginpanel, "2");
game.add(tutorial, "3");
game.add(registration, "4");
game.add(town_map, "5");
add(game);
opening();
}
public void opening()
{
page.show(game, "1");
JLabel bgmainmenu;
JButton start;
JButton exit;
bgmainmenu = new JLabel();
start = new JButton();
exit = new JButton();
bgmainmenu.setIcon(new ImageIcon(getClass().getResource("/FF-XV.jpg")));
bgmainmenu.setBounds(0,0,640,480);
mainmenu.add(bgmainmenu);
mainmenu.add(start);
start.setBounds(280, 360, 70, 20);
start.setBorder(null);
start.setBorderPainted(false);
start.setContentAreaFilled(false);
start.setOpaque(false);
start.addActionListener(new Start());
exit.setBounds(280, 385, 70, 20);
mainmenu.add(exit);
exit.setBorder(null);
exit.setBorderPainted(false);
exit.setContentAreaFilled(false);
exit.setOpaque(false);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
class Start implements ActionListener{
public void actionPerformed(ActionEvent e) {
login();
}
}
public void login()
{
page.show(game, "2");
bglogin.setIcon(new ImageIcon(getClass().getResource("/FF-XV Login.jpg")));
bglogin.setBounds(0, 0, 640, 480);
username.setBounds(300, 285, 85, 15);
username.setBorder(null);
username.setForeground(Color.WHITE);
username.setOpaque(false);
password.setBounds(300, 310, 85, 20);
password.setBorder(null);
password.setForeground(Color.WHITE);
password.setOpaque(false);
log_in.setBounds(280, 335, 50, 45);
log_in.setBorder(null);
log_in.setBorderPainted(false);
log_in.setContentAreaFilled(false);
log_in.setOpaque(false);
log_in.addActionListener(new Log_in());
register.setBounds(335, 335, 55, 45);
register.setBorder(null);
register.setBorderPainted(false);
register.setContentAreaFilled(false);
register.setOpaque(false);
register.addActionListener(new Register());
loginpanel.add(username);
loginpanel.add(password);
loginpanel.add(bglogin);
loginpanel.add(log_in);
loginpanel.add(register);
}
class Log_in implements ActionListener{
public void actionPerformed(ActionEvent e)
{
Tutorial();
}
}
class Register implements ActionListener{
public void actionPerformed(ActionEvent e)
{
page.show(game, "4");
}
}
public void Tutorial()
{
page.show(game, "3");
JLabel bgtutorial=new JLabel();
JLabel animeforward=new JLabel();
JLabel animeright=new JLabel();
JLabel animeleft=new JLabel();
JButton next=new JButton();
JLabel animebackward=new JLabel();
bgtutorial.setIcon(new ImageIcon(getClass().getResource("/FF-XV Tutorial.jpg")));
bgtutorial.setBounds(0, 0, 640, 480);
animeforward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
animeforward.setBounds(115, 230, 30, 30);
animeright.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
animeright.setBounds(190, 315, 30, 30);
animeleft.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
animeleft.setBounds(45, 315, 30, 30);
animebackward.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
animebackward.setBounds(115, 405, 30, 30);
next.setBounds(530, 430, 100, 30);
next.setBorder(null);
next.setBorderPainted(false);
next.setContentAreaFilled(false);
next.setOpaque(false);
next.addActionListener(new Next());
tutorial.add(next);
tutorial.add(animeright);
tutorial.add(animeleft);
tutorial.add(animebackward);
tutorial.add(animeforward);
tutorial.add(bgtutorial);
}
class Next implements ActionListener{
public void actionPerformed(ActionEvent e)
{
town();
}
}
public void town()
{
page.show(game, "5");
JLabel map=new JLabel();
map.setIcon(new ImageIcon(getClass().getResource("/FF-XV Town.jpg")));
map.setBounds(0, 0, 640, 480);
character.setIcon(new ImageIcon(getClass().getResource("standfront.png")));
character.setBounds(charx, chary, 30, 35);
town_map.add(character);
town_map.add(map);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialfront.gif")));
character.setLocation(character.getX(), character.getY()+5);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialright.gif")));
character.setLocation(character.getX()+5, character.getY());
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialleft.gif")));
character.setLocation(character.getX()-5, character.getY()+5);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
character.setIcon(new ImageIcon(getClass().getResource("walkanimofficialback.gif")));
character.setLocation(character.getX(), character.getY()-5);
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
Add following code in your programme.
public static JPanel mainmenu = new JPanel();
public static JPanel loginpanel = new JPanel();
or initialize mainmenu & loginpanel before adding them into the game panel.
when you call game.add(loginPanel, "2") loginPanel is null

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.

Categories

Resources