gpt4 book ai didi

java - JScrollPane 打破从右到左的方向

转载 作者:行者123 更新时间:2023-11-30 11:29:14 25 4
gpt4 key购买 nike

我的应用程序分为两个主要部分:

  • 一个由 JLabelJTableHeader 组成的 header >
  • 一个 JScrollPane 包含一个 JTable

JTableHeader 位于单独面板中的原因是它不应滚动。表格中的所有列都设置了首选/最大宽度(中间的列除外),因此当调整框架大小时,只有列应该更改其宽度。

编辑:我相信我可以使用 JScrollPane.setColumnHeaderView( JTable.getTableHeader() ); 让表头保持静态 - 我得到的代码是这样的,改变它不会据我所知,这会影响手头的问题。

我遇到了 JScrollPane 打破 JTableHeader 从右到左 (RTL) 方向的问题。 JTableHeader 将在左侧对齐,而 JTable 将在右侧对齐。

问题类似于发布的问题 here这指向这个 Unresolved bug .我不完全确定这是我看到的问题,因为 JTableHeader 不应该在滚动 Pane 中。此外,建议的解决方案不起作用,因为我确实需要将滚动条放在左侧,因为它在 RTL 方向上是预期的。

如何修复 JTableHeader 的方向,同时仍然保持自动调整列大小的能力?

编辑:向 tablePanel 添加了一个 JButton,因为它应该在那里。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class SwingTest {

public static void main( String[] args ) {
final JFrame frame = new JFrame( "RTL Test" );

//ComponentOrientation orientation = ComponentOrientation.LEFT_TO_RIGHT;
ComponentOrientation orientation = ComponentOrientation.RIGHT_TO_LEFT;

frame.setComponentOrientation( orientation );

/* Build and populate table */
String data[][] = {
{ "1", "foo", "bar", "5000" },
{ "2", "wtf", "RTL", "20000" },
{ "3", "hello", "world", "30000" },
{ "4", "why", "align", "25000" },
{ "5", "foo", "bar", "5000" },
{ "6", "wtf", "RTL", "20000" },
{ "7", "hello", "world", "30000" },
{ "8", "why", "align", "25000" },
{ "9", "hello", "world", "30000" },
{ "10", "hello", "world", "30000" },
{ "11", "hello", "world", "30000" }
};
String col[] = { "First", "Second", "Third", "Fourth" };
DefaultTableModel model = new DefaultTableModel( data, col );
/* Simply overrides isCellEditable to always * return false */
JTable table = new NonEditableTable( model );
/* By using AUTO_RESIZE_OFF, the header becomes correctly aligned but columns no longer auto-resize */
//table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
table.applyComponentOrientation( orientation );

/* Set all but 3rd column to have a preferred/max size */
TableColumn tableColumn = null;
for ( int i = 0; i < table.getColumnCount(); i++ ) {
if ( i != 2 ) {
tableColumn = table.getColumnModel().getColumn( i );
tableColumn.setMaxWidth( 100 );
tableColumn.setPreferredWidth( 100 );
}
}

/* Pretty */
JTableHeader header = table.getTableHeader();
header.setForeground( Color.RED );

/* ScrollPane = JScrollPane + JTable */
JPanel tablePanel = new JPanel( new BorderLayout() );
tablePanel.add( BorderLayout.CENTER, table );
tablePanel.add( BorderLayout.CENTER, new JButton( "Doh!" ) );
JScrollPane scrollPane = new JScrollPane( tablePanel );
scrollPane.setBorder( BorderFactory.createEmptyBorder() );
/* NOTE: This is what breaks the header when using AUTO_RESIZE_ALL_COLUMNS, comment out to see */
scrollPane.setComponentOrientation( orientation );

/* Header */
JPanel headerPanel = new JPanel( new BorderLayout() );
headerPanel.add( BorderLayout.NORTH, new JLabel( "SWING TEST" ) );
headerPanel.add( BorderLayout.CENTER, Box.createVerticalStrut( 5 ) );
headerPanel.add( BorderLayout.SOUTH, table.getTableHeader() );
headerPanel.applyComponentOrientation( orientation );

/* Main = Header + ScrollPane */
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add( BorderLayout.NORTH, headerPanel );
mainPanel.add( BorderLayout.CENTER, scrollPane );

/* Add to main frame */
frame.add( mainPanel );
frame.setUndecorated( true );
frame.getRootPane().setWindowDecorationStyle( JRootPane.PLAIN_DIALOG );
frame.setSize( 500, 200 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

SwingUtilities.invokeLater( new Runnable() {

@Override
public void run() {
frame.setVisible( true );
}
} );

}

@SuppressWarnings( "serial" )
public static class NonEditableTable extends JTable {

public NonEditableTable( DefaultTableModel model ) {
super( model );
}

@Override
public boolean isCellEditable( int row, int column ) {
return false;
}

}
}

最佳答案

做这样的事情怎么样?这样您就可以更好地控制方向。

JPanel headerPanel = new JPanel( new BorderLayout() );

JPanel headerPan = new JPanel();
JLabel xxx = new JLabel( "SWING TEST" );
xxx.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

headerPan.add(xxx);

希望对您有所帮助。

关于java - JScrollPane 打破从右到左的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499653/

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