gpt4 book ai didi

java - JTable 中的换行、右对齐、自动调整行高

转载 作者:行者123 更新时间:2023-11-30 06:03:35 24 4
gpt4 key购买 nike

我编写了一个 Java 应用程序,我需要一个具有一些功能的 JTable 类对象:

  1. 自动缠绕线
  2. 右对齐
  3. 自动调整行高(这意味着,例如,行的大小为 25,但表中另一行的大小为 50,行的内容大小)
  4. 渲染速度必须

但是我的代码运行速度不快,并且没有完全的顶级功能,我在最后一个问题中编写了应用程序的最小版本:

这是我的 gui 类:

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame {
private BankTable table;
private JScrollPane scrollPane;

public GUI(){
super("Bank Table");
JPanel contentPanel = new JPanel();
setContentPane(contentPanel);
contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
setLayout(new BorderLayout());
setMinimumSize(new Dimension(1000,700));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

makeTable();

scrollPane = new JScrollPane(table);
scrollPane.getVerticalScrollBar().setUnitIncrement(50);

add(scrollPane,BorderLayout.CENTER);

setVisible(true);
}

public void makeTable(){
String[][]data= new String[][]{{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/01"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/02"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/03"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/04"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/05"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/06"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/07"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/08"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/09"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/10"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/11"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/12"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/13"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/14"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/15"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/16"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/17"},
{"0212670003009", "ص 318", "77081111111111111111111111111111111111634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/18"},
{"0212670003009", "ص 318", "77081222222222222222222222222222634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/19"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/20"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/21"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/22"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/23"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/24"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/25"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/26"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/27"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/28"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/29"},
{"0212670003009", "ص 318", "77081634","38000000","0","331142","2102","وصول چک","08:56:46","1397/05/30"}};

String[] columns= new String[]{"شماره حساب", "اطلاعات اضافه", "مانده","واریز","برداشت","فیش/ حواله","کد شعبه","شرح","ساعت","تاریخ"};

table = new BankTable(data,columns);
table.updateRowHeights();
System.out.println("Table");
}

public static void main(String args[]){
GUI gui = new GUI();
}

}

这是表格的渲染:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;

public class TextAreaRenderer extends JTextArea
implements TableCellRenderer {

public TextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

setEditable(false);
setCursor(null);
setOpaque(false);
setFocusable(false);
setLineWrap(true);
setWrapStyleWord(true);

}

public Component getTableCellRendererComponent(JTable jTable,
Object obj, boolean isSelected, boolean hasFocus, int row,
int column) {
setText((String)obj);
setFont(new Font("bnazanin", Font.BOLD, 15));

return this;
}
}

这是表类:

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

