PDA

View Full Version : تغییر رنگ رکوردی در jtable



shirin_h
یک شنبه 14 بهمن 1386, 12:12 عصر
چطور می توان رنگ رکوردی را که از درون کد آن را انتخاب کرده ایم در jtable تغییر داد و این رکورد اگر در پایین است و دیده نمی شود آن را نشان دهد؟

javaphantom
یک شنبه 14 بهمن 1386, 14:27 عصر
از طریق یک event طوری هندل کن که متدود rowAtPoint که یک مقدار int بر می گردونه بطونه بهت بگه آیا روی row دلخواه هستی یا نه.
یا از طریق setSelectionBackground که یک cell را رنگی می کنه کافی هست که بدونی کدام row می باشه بعد به تعداد columnها از این متدود استفاده کن.
برای اینکه بتونی یک scroll هم داشت باشی کل JTable رو بنداز توی JScrollPane
اون موقع می تونی عمل رول کردن هم انجام بدی

saeedIRHA
یک شنبه 14 بهمن 1386, 19:46 عصر
یک مثال برای تغییر رنگ JTable



import java.awt.Color;

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
Color alternateColor = new Color(252,242,206);
Color whiteColor = Color.WHITE;


}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jTable1.setAutoCreateRowSorter(true);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"File", "Size", "Speed"
}
));
jTable1.setSelectionBackground(new java.awt.Color(255, 255, 0));
jTable1.setSelectionForeground(new java.awt.Color(0, 0, 255));
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(317, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration//GEN-END:variables

}

shirin_h
دوشنبه 15 بهمن 1386, 10:40 صبح
jTable1.changeSelection((data[0]), 2, false, false);
jTable1.setSelectionBackground(java.awt.Color.PINK );


با این کد جواب داد