Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why are the contents of this table not being shown?

Reply
Thread Tools

Why are the contents of this table not being shown?

 
 
Ramon F Herrera
Guest
Posts: n/a
 
      11-10-2007

The code below was entirely generated by a visual GUI builder (*).
This is the way it looks like, in design mode:

http://patriot.net/~ramon/misc/InvisibleTable.png

My problem is that the cell contents are not being displayed at run
time for some reason.

What I am trying to do is port this NetBeans-specific module to
standard code and jar:

http://platform.netbeans.org/tutoria...en-office.html

TIA!,

-Ramon

(*) from Instantiations at http://www.windowbuilderpro.com/

-------------------

package swingexample;

import java.awt.ComponentOrientation;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.table.AbstractTableModel;

public class SwingExample extends JFrame {

private JTextField textField;
private JTable table;

class TableTableModel extends AbstractTableModel {
public final String[] COLUMN_NAMES = new String[] {"JavaLobby
Title", "Date", "Author", "Reply"};
public int getRowCount() {
return 0;
}
public int getColumnCount() {
return COLUMN_NAMES.length;
}
public String getColumnName(int columnIndex) {
return COLUMN_NAMES[columnIndex];
}
public Object getValueAt(int rowIndex, int columnIndex) {
return null;
}
}


public static void main(String args[]) {
try {
SwingExample frame = new SwingExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

public SwingExample() {
super();
getContentPane().setLayout(null);
setTitle("Swing Example");
setBounds(100, 100, 623, 581);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 23, 554, 215);
getContentPane().add(scrollPane);

table = new JTable();
table.setCellEditor(null);
table.setShowGrid(true);
table.setCellSelectionEnabled(true);
table.setModel(new TableTableModel());
// table.setValueAt("Hello world?", 2, 3) <-- line added by me.
scrollPane.setViewportView(table);

final JPanel panel = new JPanel();
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
panel.setName("name here");
panel.setLayout(null);
panel.setBounds(26, 345, 563, 74);
getContentPane().add(panel);

final JLabel executableLabel = new JLabel();

executableLabel.setComponentOrientation(ComponentO rientation.RIGHT_TO_LEFT);
executableLabel.setText("Executable:");
executableLabel.setBounds(32, 30, 77, 14);
panel.add(executableLabel);

textField = new JTextField();
textField.setBounds(111, 27, 329, 20);
panel.add(textField);

final JButton browseButton = new JButton();
browseButton.setText("Browse...");
browseButton.setBounds(458, 26, 95, 23);
panel.add(browseButton);

final JRadioButton mostRepliedRadioButton = new JRadioButton();
mostRepliedRadioButton.setSelected(true);
mostRepliedRadioButton.setText("Most Replied");
mostRepliedRadioButton.setBounds(169, 452, 113, 23);
getContentPane().add(mostRepliedRadioButton);

final JRadioButton leastRepliedRadioButton = new JRadioButton();
leastRepliedRadioButton.setText("Least Replied");
leastRepliedRadioButton.setBounds(344, 452, 113, 23);
getContentPane().add(leastRepliedRadioButton);

final JButton generateReportButton = new JButton();
generateReportButton.setText("Generate Report");
generateReportButton.setBounds(236, 494, 142, 30);
getContentPane().add(generateReportButton);
}

}

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-10-2007
Ramon F Herrera wrote:
> The code below was entirely generated by a visual GUI builder (*).
> This is the way it looks like, in design mode:
>
> http://patriot.net/~ramon/misc/InvisibleTable.png
>
> My problem is that the cell contents are not being displayed at run
> time for some reason.


I suspect it's the "return 0" in getRowCount() and the "return null" in
getValueAt().

Also, you need to construct your SwingExample instance on the EDT.

And do not embed TAB characters in Usenet posts!

Class TableTableModel can be static.

> -------------------
> public int getRowCount() {
> return 0;
> }
> public Object getValueAt(int rowIndex, int columnIndex) {
> return null;
> }
>
> public static void main(String args[]) {
> try {
> SwingExample frame = new SwingExample();
> frame.setVisible(true);


Oops.

> } catch (Exception e) {
> e.printStackTrace();
> }
> }


--
Lew
 
Reply With Quote
 
 
 
 
Ramon F Herrera
Guest
Posts: n/a
 
