9 Replies - 2497 Views - Last Post: 01 November 2010 - 07:26 AM Rate Topic: -----

#1 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Is there a conditional formatting similar thing with java?

Posted 28 October 2010 - 09:13 PM

I have MS Excel 2010 and am loving the conditional formatting. granted I know that openoffice has had it forever but excel has more eye candy in this regard. What i'm interested in implementing in one of my programs is the data bars in the conditional formatting. At first I thought I could use JProgress bar (which was graphically similar) but then thought differently.

it's for a game I play. I'm a guild leader and I keep track of gold donations and guild battles. In my excel sheet I enter in each week everyone's donations. My sheet updates the values and compares the new ones to last weeks and lets me know how much everyone has donated. Excel then makes a nice data bar. problem with excel is (i know I should use access or base for this problem) is that when someone leaves my guild or a new person is added I have to shuffle all the values around until everyone is back in order, hence why i'm making my program. My program is mostly complete. I can enter in my data and then it saves it to individual files. I'm currently working on the "make report" button. So i figured I'd just ask if there was a similar feature with java that anyone could think of, or am I asking for too much of my program?

Is This A Good Question/Topic? 0
  • +

Replies To: Is there a conditional formatting similar thing with java?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Is there a conditional formatting similar thing with java?

Posted 28 October 2010 - 09:57 PM

Don't really understand what you mean. A kind of bar or conditonnal formatting ? Formatting of what ?

For the bar you can use a JProgressBar or an array of 100 (or less or more) JLabel opaque in a GridLayout(100,1). Just set the background color of these JLabel to your needs.
just set the background color of the JLabel.

Java permist all kind of formatting plus the ones of the <HTML> tag so you will have to explain more what you want to achieve.
Was This Post Helpful? 1
  • +
  • -

#3 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Is there a conditional formatting similar thing with java?

Posted 29 October 2010 - 07:47 AM

PBL i think i'm giong to use your suggestion. about using 100 labels...well it will actually be about 2,000 labels, but up to 5,000 if my guild ever gets 50 people.

I think i'll make my frame kinda like this
progressFrame.setLayout(3,50) //50 rows, 3 columns

for (int a=0; a<Active_Guild_Members; a++)
{
  progressFrame.add(Guild_Member_Name_Label[a]);
  progressFrame.add(Guild_Member_Donation_total[a]);
  progressFrame.add(Hundred_Labels_in_this_panel[a]);
}



granted that's not actual code, but I think that will work. I sorta only wanted 2 columns and have the Donation total be on top of the background color.

I'll give it a try and see what I can come up with. Thank you PBL. I had no idea about the <HTML> tags... Here is link of what i"m trying to copy from excel into java.

http://blogs.msdn.co...excel-2010.aspx

I would have used the link button but it wasn't working for me sorry.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Is there a conditional formatting similar thing with java?

Posted 29 October 2010 - 08:26 AM

Perhaps you should look at using JTable instead. If you use a DefaultTableModel for it, it will be really easy add and remove rows this way, as well as organize your data better. No need to manage 5000 JLabels or JComponents, as the JTable does that.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Is there a conditional formatting similar thing with java?

Posted 29 October 2010 - 01:41 PM

And if you are using Excel, you can change your data into a table. Then, if you need to remove a column or row, just do it and then click the column header. This will automatically sort it for you.

http://office.micros...H010102459.aspx
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1953
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: Is there a conditional formatting similar thing with java?

Posted 29 October 2010 - 06:55 PM

I'd go with a JTable and a custom cell renderer. Quite easy if you override the DefaultTableCellRenderer.

1. Add a field for the percentage (like a progress bar)

2. Override the getTableCellRendererComponent(...) method and detect if the value is a number. Work out and set the percentage (zero if no number). Then call super.getTableCellRendererComponent(...)

3. Override the paintComponent(...) method. Draw the progress rectangles then call super.paintComponent(...). You'll have to make sure the component is not opaque too or your rectangles may get grayed out.
Was This Post Helpful? 2
  • +
  • -

#7 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Is there a conditional formatting similar thing with java?

Posted 30 October 2010 - 10:36 PM

ok so i took your suggestion. I found that you can make a progress bar figure out the percentage deal for you with it's constructor new JProgressBar(int min, int max) which is awesome. so i'll have to figure that out. but when i give it temporary values the progressbar doesn't reflect it. here is my code
package dungeontracker;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

class ReportFrame extends JFrame
{
  private String[] columns = {"Name", "Donated Gold", "Level", "+ Levels"};
  private Object[][] data;
  private ArrayList<GameCharacter> guilders;
  private JTable reportTable;

  public ReportFrame(ArrayList<GameCharacter> ImportGuild)
  {

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLayout(new BorderLayout());
    guilders = ImportGuild;

    reportTable = new JTable(new MyTabled());

    JScrollPane SP = new JScrollPane(reportTable);
    reportTable.setFillsViewportHeight(true);
    TableColumn col = reportTable.getColumnModel().getColumn(1);
    col.setCellRenderer(new ColorRenderer(500));
    add(reportTable.getTableHeader(), BorderLayout.PAGE_START);
    add(reportTable, BorderLayout.CENTER);
    pack();
    setVisible(true);
  }

  private class MyTabled extends AbstractTableModel
  {
    public int getRowCount()
    {
      return guilders.size();
    }

    public int getColumnCount()
    {
      return columns.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex)
    {
      return data[rowIndex][columnIndex];
    }

    public Class getCellClass(int rowIndex, int columnIndex)
    {
      return data[rowIndex][columnIndex].getClass();
    }

    public MyTabled()
    {
      data = new Object[guilders.size()][columns.length];
      for (int a = 0; a < guilders.size(); a++)
      {
        data[a][0] = guilders.get(a).getName();
        data[a][1] = "450"; //temp value
        data[a][2] = "200"; //temp value
        data[a][3] = "300"; //temp value
      }
    }
  }
  public class ColorRenderer extends JProgressBar implements TableCellRenderer
  {
    public ColorRenderer(int max)
    {
      super(0,max);

      setOpaque(true); //I was told I MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column)
    {
      int min = Integer.parseInt(value.toString());
      setMinimum(min);
      repaint();
      return this;
    }
  }
}



it displays my name column perfect, and I forgot to make the column headers work in this.. but i'm too tired to figure it out now...but it did display those good.

the next column is a bunch of progressbars (YEAH!!!) but they are all set to 0% progress.. and i dont' know why.. they should be at 450/500 (or so i think) then it displays the other numbers that i told the data to be temporary. or should i do it as a Integer..i dont know.. i'm tired bet time..

what am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1953
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: Is there a conditional formatting similar thing with java?

Posted 01 November 2010 - 03:04 AM

Surely you want to setValue(), not setMinimum()?
Was This Post Helpful? 1
  • +
  • -

#9 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Is there a conditional formatting similar thing with java?

Posted 01 November 2010 - 07:24 AM

:) :) :) :) :)

YOU ARE THE AWESOME
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1953
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: Is there a conditional formatting similar thing with java?

Posted 01 November 2010 - 07:26 AM

Glad it's working. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1