473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Adding Components to JPanel - Help required

11
I'm trying to display a grid of TextPane's that are sized to fit a JPanel i've drawn into a Dialog class, the number of 'Cells' are entered into JTextFields and when the appropriate button is hit this method is executed. Each TextPane is stored as an object in the aray tableGrid. The code compiles fine, but when executed the JPanel remains blank.

I've created did using a book that explains it in AWT and i'm using SWING but so far as i can tell this should work for SWING too

Any help would be much appreciated.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

//Parse all user input values for Table generation
rows = Integer.parseInt(jTextFieldRows.getText());
cols = Integer.parseInt(jTextFieldCols.getText());
width = Integer.parseInt(jTextFieldWidth.getText());
border = Integer.parseInt(jTextFieldBorder.getText());

//instantiate objects
int rowHeight = 500/rows;
int colWidth = 700/cols;
String labelString;
tableGrid = new Object[cols][rows];

for (int y=0; y<rows; y++);
{
for (int x=0; x<cols; x++);
{
labelString = "jTextPain"+x+y;
JTextPane textPane = new JTextPane();
tableGrid[x][y] = textPane;
textPane.setSize(colWidth, rowHeight);
textPane.setName(labelString);

}
}

jPanelTable.setLayout(new GridLayout(rows,cols));
//Pack the panel with the objects
for (int y=0; y<rows; y++);
{
for (int x=0; x<cols; x++);
{
JTextPane textPane = (JTextPane) tableGrid[x][y];
jPanelTable.add(textPane);
}
}
}
Apr 4 '07 #1
6 3429
r035198x
13,262 8TB
I'm trying to display a grid of TextPane's that are sized to fit a JPanel i've drawn into a Dialog class, the number of 'Cells' are entered into JTextFields and when the appropriate button is hit this method is executed. Each TextPane is stored as an object in the aray tableGrid. The code compiles fine, but when executed the JPanel remains blank.

I've created did using a book that explains it in AWT and i'm using SWING but so far as i can tell this should work for SWING too

Any help would be much appreciated.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

//Parse all user input values for Table generation
rows = Integer.parseInt(jTextFieldRows.getText());
cols = Integer.parseInt(jTextFieldCols.getText());
width = Integer.parseInt(jTextFieldWidth.getText());
border = Integer.parseInt(jTextFieldBorder.getText());

//instantiate objects
int rowHeight = 500/rows;
int colWidth = 700/cols;
String labelString;
tableGrid = new Object[cols][rows];

for (int y=0; y<rows; y++);
{
for (int x=0; x<cols; x++);
{
labelString = "jTextPain"+x+y;
JTextPane textPane = new JTextPane();
tableGrid[x][y] = textPane;
textPane.setSize(colWidth, rowHeight);
textPane.setName(labelString);

}
}

jPanelTable.setLayout(new GridLayout(rows,cols));
//Pack the panel with the objects
for (int y=0; y<rows; y++);
{
for (int x=0; x<cols; x++);
{
JTextPane textPane = (JTextPane) tableGrid[x][y];
jPanelTable.add(textPane);
}
}
}
after adding new components to a Frame/Panel, you need to call repaint for the changes to become visible.
Apr 5 '07 #2
davedwm
11
after adding new components to a Frame/Panel, you need to call repaint for the changes to become visible.

the idea of this function is to fill the panel with text boxes that are spaced apart, in the form of a grid specified by the number of rows and cols, stored as ints, their width is calculated so they are reduced to fit the more there are, however when drawn in the panel they appear a bit of a mess and its hard to see the arrangement, any advice on errors in the code would be appreciated.

Thanks
Apr 9 '07 #3
davedwm
11
the idea of this function is to fill the panel with text boxes that are spaced apart, in the form of a grid specified by the number of rows and cols, stored as ints, their width is calculated so they are reduced to fit the more there are, however when drawn in the panel they appear a bit of a mess and its hard to see the arrangement, any advice on errors in the code would be appreciated.

Thanks
Seem's to work but there is no spacing, the textpanes could be set too big, but with the parameters i have set they shouldn't be.

Also even with the same numbers of rows and cols specified, each time the generate button is pressed, different numbers of text boxes appear, does this mean the array needs to be emptied? tho if the figures are the same i would presume it would just overwrite the old entries.
Apr 9 '07 #4
davedwm
11
Seem's to work but there is no spacing, the textpanes could be set too big, but with the parameters i have set they shouldn't be.

Also even with the same numbers of rows and cols specified, each time the generate button is pressed, different numbers of text boxes appear, does this mean the array needs to be emptied? tho if the figures are the same i would presume it would just overwrite the old entries.
OK the last two posts can be pretty much ignored having played round with the grid layout parameters i've selected a gap between the text boxes which show how they appear, which i muist say doesn't work correctly, with the below code, the text boxes add them selves to the panel one by one, each time the method is executed the loop doesn't not seem to follow through and each text box prints the contents which tells me there must be a problem with the loop and the x and y variables which i cannot pinpoint,

any help greatly appreciated.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

//Parse all user input values for Table generation
rows = Integer.parseInt(jTextFieldRows.getText());
cols = Integer.parseInt(jTextFieldCols.getText());
width = Integer.parseInt(jTextFieldWidth.getText());
border = Integer.parseInt(jTextFieldBorder.getText());

//instantiate objects
int rowHeight = 500/rows;
int colWidth = 700/cols;
String labelString;
tableGrid = new Object[cols][rows];
jPanelTable.setLayout(new GridLayout(rows,cols,5,5));


for (int y=0; y<rows; y++);
{
for (int x=0; x<cols; x++);
{

labelString = "jTextPain "+x+y;
JTextPane textPane = new JTextPane();
tableGrid[x][y] = textPane;
textPane.setSize(colWidth, rowHeight);
textPane.setName(labelString);
textPane.setText(labelString);
jPanelTable.add(textPane);

}
}


jPanelTable.repaint();

}
Apr 10 '07 #5
davedwm
11
AHAHAHA

; at the end of the For loop statement.

:-)

thanks for the help

Any tips on how to clear the panel? when you run the method again the Panel gets double packed.
Apr 10 '07 #6
davedwm
11
jPanelTable.removeAll();

:-)
Apr 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Hal Vaughan | last post by:
I have a JPanel in a window and I am putting different components in it as the user goes through a setup wizard. In theory, I have an array of components (other JPanels), and I want to step...
4
by: Fereshteh Shojaei | last post by:
Hi, I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>. Regards, Fereshteh
6
by: cpnet | last post by:
I've authored a custom web component (a non-ui component kinda like a DataSet) and so far it's working. When my web component is added to the web form in the designer from the toolbox, the...
1
by: npaulus | last post by:
Hi, I am trying to dynamically add user controls on to my web form but for some reason my form isnt displaying the user control. form1.cs: using System; using System.Drawing; using...
0
by: porky008 | last post by:
I need to add a search button to this and have no clue how to do it. Can some one please help me on this? import java.awt.*; import java.awt.event.*; import javax.swing.*; import...
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
8
nexcompac
by: nexcompac | last post by:
Ok, I am working on an inventory program and this is where I am at thus far. I seem to be getting two errors that I can not figure out. I will post the errors at the end of the code. import...
3
by: gaya3 | last post by:
Hi, I'm having a small doubt in swings.pl consider two java classes. First java class: JFrame -> frame1 and when some action is performed it calls the method in 2nd java function with...
2
by: chanshaw | last post by:
Alright I'm trying to place the textarea on top of the image but right now it displays the textarea below the image, how do i go about doing this. import java.awt.Color; import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.