public class BankTable extends JTable {

public BankTable(String[][] data,String[] columns){
super(data,columns);

setForeground(Color.BLACK);
setBounds(60,80,400,600);

changeTableHeader();

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(JLabel.RIGHT);

TextAreaRenderer textAreaRenderer = new TextAreaRenderer();

for(int i=0 ; i<columns.length ; i++){
getColumnModel().getColumn(i).setCellRenderer(textAreaRenderer);
}
}

public void changeTableHeader(){
getTableHeader().setBackground(new Color(57,77,112));
getTableHeader().setForeground(Color.WHITE);
getTableHeader().setFont(new Font("Calibri Light", Font.BOLD, 20));

}

@Override
public boolean isCellEditable(int i, int i1) {
return false;
}

public void updateRowHeights()
{
for (int row = 0; row < getRowCount(); row++)
{
int rowHeight = getRowHeight();

for (int column = 0; column < getColumnCount(); column++)
{
Component comp = prepareRenderer(getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
}
setRowHeight(row, rowHeight + 20);
}
}
}

编辑: This is a simple for my table

最佳答案

这里有值(value)的是我闲置的一些旧代码。

它使用带有换行功能的 JTextArea,但将文本区域中的行数限制为 2 行。然后,您可以滚动文本区域以查看其他行。

也许这种方法可以满足您的要求?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableTextArea extends JFrame
{
public TableTextArea()
{
JTable table = new JTable(40, 5);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setRowHeight(40);
table.setValueAt("one two three four five six seven eight nine ten", 0, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 1, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 0, 4);
table.setValueAt("one two three four five six seven eight nine ten", 2, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 3, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 4, 4);
table.setValueAt("one two three four five six seven eight nine ten", 5, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 6, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 7, 4);
table.setValueAt("one two three four five six seven eight nine ten", 8, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 9, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 10, 4);
table.setValueAt("one two three four five six seven eight nine ten", 11, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 12, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 13, 4);
table.setValueAt("one two three four five six seven eight nine ten", 14, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 15, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 16, 4);
table.setValueAt("one two three four five six seven eight nine ten", 17, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 18, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 19, 4);
table.setValueAt("one two three four five six seven eight nine ten", 20, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 21, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 22, 4);
table.setValueAt("one two three four five six seven eight nine ten", 23, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 24, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 25, 4);
table.setValueAt("one two three four five six seven eight nine ten", 26, 2);
table.setValueAt("aaa bbb ccccc dddd eeee fff ggggg hhhhh iiii jjj", 27, 2);
table.setValueAt("1111 2222 3333 4444 5555 6666 7777 8888 9999 0000", 28, 4);
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );

// Override default renderer for a specific column

TableCellRenderer renderer = new TextAreaRenderer();
table.getColumnModel().getColumn(2).setCellRenderer( renderer );
table.getColumnModel().getColumn(4).setCellRenderer( renderer );
table.changeSelection(0, 0, false, false);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TableTextArea frame = new TableTextArea();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
});
}

/*
**
*/
class TextAreaRenderer implements TableCellRenderer
{
private JTextArea renderTextArea;
private JScrollPane renderScrollPane;
private JTextArea focusTextArea;
private JScrollPane focusScrollPane;
private boolean firstTime = true;

public TextAreaRenderer()
{
renderTextArea = new JTextArea();
renderTextArea.setEditable( false );
renderTextArea.setLineWrap( true );
renderTextArea.setWrapStyleWord( true );
renderScrollPane = new JScrollPane( renderTextArea );
renderScrollPane.setBorder(null);
renderScrollPane.revalidate();

focusTextArea = new JTextArea();
focusTextArea.setEditable( false );
focusTextArea.setLineWrap( true );
focusTextArea.setWrapStyleWord( true );
focusScrollPane = new JScrollPane( focusTextArea );
focusScrollPane.setBorder(null);
}

public Component getTableCellRendererComponent(
final JTable table, Object value, boolean isSelected, boolean hasFocus, final int row, final int column)
{
// For some reason the scrollbars don't appear on the first cell renderered.
// Forcing a repaint of the cell seems to fix the problem.

if (firstTime)
{
firstTime = false;
Rectangle cellRectangle = table.getCellRect(row, column, false);
// renderScrollPane.setBounds(cellRectangle);
// table.add(renderScrollPane);
// renderScrollPane.revalidate();
table.repaint(cellRectangle);
}

System.out.println(row + " :: " + column + " : " + hasFocus);
table.remove(focusScrollPane);

renderTextArea.setText( value != null ? value.toString() : "" );
renderTextArea.setCaretPosition(0);

if (hasFocus)
{
renderTextArea.setBackground( table.getSelectionBackground() );

SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
addRealTextAreaToTable(table, row, column);
}
});
}
else if (isSelected)
renderTextArea.setBackground( table.getSelectionBackground() );
else
renderTextArea.setBackground( table.getBackground() );

return renderScrollPane;
}

private void addRealTextAreaToTable(JTable table, int row, int column)
{
System.out.println(row + " :: " + column);
Object value = table.getValueAt(row, column);
focusTextArea.setText( value != null ? value.toString() : "" );
focusTextArea.setCaretPosition(0);
// focusTextArea.setBackground( table.getBackground() );
focusTextArea.setBackground( table.getSelectionBackground() );
Rectangle cellRectangle = table.getCellRect(row, column, false);
focusScrollPane.setBounds(cellRectangle);
table.add(focusScrollPane);
focusScrollPane.revalidate();
focusTextArea.requestFocusInWindow();
}
}
}

关于java - JTable 中的换行、右对齐、自动调整行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51806517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com