Open a JFrame into another JFrame - java

I am creating a program, and I have my main class that works with all the JButtons, but I can't get my second class that calls the first class launch with the buttons. The JFrame will launch, but the buttons won't.
I am using eclipse just for the information.
Here is the code of my main class:
public class Unescapable extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JButton b3;
protected JButton b5;
protected JTextField t1;
public Unescapable()
{
t1 = new JTextField("Unescapable");
t1.setText("Unescapable");
t1.setBounds(225, 50, 750, 100);
t1.setForeground(Color.LIGHT_GRAY);
t1.setOpaque(true);
t1.setVisible(true);
t1.setEditable(false);
t1.setBackground(Color.BLACK);
t1.setFont(font1);
b1 = new JButton("Open Window");
b1.setActionCommand("open");
b1.setBackground(Color.GRAY);
b1.setForeground(Color.white);
b1.setOpaque(true);
b1.setBorderPainted(false);
b1.setBounds(280, 200, 390, 40);
b1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b1.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b1.setBackground(Color.GRAY);
}
});
b2 = new JButton("Delete File");
b2.setActionCommand("delete");
b2.setBackground(Color.GRAY);
b2.setForeground(Color.white);
b2.setOpaque(true);
b2.setBorderPainted(false);
b2.setBounds(280, 255, 390, 40);
b2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b2.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b2.setBackground(Color.GRAY);
}
});
b3 = new JButton("Info...");
b3.setActionCommand("info");
b3.setBackground(Color.GRAY);
b3.setForeground(Color.white);
b3.setOpaque(true);
b3.setBorderPainted(false);
b3.setBounds(278, 330, 185, 40);
b3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b3.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b3.setBackground(Color.GRAY);
}
});
b5 = new JButton("Quit Game");
b5.setActionCommand("close");
b5.setBackground(Color.GRAY);
b5.setForeground(Color.white);
b5.setOpaque(true);
b5.setBorderPainted(false);
b5.setBounds(485, 330, 185, 40);
b5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b5.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b5.setBackground(Color.GRAY);
}
});
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b5.addActionListener(this);
b1.setToolTipText("Opens Another JWindow");
b2.setToolTipText("Deletes \"text.txt\"");
b3.setToolTipText("Give's some information.");
add(b1);
add(b2);
add(b3);
add(b5);
add(t1);
System.out.println("Main Window Is Running");
}
public void actionPerformed(ActionEvent e)
{
if ("open".equals(e.getActionCommand()))
{
File f = new File("text.txt");
try
{
PrintWriter out = new PrintWriter(f);
out.println("TheBestMacTutorials");
out.close();
}
catch (Exception e2)
{
}
}
else if ("delete".equals(e.getActionCommand()))
{
File f = new File("text.txt");
f.delete();
}
else if ("info".equals(e.getActionCommand()))
{
InfoBook add = new InfoBook();
add.call();
}
else
{
System.out.println("Window Is Now Closing");
System.exit(0);
}
}
private static void createAndShowGUI()
{
JFrame program = new JFrame("My Program");
program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Unescapable newContentPane = new Unescapable();
program.setContentPane(newContentPane);
program.setLayout(null);
program.setVisible(true);
program.setLocation(850, 445);
program.setSize(900, 580);
program.setTitle("Unescapable 1.0");
program.setBackground(Color.GREEN);
program.isOpaque();
program.isForegroundSet();
program.getContentPane().setBackground(Color.BLACK);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
The code for the "Info Book":
public class InfoBook extends JFrame
{
private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JTextField t1;
public InfoBook()
{
b1 = new JButton("Cancel");
b1.setActionCommand("cancel");
b1.setBackground(Color.GRAY);
b1.setForeground(Color.white);
b1.setOpaque(true);
b1.setBorderPainted(false);
b1.setBounds(280, 200, 390, 40);
b1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b1.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b1.setBackground(Color.GRAY);
}
});
b2 = new JButton("More Info");
b2.setBackground(Color.GRAY);
b2.setForeground(Color.WHITE);
b2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
b2.setBackground(Color.BLUE);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
b2.setBackground(Color.GRAY);
}
});
t1 = new JTextField("Info");
t1.setText("Info...");
t1.setBounds(225, 50, 750, 100);
t1.setForeground(Color.LIGHT_GRAY);
t1.setOpaque(true);
t1.setVisible(true);
t1.setEditable(false);
t1.setBackground(Color.BLACK);
t1.setFont(font1);
b1.setToolTipText("Opens Another JWindow");
b2.setToolTipText("Gives More Infomation");
add(b1);
add(b2);
add(t1);
System.out.println("Info is now running");
}
public static void creatAndShowGUI()
{
JFrame frame = new JFrame("Info Panel");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
InfoBook newContentPane = new InfoBook();
frame.setLayout(null);
frame.setVisible(true);
frame.setLocation(850, 445);
frame.setSize(900, 580);
frame.setTitle("Unescapable 1.0");
frame.setBackground(Color.GREEN);
frame.isOpaque();
frame.isForegroundSet();
frame.getContentPane().setBackground(Color.BLACK);
}
public static void call()
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
creatAndShowGUI();
}
});
}
}
I believe thats all the code, and I am not very used to how to format the code in stack overflow, so I might of messed something up. I am probably just getting something messed up, so i am sorry for that but thanks in advance.