      11-10-2007
On Nov 10, 11:17 am, Lew <l...@lewscanon.com> wrote:
> Ramon F Herrera wrote:
> > The code below was entirely generated by a visual GUI builder (*).
> > This is the way it looks like, in design mode:

>
> > http://patriot.net/~ramon/misc/InvisibleTable.png

>
> > My problem is that the cell contents are not being displayed at run
> > time for some reason.

>


> I suspect it's the "return 0" in getRowCount() and the "return null" in
> getValueAt().
>



You are right, Lew. I modified the two returns:

public int getRowCount() {
return 10;
}

public Object getValueAt(int rowIndex, int columnIndex) {
return "null";
}

and now the app behaves better. I guess I am supposed to build the
table contents inside the 'TableTableModel'? IOW:

contents[1, 2] = "Hello";
contents[1, 3] = "World";
[...]
public Object getValueAt(int rowIndex, int columnIndex) {
return contents[rowIndex, columnIndex];
}

Matisse provides a fully implemented mechanism to fill JTables in
design mode. I am less impressed by the mechanism above, which seems
to be the only option with WindowBuilder.

-Ramon


 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      11-10-2007
On Nov 10, 11:17 am, Lew <l...@lewscanon.com> wrote:
> Also, you need to construct your SwingExample instance on the EDT.


I don't understand. Are you saying that I should switch
(complement?) my WindowBuilder Pro to the Eiffel Development Tool?

The other comment I didn't understand was your "oops". What's wrong
with:

frame.setVisible(true);

?

-Ramon


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-10-2007
Ramon F Herrera wrote:
> On Nov 10, 11:17 am, Lew <l...@lewscanon.com> wrote:
>> Also, you need to construct your SwingExample instance on the EDT.

>
> I don't understand. Are you saying that I should switch
> (complement?) my WindowBuilder Pro to the Eiffel Development Tool?


Event Dispatch Thread.

Swing is not thread safe, so all Swing methods must execute in its thread.
Your calls to the Swing library occurred in the main thread of the Java
program. This can lead to unexpected behavior.

> The other comment I didn't understand was your "oops". What's wrong
> with:
>
> frame.setVisible(true);


<http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#package_description>


--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-10-2007
Ramon F Herrera wrote:
>>> My problem is that the cell contents are not being displayed at run
>>> time for some reason.


Lew wrote:
>> I suspect it's the "return 0" in getRowCount() and the "return null" in
>> getValueAt().


Ramon F Herrera wrote:
> You are right, Lew. I modified the two returns:
>
> public int getRowCount() {
> return 10;
> }
>
> public Object getValueAt(int rowIndex, int columnIndex) {
> return "null";
> }


I predict that your JTable will always think the table is ten rows long, and
will show the string "null" at every cell.

Read
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
particularly
<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data>

Among other things, if you want your table to be editable you will need a method
public void setValueAt(Object value, int row, int col)

<http://java.sun.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html#setValueAt(java.lang.Objec t,%20int,%20int)>


--
Lew
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-11-2007
Lew wrote:
..
>> frame.setVisible(true);

>
><http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#package_description>


I sometimes wonder 'What could possess people to use
methods deprecated in 1.2?'.

One source is the unmaintained Sun Java Tutorial codes
that use it, and apparently another is the Java 6.0 package
docs., from which this example is lifted.

<snippet>
...
public static void main(final String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyApp(args).show();
}
});
}
</snippet>

..Sheesh!

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200711/1

 
Reply With Quote
 
Ramon F Herrera
Guest
Posts: n/a
 
      11-11-2007
On Nov 10, 8:04 pm, "Andrew Thompson" <u32984@uwe> wrote:
> Lew wrote:
>
> .
>
> >> frame.setVisible(true);

>
> ><http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.htm...>

>
> I sometimes wonder 'What could possess people to use
> methods deprecated in 1.2?'.
>
> One source is the unmaintained Sun Java Tutorial codes
> that use it, and apparently another is the Java 6.0 package
> docs., from which this example is lifted.
>
> <snippet>



Lew & Andrew:

Given the weaknesses of Swing, which in addition to being thread-
unsafe has bugs that continuously crash the JRE in 1.6, should I avoid
it altogether and concentrate on learning SWT (*)?

-Ramon

(*) JFaces included


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-11-2007
Ramon F Herrera wrote:
> Given the weaknesses of Swing, which in addition to being thread-unsafe


As compared to what other graphics library?

All the GUI APIs are thread-unsafe AFAIK.

> has bugs that continuously crash the JRE in 1.6,


What bugs are those?

> should I avoid it altogether and concentrate on learning SWT (*)?


I'd stick with Swing.

> (*) JFaces included


I'm not familiar with JFaces.

--
Lew
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-11-2007
Lew wrote:
>> Given the weaknesses of Swing, which in addition to being thread-unsafe

>
>As compared to what other graphics library?
>
>All the GUI APIs are thread-unsafe AFAIK.


AWT* is 'fine' that way - it is 'thread-safe' AFAIU.

Otherwise I agree with the gist of the trimmed text.

* But no, I would not recommend using AWT just because
you need a handful of extra lines to ensure Swing works
as expected. I imagine SWT is the same, though I have not
bothered to check.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200711/1

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57