Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-03-2008, 11:54 PM
Member
 
Join Date: May 2008
Posts: 18
impact is on a distinguished road
How to place the GUI components in correct order
Hi,
I have done following code, it works but always I see the "search" button comes first and then the "text area". Can you please show me how to get this other way around?

As you can understand I novice in java, so if you can explain me what actually you did, it will be very helpful.

Thank you very much.

Code:
package test; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTextField; public class NewClass2 extends JPanel implements ActionListener { protected JButton b1; protected JTextField textField; public NewClass2() { b1 = new JButton("Search"); b1.setVerticalTextPosition(AbstractButton.CENTER); b1.setHorizontalTextPosition(AbstractButton.LEADING); b1.setMnemonic(KeyEvent.VK_D); b1.setActionCommand("disable"); //Listen for actions on buttons 1 and 3. b1.addActionListener(this); b1.setToolTipText("Go"); //Add Components to this container, using the default FlowLayout. TextDemo(); add(b1); } // This is for Button public void actionPerformed(ActionEvent e) { if ("disable".equals(e.getActionCommand())) { b1.setEnabled(true); } else { b1.setEnabled(true); } } /** * Text area * @return */ public void TextDemo() { textField = new JTextField(20); textField.addActionListener(this); } public void TextDemoactionPerformed(ActionEvent evt) { } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Version 1.0"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NewClass2 newContentPane = new NewClass2(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); frame.setSize(new Dimension(600, 600)); frame.add(new TextDemo()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-04-2008, 08:23 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Compiling your posted code:
Code:
C:\jexp>javac newclass2.java newclass2.java:92: cannot find symbol symbol : class TextDemo location: class NewClass2 frame.add(new TextDemo()); ^ 1 error
Code:
// This line sets the contentPane to "newContentPane". frame.setContentPane(newContentPane); ... // This line tries to add a new class // (that must extend Component) to the // center section of the contentPane. frame.add(new TextDemo());
If you are going to have NewClass2 extend JPanel and to use an instance of it as your contentPane then you could add the textComponent to the panels layout in the class constructor. To fix this with minimal changes to your code try:
Code:
//Add Components to this container, using the default FlowLayout. add(getTextComponent()); add(b1); ... /** * Text area * @return */ public JTextField getTextComponent() { textField = new JTextField(20); textField.addActionListener(this); return textField; } public void TextDemoactionPerformed(ActionEvent evt) { }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-04-2008, 08:41 AM
Member
 
Join Date: May 2008
Posts: 22
Jesdisciple is on a distinguished road
Don't you get a compile error from that?
Code:
frame.add(new TextDemo());
There are a few things wrong with this, but ultimately it just needs to be removed. (TextDemo is called in the NewClass2 constructor.)

For that matter, you don't need the TextDemo method at all, because you never reuse the logic it contains. So put its contents into the NewClass2 constructor, like so:
Code:
public NewClass2() { //... //Add Components to this container, using the default FlowLayout. textField = new JTextField(20); textField.addActionListener(this); add(b1); }
Also, I don't see that the text field is ever added to the panel, so pass it to add - before add(b1) so the components are arranged in that order.
Code:
public NewClass2() { //... //Add Components to this container, using the default FlowLayout. textField = new JTextField(20); textField.addActionListener(this); add(textField); add(b1); }
I believe that will do what you want.

EDIT: Woops, just looked and saw the post conflict.

Last edited by Jesdisciple : 05-05-2008 at 01:55 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i copy a folder from one place to another.. rajeshgubba New To Java 4 06-14-2008 04:21 AM
Problem with applying Sql order by to html table sireesha264 Advanced Java 2 02-04-2008 12:20 PM
How to place panel into frame vivek_9912 AWT / Swing 2 11-20-2007 01:21 AM
adding a variable in order to a list Jrr New To Java 2 11-19-2007 03:10 AM
how to place a divider of the splitpane? christina New To Java 1 08-06-2007 09:41 PM


All times are GMT +3. The time now is 03:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org