The main problem is in your createAndShowGUI in your InfoBook class...
public static void creatAndShowGUI() {
JFrame frame = new JFrame("Info Panel");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
InfoBook newContentPane = new InfoBook();
frame.setLayout(null);
frame.setVisible(true);
frame.setLocation(850, 445);
frame.setSize(900, 580);
frame.setTitle("Unescapable 1.0");
frame.setBackground(Color.GREEN);
frame.isOpaque();
frame.isForegroundSet();
frame.getContentPane().setBackground(Color.BLACK);
}
Essentially, you create an instance of newContentPane, but you never use it.
But, you're about to run into a second problem...
} else if ("info".equals(e.getActionCommand())) {
InfoBook add = new InfoBook();
add.call();
Here you create an instance of InfoBook, but this instance will have no relationship to the one that is created in your creatAndShowGUI and which I assume you want to show on the screen, so getting information from the class will be impossible
I suspect, you actually want to use a JDialog of some kind instead, which will allow you to present a window to the user, BUT which will block the execution of the code until the user closes the window, at which time you could then interrogate the object for it's information.
See How to Make Dialogs for more details
As a general rule of thumb, you don't want to extend from top level containers like JFrame or JDialog, instead, you want to use a JPanel as the base component and add these to what ever container you need.
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
You might also like to have a look at The Use of Multiple JFrames, Good/Bad Practice?

Related

Press JButton to draw a ball, the ball is not visible

So, I want to draw a ball whenever the user presses JButton. My problem is that the ball is not visible after I call revalidate() and repaint(). Am I forgetting something?
Here is my code, I have another class for the queue and stack that's why I extended Queueue. My buttons are visible and I know they work when I press them, it's jst that the ball doesn't show up on the screen. Earlier I tried to have my void paintComponent in my ActionListener but it would not work. I then wanted to just call the method but because of the Graphics g parameter it would not work as well. So I saw similar issue where someone suggested to use boolean
public class Main extends Queueue {
static boolean clicked = false;
public void paintComponent(Graphics g) {
if(clicked) {
g.setColor(Color.BLACK);
g.fillOval(60, 60, 15, 15);
}
}
public static void main (String[] args) {
Queueue qq = new Queueue();
JFrame f = new JFrame();
JPanel p = new JPanel();
JButton b1 = new JButton("Queue");
JButton b2 = new JButton("Stack");
JButton b3 = new JButton("Ball");
f.setSize(700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setBackground(Color.RED);
p.add(b1);
p.add(b2);
p.add(b3);
f.add(p);
f.setVisible(true);
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
qq.Q();
}
});
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
qq.S();
}
});
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
clicked = true;
f.revalidate();
f.repaint();
}
});
You had some syntax errors which I corrected. I also trimmed it down to more clearly demonstrate the procedure. Here is a working version that just paints the ball.
public class Main extends JPanel {
static boolean clicked = false;
public static void main(String[] args) {
JFrame f = new JFrame();
Main m = new Main();
JButton b3 = new JButton("Ball");
f.setSize(700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(m);
m.add(b3);
f.setVisible(true);
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
clicked = true;
f.repaint();
}
});
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (clicked) {
g.setColor(Color.BLACK);
g.fillOval(60, 60, 15, 15);
}
}
}
Your class should extend JPanel
Add it to the JFrame.
Add the JButton to Main instance
and in paintComponent call super.paintComponent(g) first.
Check out The Java Tutorials for more information on how to paint.
In response to your comments, the following should work. The main issue is that you need to have the paintComponent inside a JPanel, not as a method in Queueue.
public class Main extends Queueue {
static boolean clicked = false;
public static void main(String[] args) {
JFrame f = new JFrame();
Main m = new Main();
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (clicked) {
g.setColor(Color.BLACK);
g.fillOval(60, 60, 15, 15);
}
}
};
JButton b3 = new JButton("Ball");
f.setSize(700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
panel.add(b3);
f.setVisible(true);
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
clicked = true;
f.repaint();
}
});
}
}

Window not appearing properly after opening with JButton in Java

I have a very strange problem with my java application. I basically click the JButton and the new window I want to open opens but with no content showing. Here is what happens.
If I run the class on its own without using a JButton it runs proberly like so.
Here is the code for the button.
public Create()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 316);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnBuildData = new JButton("Build Graph");
btnBuildData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//CreateTest frame = new CreateTest();
new CreateTest().setVisible(true);
}
});
btnBuildData.setBackground(Color.WHITE);
btnBuildData.setForeground(Color.BLACK);
btnBuildData.setBounds(29, 59, 107, 23);
contentPane.add(btnBuildData);
This is strange to me because I have used this code for other classes and it works as intended. I have tried many different ways to do the same thing but none of them have worked. Here is some code for the frame I am opening with the button.
public class CreateTest extends JFrame {
public CreateTest() {
}
//Create the connection to Neo4j
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "*****", "*******" ) );
Session session = driver.session();
StatementResult resultVariable;
Record recordVariable;
//Create variables to manage communication with Neo4j
String resultString = new String();
Query neoQuery = new Query();
private JTextField progressTextField;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new CreateTest().initUI();
}
});
}
protected void initUI()
{
final JFrame frame = new JFrame();
//the form
frame.setTitle(CreateTest.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click here to add data");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
doWork();
}
});
progressTextField = new JTextField(25);
progressTextField.setEditable(false);
frame.getContentPane().add(progressTextField, BorderLayout.NORTH);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
protected void doWork() {
SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
#Override
protected Void doInBackground() throws Exception {
CreateTest frame = new CreateTest();
}
#Override
protected void process(List<Integer> chunks) {
progressTextField.setText(chunks.get(chunks.size() - 1).toString());
}
#Override
protected void done() {
progressTextField.setText("Success: All Nodes have been added ");
}
};
worker.execute();
}
}
There is a difference between the two windows. One being absolute layout and the other jpanel but I don't think this is the problem. If anyone has any ideas please let me know, any ideas will be appreciated. Thanks.
Your calling an empty constructor with new CreateTest()
public CreateTest() {
}
It does nothing since your code is outside of it.

Navigating through an applet with Card Layout (Need some guidance and code fixing)

First of all I would like to say that in terms of my java knowledge I am a total beginner. So, I am developing an applet which would have different pages using Card Layout. I managed to gather some code from an online sources, however, I am struggling to accomplish a few things. I will split it into questions.
How do I make it so that when the button is pressed, the user would be taken to a specified card rather than a next one?
In the 'Main' class, how do I set visibility to a card that I want rather than the first one on the list?
In pages classes (Menu, FirstPage, etc), how can I only have a single method for button handling rather than having to repeat the same code?
Will my page classes work if I will have paint methods in them?
How do I turn this application into an applet? As at the moment it is a JFrame.
I would really appreciate if someone could help me with this.
Main class
public class Main
{
private Menu menu;
private FirstPage page1;
private SecondPage page2;
private ThirdPage page3;
private FourthPage page4;
private void showGUI()
{
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
contentPane.setPreferredSize(new Dimension(240,600));
menu = new Menu(contentPane);
page1 = new FirstPage(contentPane);
page2 = new SecondPage(contentPane);
page3 = new ThirdPage(contentPane);
page4 = new FourthPage(contentPane);
contentPane.add(menu, "Menu");
contentPane.add(page1, "First page");
contentPane.add(page2, "Second page");
contentPane.add(page3, "Third page");
contentPane.add(page4, "Fourth page");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Main().showGUI();
}
});
}
}
Menu page class
class Menu extends javax.swing.JPanel
{
private javax.swing.JLabel textLabel;
private JPanel contentPane;
private JButton pageOne;
private JButton pageTwo;
private JButton pageThree;
private JButton pageFour;
public Menu(JPanel cp)
{
this.contentPane = cp;
initComponents();
}
private void initComponents()
{
setBackground(Color.WHITE);
setLayout(null);
textLabel = new JLabel("This is the menu page", JLabel.CENTER);
textLabel.setForeground(Color.BLACK);
pageOne = new JButton("Flowers");
pageTwo = new JButton("Trees");
pageThree = new JButton("Rivers");
pageFour = new JButton("Seas");
pageOne.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageOneAction(ae); } });
pageTwo.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
pageThree.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
pageFour.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
textLabel.setBounds(20, 20, 200, 60);
pageOne.setBounds(20, 100, 200, 60);
pageTwo.setBounds(20, 170, 200, 60);
pageThree.setBounds(20, 240, 200, 60);
pageFour.setBounds(20, 310, 200, 60);
add(pageOne);
add(pageTwo);
add(pageThree);
add(pageFour);
add(textLabel);
}
private void pageOneAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageTwoAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageThreeAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageFourAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
}
First page class
class FirstPage extends javax.swing.JPanel
{
private javax.swing.JLabel textLabel;
private JPanel contentPane;
private JButton menu;
private JButton pageTwo;
private JButton pageThree;
private JButton pageFour;
public FirstPage(JPanel cp)
{
this.contentPane = cp;
this.setSize(500, 500);
initComponents();
}
private void initComponents()
{
setBackground(Color.WHITE);
setLayout(null);
textLabel = new JLabel("This is the flowers page", JLabel.CENTER);
textLabel.setForeground(Color.BLACK);
menu = new JButton("Back to Menu");
pageTwo = new JButton("Trees");
pageThree = new JButton("Rivers");
pageFour = new JButton("Seas");
menu.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { menuAction(ae); } });
pageTwo.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
pageThree.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
pageFour.addActionListener(new ActionListener(){ #Override public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
textLabel.setBounds(20, 20, 200, 60);
menu.setBounds(20, 100, 200, 60);
pageTwo.setBounds(20, 170, 200, 60);
pageThree.setBounds(20, 240, 200, 60);
pageFour.setBounds(20, 310, 200, 60);
add(menu);
add(pageTwo);
add(pageThree);
add(pageFour);
add(textLabel);
}
private void menuAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageTwoAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageThreeAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageFourAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
}
The other pages are the same as the ones above.

