Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
label loop ontop of array
jason gass
Greenhorn
Posts: 13
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to get a simple grid with labels on the top and side, like a spreadsheet. How can I get the labels to be added properly to this format?
Panel buttonPanel = new Panel ( ); buttonPanel.setLayout (new GridLayout (4, 4, 1, 1) ); tf = new TextField[3][4]; for(int r = 0; r<=2; r++){ for(int c =0; c<=3; c++){ tf[r][c]= new TextField(10); for (int i = 0; i<=2; i++){ buttonPanel.add(new Label(i+"")); } buttonPanel.add(tf[r][c]);
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Learning how to make layouts is like playing with clay. It takes a lot of experimenting and sometimes the clay dries up and you have to start over.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LayoutTest { public static void main(String[] args) { TextField[][] tfs = new TextField[3][3]; // initialize textfields for(int row = 0; row < tfs.length; row++) for(int col = 0; col < tfs[0].length; col++) tfs[row][col] = new TextField(10); Panel panel = new Panel(); int rows = 4; int cols = 4; panel.setLayout(new GridLayout(rows,cols,1,1)); /** assemble panel, add components to grid **/ // first row, // add full row of labels, one for each column // with a blank Label in the first column for(int col = 0; col < cols; col++) { if(col == 0) panel.add(new Label()); else panel.add(new Label(String.valueOf(col), Label.CENTER)); } // rest of grid with an alternate way of adding the components // start at the second row of the grid, below labels // which is the first row of the tfs array // first option: for(int tfRow = 0; tfRow < tfs.length; tfRow++) for(int col = 0; col < cols; col++) { if(col == 0) panel.add(new Label(String.valueOf(tfRow + 1), Label.CENTER)); else panel.add(tfs[tfRow][col - 1]); } /**** // second option: // alternate layout loops with same results // it's all in the counting... for(int row = 0; row < tfs.length; row++) { // add label in first column of grid panel.add(new Label(String.valueOf(row + 1), Label.CENTER)); // fill rest of columns each grid row with textfields for(int col = 0; col < tfs[0].length; col++) panel.add(tfs[row][col]); } ****/ Frame f = new Frame(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(panel); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
converting program
Exception in thread
I'm sure I'm doing something very dumb.... why can't I see my JLabel?
Moving the scrollbar on tab to a specified field
File Reader
More...