JTable:如何在中心对齐数据或如何设置行背景颜色交替

时间:2015-06-23 07:57:44

标签: java swing jtable background-color defaulttablemodel

如何在var ns = $('.sortable-list').nestedSortable({ forcePlaceholderSize: true, handle: '.handle', helper: 'clone', items: 'li', opacity: .9, placeholder: 'sort-placeholder', revert: 250, tabSize: 15, tolerance: 'pointer', toleranceElement: '> div', maxLevels: 4, isTree: true, expandOnHover: 700, startCollapsed: true, stop: function() { updateList(); setTimeout(updateList, 100); } }); 中设置行背景颜色,并将数据与中心对齐。JTable是动态的。JTable1是动态的。 JTable使用defaultTableModel来设置数据。我希望i%==0如果背景颜色为#Color.GRAY,则#Color.WHITE我读了渲染类,但无法理解渲染器类

import java.awt.Dimension;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

class FacultyList extends JPanel {

    static DefaultTableModel dm;
    JTable table;
    JScrollPane jsp_table;
    static Connection conn;
    static Statement stmt;
    static ResultSet rs;

    public FacultyList(Connection c) {
        conn = c;
        String col_name[] = {"S.No.", "Name", "Father Name", "College ID", 
            "Gender", "Date of Birth", "Branch", "Contact No."};
        dm = new DefaultTableModel(null, col_name);
        table = new JTable(dm);
        table.getTableHeader().setFont(new Font("Goudy Old Style", Font.BOLD, 15));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.getColumnModel().getColumn(0).setPreferredWidth(50);
        table.getColumnModel().getColumn(1).setPreferredWidth(150);
        table.getColumnModel().getColumn(2).setPreferredWidth(150);
        table.getColumnModel().getColumn(3).setPreferredWidth(125);
        table.getColumnModel().getColumn(4).setPreferredWidth(50);
        table.getColumnModel().getColumn(5).setPreferredWidth(125);
        table.getColumnModel().getColumn(6).setPreferredWidth(225);
        table.getColumnModel().getColumn(7).setPreferredWidth(150);
        jsp_table = new JScrollPane(table);
        jsp_table.setPreferredSize(new Dimension(975, 520));
        addRowTable();
        add(jsp_table);
    }

    public static void addRowTable() {
        try {
            int a = dm.getRowCount();
            int i = 0;
            while (i < a) {
                dm.removeRow(0);
                i++;
            }
            String fac = "Faculty";
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
                    ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery("SELECT NAME, FNAME, CLG_ID, GANDER, "
                    + "DOB, BRANCH, CONTACT FROM PROFILE "
                    + "where I_AM = '" + fac + "'");
            int count = 1;
            while (rs.next()) {
                String s[] = new String[8];
                s[0] = "" + count;
                s[1] = rs.getString(1);
                s[2] = rs.getString(2);
                s[3] = rs.getString(3);
                s[4] = rs.getString(4);
                s[5] = rs.getString(5);
                s[6] = rs.getString(6);
                s[7] = "" + rs.getLong(7);
                count++;
                if (i % 2 == 0) {
                } else {
                }
                dm.addRow(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

在这个网站上或java oracle doc

  • TableCellRenderer只是关于绘画,是基于XxxTableCellRenderer中的设置的静态绘画,不是交互式的,是关于绘画错觉的,

  • XxxTableCellRenderer中绘画是非常有意义的,并且是从所有关键和鼠标事件中调用的,在相关API中实现的方法

  • 它可以是JTable的单独定义,也可以是单独的类,它返回rulles,JTable视图的绘制定义

  • JDBC不是设置或定义渲染器的好地方,关于将新数据从数据库添加到DefaultTableModel

  • 无法为ResultSet.CONCUR_UPDATABLE设置
  • JTable,是来自ResultSet的快照,它没有任何操作回数据库

  • 为什么JDBC的回复仅限于String s[] = new String[8];,设为data typed correctly by override getColumnClass

  • 在Renderer中设置对齐(数据类型默认返回alingment),例如,通过强制转换为JLabel,然后JLabels API中实现的所有方法都可以在那里使用

  • 默认使用prepareRenderergetTableCellRendererComponent,用于整个JTable视图,并且Renderer的各种尝试仅适用于具体列< / p>

答案 1 :(得分:1)

您可以使用自定义渲染器执行此操作。它可以按如下方式完成:

 table1 = new JTable(){

public Component prepareRenderer(TableCellRenderer tcr, int row,
    int column) {
    Component c = super.prepareRenderer(tcr, row, column);

    if (isRowSelected(row)) {

        c.setForeground(getSelectionForeground());
        c.setBackground(getSelectionBackground());

    } else {

        c.setForeground(getForeground());
        c.setBackground((row % 2 == 0) ? getBackground()
            : Color.lightGray);
    }

    int rendererWidth = c.getPreferredSize().width;
    TableColumn tableColumn = getColumnModel().getColumn(column);
    tableColumn.setPreferredWidth(Math.max(rendererWidth
        + getIntercellSpacing().width,
        tableColumn.getPreferredWidth()));

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(SwingConstants.CENTER);
table1.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);
return c;
    }

  };;