Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2007

Tip: Looking for answers? Try searching our database.

Advanced Swong jTable cells

Thread view: 
vachacz - 21 Jul 2007 19:47 GMT
Hi !

I'm writing swing application that shall compare differences between
objects. I have a jTable, and its every row represents one entity. In
database i have corresponding states of that object and it is possible
to show differences between these states in table cells. I have my own
table model

    public Object getValueAt(int rowNumber, int columnNumber) {
        if (rowNumber < 0 || rowNumber >= getRowCount())
            return "";

        Object obj = objectList.get(rowNumber);

        switch (columnNumber) {
                       ....
            case 2:
                return obj.getSomeValue()
                       ...
                       case x:
                              return new
ComparableValues(obj.getSomValue(), value to compare with);
              }
   }

I also have specific TableCellRenderer attached to column, it acts
like this

class MyCellRenderer extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);

            if (value instanceof ComparableValues) {
                               // cast
                               setText("(" + value.getDifference() +
" )" + value.getOriginalValue())
                       }
       }
}

Everything works great, but i would like to improve cell design ...

Q1) How to use more than one text color in cell ?
Q2) How to use many font sizes in one cell ?

I have one idea but i don't like it ;) (MyTableCellRendere extends
JLabel and use html as a content ... )

in advance, thx for any help
vachacz - 21 Jul 2007 19:50 GMT
sorry for misprint in topic title ...
vachacz - 21 Jul 2007 20:47 GMT
solved ;)

class MyTableCellRenderer extends JPanel implements TableCellRenderer
{

    private JLabel leftValue = new JLabel();
    private JLabel rightValue = new JLabel();

    public SkillsTableCellRenderer() {
        leftValue.setBorder(BorderFactory.createEtchedBorder());
        rightValue.setBorder(BorderFactory.createEtchedBorder());

        this.add(leftValue);
        this.add(rightValue);
    }

    public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
              leftValue.setText(originalText)
              rightValue.setText(valueToCompare)
       }
}

Both left and right labels may have their own style. This is pretty
quick solution. I've also tested solution with HTML, it also works,
but it is slow

Hopefully it will help someone in future ... :)
Roedy Green - 23 Jul 2007 05:29 GMT
>sorry for misprint in topic title ...

Your use of the an archaic past tense of "Swing" reminds me of an old
joke.

A man went to New England on his holiday.  He was eager to sample the
fresh fish of the region, collectively termed "scrod". He went down to
the local pier and asked, "Do you know where I an get scrod?".  The
old salt replied, "I've been asked that question many times before,
but never in the pluperfect subjunctive."

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 23 Jul 2007 05:26 GMT
>Q1) How to use more than one text color in cell ?
>Q2) How to use many font sizes in one cell ?

See http://mindprod.com/jgloss/jtextarea.html
and http://mindprod.com/jgloss/jtextfield.html

jTextField.setContentType( "text/html" );

Then you can embed simple HTML in your component.  Keep in mind that
you are recycling the same component over and over for the various
rows, so always start from scratch in your rendering setting colours
and fonts.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.