- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个 Java 应用程序,我需要一个具有一些功能的 JTable
类对象:
但是我的代码运行速度不快,并且没有完全的顶级功能,我在最后一个问题中编写了应用程序的最小版本:
这是我的 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);
}
}
}
最佳答案
这里有值(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/
我已经多次看到我的问题被问到,但我从未看到我期望的答案。我已经在 JTable 中输入了数据库的元素,并且我希望能够通过一些 JButtons 删除/添加元素。问题是当我添加/删除时,修改在数据库
我正在使用 JTable 做一个项目,我想让我的表格单元格可编辑。我用过, public boolean isCellEditable(int row, int column) {
我正在编写一个应用程序包,其中包含一个主类,其中主方法与GUI类分开,GUI类包含一个带有jtabbedpane的jframe,它有两个选项卡,第一个选项卡包含一个jtable,称为jtable1,第
我制作了一个表格来将 Arraylist 中的数据加粗,但如果我删除该数据,我会希望该表格更新并取消对 Arraylist 中加粗的单元格的粗体显示。我该怎么做呢?或从另一个类关闭该类的实例 最佳答案
如果我的 JTable 的列未按字母顺序排列,我可以使用 getSelectedRows() 并毫无问题地获取它们的行的值。但是,如果用户单击列名并且行在该列中按字母顺序排列,则 getSelecte
我有一个 JTable,用户可以在其中在单元格中输入数据。然后有一个“保存”按钮,用于收集表格数据,将其格式化为 csv,并将其保存到文件中。 但是,如果用户将最后编辑的单元格保留在选定状态,并单击“
我编写了下面的代码,以便在当前 JTable 上进行选择时创建一个新的 JTable: makeTbale.addActionListener(new ActionListener() { pub
我正在使用 Swing 编写 Java 应用程序。我有两个表,我必须将内容从一个表复制到另一个表(复制)。问题是,如果我清除目标表行,那么我的源表行也会被删除。 如果我按 CopyAll,那么我会将
我一直致力于JTable,我的项目: 从数据库读取数据(我完成了这个任务并能够在JTable中显示)。 然后将数据按子组显示并保存到文件(文本/Excel)中。 我有 JTable 的基本知识,并且使
我有以下类(class): public class customer_master extends javax.swing.JInternalFrame { Connection con =
您好,我是 JAVA 的新手,在学习时正在开发 GUI。我创建了一个带有 ScrollPane 和 JTable 的 JFrame。当我增加 2 列以上时,第一行下方的数据不会显示。 此外,当我的 J
我正在进行项目的最后一部分,这是我遇到的最后问题之一。这部分用于编辑预订,即更改为特定预订预订的房间。我有 2 个 JTable,其中一个有可用房间,另一个有已预订的房间。两者都有单独的 Defaul
我有 2 个 JTable,我需要从表 2 中复制特定列(包括该列中的所有数据)并将其添加到表 1 中的下一个空闲列中。有人知道执行此操作的最佳方法吗? 谢谢 最佳答案 DefaultTableMod
美好的一天!我在 Jtable 方面遇到了困难。我已经阅读和浏览了各种教程,但我不太明白它的要点。我的问题是,我必须从 jtable (jTable1) 中选择包含 (ClientID、LastNam
我的 SERVER 表单上有一个 JTable,它是从 MySQL 数据库填充的,在构造函数中编码: String sql = "SELECT * from fiekorari"; t
我在我的项目上工作,我需要将一行从 JTable 复制到另一个 JTable,第二个 JTable 应该只是单行表。我为第一个 JTable 创建了 mouselistener,双击它应该复制行并将其
我有这段代码可以完全按预期工作 package com.grantbroadwater.signInAssistant.view; import java.awt.BorderLayout; impo
我有一个列表人员(在 jTable 中)并想将其导出到 excel 文件我需要每个人转到单独的工作表所以我需要拆分原始 jTable,但我不知道如何? 这就是我想做的? public void exp
我有一个包含 7 列和 2 行的 JTable。在我的 JTable 下方,我有一个 JTextField。当我在 JTextField 中输入内容时,我可以很容易地得到我输入的内容:String l
我正在尝试将一个 JTable 嵌套在另一个 JTable 的列中(使用 CellRenderer)。 示例(错误)输出: 为什么下面的例子没有输出表中表? import java.awt.Compo
我是一名优秀的程序员,十分优秀!