Re: JLabel property - setBounds
Yes What I am trying to say in my previous mail is this:
L1 L2 L3 L4
TF1 TF2 TF3 TF4 Add
When "Add" is clicked, becomes this:
(No label here the above label remains for this....)
TF5 TF6 TF7 TF8 Add
TF9 TF10 TF11 TF12 Add
and so on....
The Add button basically adds rows of textfields below (i.e) a row of
textfields and I also have to make use of inserting a row of
textfields above by clicking the down button but that is another
task... i meant inserting in between selected textfields.... I first
thought it would be hard doing this because a JList with textarean
where rows properties insertion adding and deletion is easier would be
there... A lot of experts have done this.....
That is my dilemna
Sriram
On Sep 15, 3:55 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
Please don't top-post, please add your reply to the bottom after
trimming any material not needed.
GG Sriram wrote:
On Sep 12, 2:57 pm, RedGrittyBrick wrote:
GG Sriram wrote:
Hi
I am new to Swing and I am trying to break a string of labels =
that
I have used the setBounds property (I am also failrly new to this) fo=
r
setting the labels above the JTextFields... Is it possible to set the
labels above the required positionof the textfield and upon adding
more fields assign a label correctly in the position above a
textfield. Sometimes I see that the labels are misaligned and not in
their proper place when I tr
something like this
Label 1 Label2
Label3 ....adding more textfields and labe=
ls
should be set automatically in their
correct position
JTextField1 JTextField2 JTextField
I hope this is clear. I am thinking about how to make this work. Any
sample ideas might help
Sriram
Here's a more dynamic example
--------------------------------- 8< ---------------------------------=
--
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
* @author RedGrittyBrick
*/
public class GrowGrid {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GrowGrid().createAndShowGUI();
}
});
}
private JFrame frame;
private JPanel panel = new JPanel(new GridBagLayout());
private GridBagConstraints constraints = new GridBagConst=
raints();
private List<JTextField> fields = new ArrayList<JTextFiel=
d>();
private JButton button = new JButton("More!");
private void createAndShowGUI() {
String[] labels = { "Alpha", "Bravo", "Charlie", =
"Delta" };
for (String label : labels)
addColumn(label);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) =
{
String labelText = JOptionPane.sh=
owInputDialog
(frame, "Column Title");
addColumn(labelText);
frame.pack(); // resize frame to ac=
commodate more
}
});
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(button, constraints);
frame = new JFrame("GrowGrid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE=
);
frame.add(new JScrollPane(panel));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void addColumn(String labelText) {
constraints.gridx = fields.size();
constraints.gridy = 0;
panel.add(new JLabel(labelText), constraints);
JTextField field = new JTextField(6);
constraints.gridy = 1;
panel.add(field, constraints);
fields.add(field); // so we can later retrieve cont=
ent
panel.revalidate(); // redo layout for extra column
}}
--------------------------------- 8< ---------------------------------=
--
Hi
THanks RGB for your prompt reply However I realized I made a mistak=
e
in my previous post.
I suggest you post the Java code you have written so far.
Is it possible to display textfields i meant
something like this simulateneously on clicking the Add Button
Sample
JLabel1 JLabe=
l2 JLabel3 JLabel4
textfield3
textfield4 Button "Add"
( A=
fter clicking Add button)
5 textfield7
textfield 8 Again Button Add appears
and so on sa=
me action and textfields
being displayed simulateneously
I'm having trouble understanding what you mean. Your example layout is
indented too far and uses lengthy names so it gets wrapped onto multiple
lines, try making it shorter.
Do you mean this:
L1 L2 L3 L4
TF1 TF2 TF3 TF4 Add
When "Add" is clicked, becomes this:
L5 L6 L7 L8
TF5 TF6 TF7 TF8 Add
If so, just have the "Add" button use setText on the JLabels to change
them and use setText on the JTextFields to reset them after stashing
their values in a separate String array (or other appropriate structure).
The main purpose of doing this is yto have a user (i.e) operator enter
some tasks for creating a part like in each of a textfield he enters
part no and description in the textfields....
I understand a list with JTable and JTextarea would be nice but my
My example does *not* use JTable or JTextArea
prof is adamant on using this kinda appearance....
Maybe you should ask him for help?
The very least you should learn from my example is that you don't need
to use setBounds() to lay out your GUI in a pleasing and consistent
fashion. Using a layout manager is easier and more reliable.
--
RGB- Hide quoted text -
- Show quoted text -