Help in adding a label and textfield

/*

* TabbedPaneDemo.java is a 1.4 example that requires one additional file:

*images/middle.gif.

*/

import javax.swing.*;

import javax.swing.JTabbedPane;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JComponent;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.KeyEvent;

public class TabbedPaneDemo extends JPanel {

public TabbedPaneDemo() {

JTabbedPane tabbedPane = new JTabbedPane();

JComponent panel1 = makeTextPanel("Panel #1");

tabbedPane.addTab("Tab 1",panel1);

JComponent panel2 = makeTextPanel("Panel #2");

tabbedPane.addTab("Tab 2",panel2);

JComponent panel3 = makeTextPanel("Panel #3");

tabbedPane.addTab("Tab 3",panel3);

JComponent panel4 = makeTextPanel("Panel #4 ");

panel4.setPreferredSize(new Dimension(800, 600));

tabbedPane.addTab("Tab 4",panel4);

//Add the tabbed pane to this panel.

add(tabbedPane);

//Uncomment the following line to use scrolling tabs.

//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

}

protected JComponent makeTextPanel(String text) {

JPanel panel = new JPanel(false);

JLabel filler = new JLabel(text);

filler.setHorizontalAlignment(JLabel.CENTER);

panel.setLayout(new GridLayout(1, 1));

panel.add(filler);

return panel;

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

JFrame frame = new JFrame("TabbedPaneDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new TabbedPaneDemo();

newContentPane.setOpaque(true); //content panes must be opaque

frame.getContentPane().add(new TabbedPaneDemo(),

BorderLayout.CENTER);

//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();

}

});

}

}

Actually this code consists of four tabs. i just need someone to help me to tell me where shall i add the code for creating the GUI.I want to add some texfields and labels in the respective tab. Actually i already have the code. I just need to knoe where shall i add it

Thank you very much

[2964 byte] By [ajitpal] at [2007-9-30 20:53:50]
# 1
In the method makeTextPanel you will add your objects.JLabel labelName = new JLabel("It's a Label");panel.getContentPane().add(labelName);or something like that.
lmelliot at 2007-7-7 2:26:40 > top of Java-index,Desktop,Core GUI APIs...
# 2
YEA I Tried adding it using your way, but it generates and error message saying thtcannot resolve symbol variable panel...ANY idea out there?
ajitpal at 2007-7-7 2:26:40 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi guysthankx for the help...manage to solve the problem...
ajitpal at 2007-7-7 2:26:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

i have another minor problem here

/*

* TabbedPaneDemo.java is a 1.4 example that requires one additional file:

*images/middle.gif.

*/

import javax.swing.*;

import java.awt.*;

import javax.swing.JTabbedPane;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JComponent;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.KeyEvent;

public class TabbedPaneDemo extends JPanel {

public TabbedPaneDemo() {

JTabbedPane tabbedPane = new JTabbedPane();

JComponent panel1 = makeTextPanel("Panel #1");

tabbedPane.addTab("Tab 1",panel1);

JComponent panel2 = makeTextPanel("Panel #2");

tabbedPane.addTab("Tab 2",panel2);

JComponent panel3 = makeTextPanel("Panel #3");

tabbedPane.addTab("Tab 3",panel3);

JComponent panel4 = makeTextPanel("Panel #4 ");

panel4.setPreferredSize(new Dimension(800, 600));

tabbedPane.addTab("Tab 4",panel4);

//Add the tabbed pane to this panel.

add(tabbedPane);

//Uncomment the following line to use scrolling tabs.

//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

}

protected JComponent makeTextPanel(String text) {

//JPanel panel = new JPanel(false);

//JLabel labelName = new JLabel("It's a Label");

// panel.add(labelName);;

//JTextField filler = new JTextField(10);

//labelName.setHorizontalAlignment(JLabel.CENTER);

//panel.setLayout(new GridLayout(1, 1));

//panel.add(filler);

//return panel;

String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};

int numPairs = labels.length;

//Create and populate the panel.

JPanel p = new JPanel(new SpringLayout());

for (int i = 0; i < numPairs; i++) {

JLabel l = new JLabel(labels, JLabel.TRAILING);

p.add(l);

JTextField textField = new JTextField(10);

l.setLabelFor(textField);

p.add(textField);

}

//Lay out the panel.

SpringUtilities.makeCompactGrid(p,

2, 4, //rows, cols

6, 6,//initX, initY

6, 6);//xPad, yPad

return p;

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

JFrame frame = new JFrame("TabbedPaneDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new TabbedPaneDemo();

newContentPane.setOpaque(true); //content panes must be opaque

frame.getContentPane().add(new TabbedPaneDemo());

//Display the window.

frame.pack();

frame.setSize(1024,768);

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

}

});

}

}

i have declared the panel as size 800*600..im adding 4 components in a row..like below

label: textfieldlabel: textfield

label: textfieldlabel: textfield

label: textfieldlabel: textfield

label: textfieldlabel: textfield

But i dunno why the text fields are becoming so big(occupying the empty screen)...

Any suggestions?

thnx

ajitpal at 2007-7-7 2:26:40 > top of Java-index,Desktop,Core GUI APIs...
# 5
Shall i use border to limit the size of da text field..or shall i declare another panel in the previous panel and set the size to a smaller size..what shall i do...any suggestions?
ajitpal at 2007-7-7 2:26:40 > top of Java-index,Desktop,Core GUI APIs...