Results 1 to 10 of 10
Thread: JFrame and two JPanel Classes
- 08-23-2009, 03:05 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
JFrame and two JPanel Classes
Hello. I want to send information from Panel1 to Panel2. Start and Panel1 works 100% but, Panel2 doesn't print anything in paintComponent; I don't think it even goes from Panel1 to Panel2. Here's what I'm basically trying to do: (I edited a lot of it out)
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Start extends JFrame { private Image[] image = new Image[13]; AllImages allImages = new AllImages(); public Start() { super("Swing"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(806, 626); this.getContentPane().add(new Panel1(images)); this.setResizable(false); this.setVisible(true); } public static void main(String[] arguments) { new Start(); } public void paintComponent(Graphics g) { } public class AllImages { public AllImages() { //images } } }Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; class Panel1 extends JPanel implements MouseListener { private Image[] images; private String[] newInfo; public Panel1(Image[] aimages) { images = aimages; addMouseListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); //images } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (x > 163 && x < 163 + 153 && y > 445 && y < 445 + 41) //Where the button is placed and the height and width of it. { //accept button String getName = "blah blah blah"; String getID = "213123141231"; getName = name.getText(); //name and ID are JTextFields getID = ID.getText(); newInfo[0] = getName; newInfo[1] = getID; new Panel2(newInfo); } } public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e){} public void mouseExited (MouseEvent e){} }Any help is very much appreciated.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Panel2 extends JPanel implements MouseListener { private String[] Info; public Panel2(String[] newInfo) { Info = newInfo; addMouseListener(this); } public void paintComponent(Graphics g) { g.setColor(Color.black); g.drawString(Info[0],10,50); g.drawString(Info[1],10, 100); } public void mouseClicked (MouseEvent e) {} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e){} public void mouseExited (MouseEvent e){} }
- 08-23-2009, 03:24 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems to me there are few errors. In Panel1 class,
what's getText() ?Java Code:getName = name.getText(); //name and ID are JTextFields
- 08-23-2009, 03:44 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
- 08-24-2009, 03:28 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But when I compile your code, I got some errors.
-
getText() will extract a String from a JTextField, but you don't declare either of the alleged JTextFields, name or ID, anywhere in your Panel1 class. So without these JTextFields, this code is completely borked and will not compile. If you have code where this is fixed, then please post it.
-
I think you're looking to do something similar to this code below. I like to have my Panel1 and Panel2 classes know nothing about each other. Instead they have public methods to allow a main class to do the dirty work:
Start.java
Panel1.javaJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Start extends JFrame { private static final Dimension PANEL_SIZE = new Dimension(400, 200); private Panel1 panel1 = new Panel1(); private Panel2 panel2 = new Panel2(); public Start() { super("Swing"); panel1.setPreferredSize(PANEL_SIZE); panel2.setPreferredSize(PANEL_SIZE); panel1.setBorder(BorderFactory.createTitledBorder("panel 1")); panel2.setBorder(BorderFactory.createTitledBorder("panel 2")); add(panel1, BorderLayout.NORTH); add(panel2, BorderLayout.SOUTH); // add actionlistener to button in panel1 object // use public methods found in Panel1 and Panel2 classes // to get strings from Panel1's textfields and display in // Panel2. panel1.addTransferActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // first extract Strings from panel1's textfields String name = panel1.getNameText(); String id = panel1.getIdText(); String[] info = {name, id}; // place into array // then pass array to panel2 panel2.setStrings(info); } }); } // to display our JFrame cleanly private static void createAndShowUI() { JFrame frame = new Start(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } // start Swing in a thread-safe manner public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Panel2.javaJava Code:import java.awt.event.*; import javax.swing.*; class Panel1 extends JPanel { private JTextField name = new JTextField(10); private JTextField id = new JTextField(10); private JButton transferInfoBtn = new JButton("Transfer Info"); public Panel1() { add(name); add(id); add(transferInfoBtn); } public String getNameText() { return name.getText(); } public String getIdText() { return id.getText(); } // allows the main class to add an actionlistener to the transfer info button public void addTransferActionListener(ActionListener al) { transferInfoBtn.addActionListener(al); } }
Java Code:import java.awt.*; import javax.swing.*; public class Panel2 extends JPanel { private String[] info = {"", ""}; public void setStrings(String[] info) { // copy String array over System.arraycopy(info, 0, this.info, 0, info.length); repaint(); // tells JVM to call paintComponent method } public void paintComponent(Graphics g) { super.paintComponent(g); // to clear previous data from panel g.setColor(Color.black); g.drawString(info[0], 10, 50); g.drawString(info[1], 10, 100); } }
- 08-24-2009, 01:38 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
Sorry, must have edited them out by accident. If you still need the completed code, here:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; class Panel1 extends JPanel implements MouseListener { private Image[] images; private String[] newInfo; private JTextField name; private JTextField ID; public Panel1(Image[] aimages) { images = aimages; addMouseListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); //images } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (x > 163 && x < 163 + 153 && y > 445 && y < 445 + 41) //Where the button is placed and the height and width of it. { //accept button String getName = "blah blah blah"; String getID = "213123141231"; getName = name.getText(); //name and ID are JTextFields getID = ID.getText(); newInfo[0] = getName; newInfo[1] = getID; new Panel2(newInfo); } } public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e){} public void mouseExited (MouseEvent e){} }
Also, thanks Fubarable, I'll take a look at it.
-
If you look at my code, you should be able to get ideas on how to complete your code. Why not give it a try first, then come back only if it doesn't work.
- 08-25-2009, 04:38 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I appreciate, if all the members can ask the question more clearly in very first post with all relevant details, it's easy to answer as well as easy to find the solution for there question.
- 08-25-2009, 12:49 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
How to add JFrame inside JPanel
By niteshwar.bhardwaj in forum Java 2DReplies: 8Last Post: 12-13-2009, 08:41 PM -
Multiple Panels in JFrame or JPanel
By DavidG24 in forum AWT / SwingReplies: 5Last Post: 05-16-2009, 12:47 PM -
Display output in Jframe or JPanel
By rammurugesan in forum AWT / SwingReplies: 12Last Post: 04-14-2009, 01:45 PM -
problems with JPanel and JFrame
By v1nsai in forum New To JavaReplies: 13Last Post: 04-08-2009, 07:49 PM -
scroll a Jpanel in a JFrame
By nidhirastogi in forum SWT / JFaceReplies: 1Last Post: 09-07-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks