5th col dies, cols 6 thru 15 move 1 col left, 15th col is born

C

clusardi2k

What is the best way to build the following application.

I would like to create an application that when it opens it will have 300 rows and 15 columns.

The first 4 columns on the left side will be updated with alphanumeric data but will always be displayed. They never disappear.

The remaining 11 columns (also containing alphanumeric data) will move to the left with the leftmost column (column 5) disappearing and a brand new column 15 will appear on the right side of the window. Column 15 never existed before.

Again, I want an application with 15 columns always displayed with the 5th column leaving (data not saved) the application, columns 6 through 15 will move one column to the left, and the 15th column will be populated with new data.

What's the best way to do this. Do you have example code for me.

Thank you,
 
K

Knute Johnson

What is the best way to build the following application.

I would like to create an application that when it opens it will have
300 rows and 15 columns.

The first 4 columns on the left side will be updated with
alphanumeric data but will always be displayed. They never
disappear.

The remaining 11 columns (also containing alphanumeric data) will
move to the left with the leftmost column (column 5) disappearing and
a brand new column 15 will appear on the right side of the window.
Column 15 never existed before.

Again, I want an application with 15 columns always displayed with
the 5th column leaving (data not saved) the application, columns 6
through 15 will move one column to the left, and the 15th column will
be populated with new data.

What's the best way to do this. Do you have example code for me.

Thank you,

Are these data entry fields? Or just to be displayed? Probably just
drawing the data on a JComponent would be the easiest and quickest but
would take a little more effort to create. JLabels are nice too.
 
C

clusa

Are these data entry fields? Or just to be displayed? Probably just drawing the
data on a JComponent would be the easiest and quickest but would take a little
more effort to create. JLabels are nice too. -- Knute Johnson

No, the data in the cols comes from files. The user does not enter the data.

Thank you,
 
C

clusa

Are these data entry fields? Or just to be displayed? Probably just drawing the
data on a JComponent would be the easiest and quickest but would take a little
more effort to create. JLabels are nice too. -- Knute Johnson

So, you suggest to use a JComponent with (300 x 15) JLabels.

Why would using a JComponent be better than using a JFrame containing JPanels.

With a JComponent, will I be able to easily change the color of the JLabels.

I guess I should use a Layout also.

I was planning to have the columns move left after N seconds.

Are there any other details that I should worry about such as flicker.

Thanks,
 
M

Manuel Collado

El 20/04/2013 1:54, (e-mail address removed) escribió:
What is the best way to build the following application.

I would like to create an application that when it opens it will have
300 rows and 15 columns.

The first 4 columns on the left side will be updated with alphanumeric
data but will always be displayed. They never disappear.

The remaining 11 columns (also containing alphanumeric data) will move
to the left with the leftmost column (column 5) disappearing and a brand
new column 15 will appear on the right side of the window. Column 15
never existed before.

Again, I want an application with 15 columns always displayed with the
5th column leaving (data not saved) the application, columns 6 through
15 will move one column to the left, and the 15th column will be
populated with new data.

What's the best way to do this. Do you have example code for me.

If your input and output data are just text files you could consider using
awk instead of Java.
 
E

Eric Sosman

What is the best way to build the following application.

I would like to create an application that when it opens it will have 300 rows and 15 columns.

The first 4 columns on the left side will be updated with alphanumeric data but will always be displayed. They never disappear.

The remaining 11 columns (also containing alphanumeric data) will move to the left with the leftmost column (column 5) disappearing and a brand new column 15 will appear on the right side of the window. Column 15 never existed before.

Again, I want an application with 15 columns always displayed with the 5th column leaving (data not saved) the application, columns 6 through 15 will move one column to the left, and the 15th column will be populated with new data.

What's the best way to do this. Do you have example code for me.

JTable should be straightforward. You'd extend AbstractTableModel
to handle the "sliding," calling fireTableDataChanged() to update the
display. Conveniences like scrolling would come for free (almost),
and the JTable's display might well be more efficient than managing
4500 JLabels.

No, I don't have example code.
 
K

Knute Johnson

So, you suggest to use a JComponent with (300 x 15) JLabels.

Why would using a JComponent be better than using a JFrame containing JPanels.

With a JComponent, will I be able to easily change the color of the JLabels.

I guess I should use a Layout also.

I was planning to have the columns move left after N seconds.

Are there any other details that I should worry about such as flicker.

Thanks,

No, what I was suggesting was that you draw your data on the JComponent,
or JPanel if you want, rather than use components as it will render much
quicker and you can do a fade in/out or change colors when you move your
data pretty easily. It would be easier to scale as well.

import java.awt.*;
import java.awt.event.*;

public class test extends JComponent implements Runnable {
private static final String text = "Now is the time for all good
men to " +
"come to the aid of their country. The quick brown fox jumped
over the " +
"lazy red dog. Four score and seven years ago our fathers brought
forth" +
"on this continent a new nation conceived in liberty and dedicated
to " +
"the proposition that all men are created equal.";
private final String[][] words = new String[6][6];
private volatile int n;

public test() {
setPreferredSize(new Dimension(400,300));

String[] array = text.split(" ");
for (int i=0; i<6; i++)
for (int j=0; j<6; j++)
words[j] = array[n++];
n = 0;

setFont(new Font("Monospaced",Font.PLAIN,10));
}

public void paintComponent(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0,0,getWidth(),getHeight());

g.setColor(Color.BLUE);
for (int cols=0; cols<6; cols++)
for (int rows=0; rows<6; rows++)
g.drawString(words[cols][rows],10+60*cols,20+15*rows);
}

public void run() {
while (true) {
++n;
repaint();
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
String[] temp = new String[6];
for (int i=0; i<6; i++)
temp = words[2];

for (int i=2; i<5; i++)
for (int j=0; j<6; j++)
words[j] = words[i+1][j];

for (int i=0; i<6; i++)
words[5] = temp;
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
test t = new test();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(t,BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
new Thread(t).start();
}
});
}
}
 
C

clusa

JTable should be straightforward. You'd extend AbstractTableModel to handle
the "sliding," calling fireTableDataChanged() to update the display.
Conveniences like scrolling would come for free (almost), and the JTable's
display might well be more efficient than managing 4500 JLabels. No, I don't
have example code. -- Eric Sosman (e-mail address removed)

I would also be able to eloquently color specific cells.

Thanks
 
E

Eric Sosman

I would also be able to eloquently color specific cells.

You can be as eloquent as you like with a TableCellRenderer,
most likely an extension of DefaultTableCellRenderer. This time,
I've got some example code (not eloquent, and not colorized, but
it should demonstrate the approach):

/**
* A {@code TableCellRenderer} that centers displayed text.
*/
class CenteredRenderer extends DefaultTableCellRenderer {

private static CenteredRenderer instance;

static CenteredRenderer instance() {
if (instance == null) {
instance = new CenteredRenderer();
}
return instance;
}

private CenteredRenderer() {
assert SwingUtilities.isEventDispatchThread();
}

@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean select, boolean focus, int row, int col) {
assert SwingUtilities.isEventDispatchThread();
Component comp = super.getTableCellRendererComponent(table,
value, select, focus, row, col);
if (comp instanceof JLabel) {
((JLabel) comp).setHorizontalAlignment(SwingConstants.CENTER);
}
return comp;
}
}

I use it this way, in the constructor of a class extending
JTable:

TableColumnModel colmod = getColumnModel();
for (int c = 0; c < COLUMNS; ++c) {
TableColumn col = colmod.getColumn(c);
if ( /* I want this column centered ... */ ) {
col.setCellRenderer(CenteredRenderer.instance());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,851
Latest member
CristineKo

Latest Threads

Top