How to use CardLayout with multiple JButtons?

I need to make a GUI that asks the users for details and then save them in a linked list. I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time. I have done probably less half of what I need to do and here I am quite lost in this part. The code below compiles and executes but when I click the buttons, the desired change does not happen . What could be wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout to BorderLayout.CENTER, then you "overlay" it with your buttonPanel, which is using null-layout.
I would go with a simple FlowLayout (the default layout-manager for a JPanel) for the buttonPanel and add it to the BorderLayout.SOUTH of the contentPane. I would also strongly recommend reading this tutorial.
So remove the following lines of code:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel); to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);.
Also forget about the null-layout / setBounds() in your Window-classes.
(Note that you still won't see the text change if you press a button because you always add a JLabel with the same text ("Memory Files") to your Windows.)

java - method and passing variable from jInternalFrame to jFrame

and I have the following problem.
i am new in java and i am trying to send a variable which is user_id, from JInternalFrame to JFrame.
I can't use constructor because JFrame is the main program activated at startup and containing jDesktoPane
so i was trying to use the methods, however, can't do it (i posted the two methods, both of which I tried but did not work)
Method A)
Code in jFrame (Main_window)
public javax.swing.JTextField getID() {
return jTextField_id;
}
public void setID(javax.swing.JTextField ID)
{
jTextField_id = ID;
}
code in jInternalFrame
Main_window send = new Main_window();
send.getID().setText("123");
Method B)
Code in jFrame (Main_window)
USER_ID is a variable accesible in any place of jFrame
public void setID(String ID)
{
USER_ID = ID;
}
code in jInternalFrame
Main_window send = new Main_window();
send.setID("123");
both method don't change anything but have no errors in compilation
if there is any other way of doing it pls tell me :)
sorry for my language and grammar.
if this help i use Eclipse compilator
this code worked for me, ty for help
public void set_ID(String ID)
{
Test_JF mainWindow = (Test_JF) this.getTopLevelAncestor();;
mainWindow.setID(ID);
}
activated by set_ID("1234");
Try this in your JInternalFrame class:
JFrame mainWindow = (JFrame)this.getTopLevelAncestor();
mainWindow.setID("123");
ok so here is smaller version of my program
"Test_JF" is a main JFrame and "Test_JIF" is a JInternalFrame
here is TestJF
public class Test_JF extends JFrame {
private JPanel contentPane;
private JTextField user_id;
String USER_ID;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test_JF frame = new Test_JF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test_JF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 750, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
user_id = new JTextField();
user_id.setBounds(338, 11, 86, 20);
contentPane.add(user_id);
user_id.setColumns(10);
JButton button_user_id = new JButton("fill user id");
button_user_id.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
user_id.setText(USER_ID);
}
});
button_user_id.setBounds(239, 10, 89, 23);
contentPane.add(button_user_id);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBounds(10, 56, 714, 395);
contentPane.add(desktopPane);
addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent arg0) {
Test_JIF Open = new Test_JIF();
desktopPane.add(Open);
Open.show();
}
});
}
public void setID(String ID)
{
USER_ID = ID;
}}
here is TestJIF
public class Test_JIF extends JInternalFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test_JIF frame = new Test_JIF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test_JIF() {
Test_JF mainWindow = (Test_JF)this.getTopLevelAncestor();
setBounds(100, 100, 328, 199);
getContentPane().setLayout(null);
JButton button_send = new JButton("New button");
button_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainWindow.setID("123");
}
});
button_send.setBounds(111, 73, 89, 23);
getContentPane().add(button_send);
}}
i clear imports here(on web) so it will be cleaner

Categories

Resources