Results 1 to 3 of 3
- 05-03-2008, 09:54 PM #1
Member
- Join Date
- May 2008
- Posts
- 18
- Rep Power
- 0
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.
Java 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(); } }); } }
- 05-04-2008, 06:23 AM #2
Compiling your posted code:
Java Code:C:\jexp>javac newclass2.java newclass2.java:92: cannot find symbol symbol : class TextDemo location: class NewClass2 frame.add(new TextDemo()); ^ 1 errorIf 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:Java 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());
Java 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) { }
- 05-04-2008, 06:41 AM #3
Member
- Join Date
- May 2008
- Posts
- 22
- Rep Power
- 0
Don't you get a compile error from that?
There are a few things wrong with this, but ultimately it just needs to be removed. (TextDemo is called in the NewClass2 constructor.)Java Code:frame.add(new TextDemo());
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: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.Java Code:public NewClass2() { //... //Add Components to this container, using the default FlowLayout. textField = new JTextField(20); textField.addActionListener(this); add(b1); }I believe that will do what you want.Java Code:public NewClass2() { //... //Add Components to this container, using the default FlowLayout. textField = new JTextField(20); textField.addActionListener(this); add(textField); add(b1); }
EDIT: Woops, just looked and saw the post conflict.Last edited by Jesdisciple; 05-05-2008 at 11:55 AM.
Similar Threads
-
How can i copy a folder from one place to another..
By rajeshgubba in forum New To JavaReplies: 4Last Post: 06-14-2008, 02:21 AM -
Problem with applying Sql order by to html table
By sireesha264 in forum Advanced JavaReplies: 2Last Post: 02-04-2008, 10:20 AM -
How to place panel into frame
By vivek_9912 in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 11:21 PM -
adding a variable in order to a list
By Jrr in forum New To JavaReplies: 2Last Post: 11-19-2007, 01:10 AM -
how to place a divider of the splitpane?
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks