gpt4 book ai didi

java - 如何调整 JScrollPane rowHeader 的大小

转载 作者:行者123 更新时间:2023-12-02 04:06:43 25 4
gpt4 key购买 nike

下面的代码将生成此表: enter image description here

如图所示,在此表中您可以调整列 A、B、... 的大小但是您无法使用小写字母 a、b、c、e...(即 JScrollPane 的 rowHeader)调整列的大小。

有什么想法我该怎么做吗?

package main;
// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html
/* (swing1.1beta3) */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
* @version 1.0 11/09/98
*/

class RowHeaderRenderer extends JLabel implements ListCellRenderer {

RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;
}
}

public class RowHeaderTable extends JFrame {

public RowHeaderTable() {
super("Row Header Example");
setSize(300, 150);

ListModel lm = new AbstractListModel() {
String headers[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };

public int getSize() {
return headers.length;
}

public Object getElementAt(int index) {
return headers[index];
}
};

DefaultTableModel dm = new DefaultTableModel(lm.getSize(), 10);
JTable table = new JTable(dm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JList rowHeader = new JList(lm);
rowHeader.setFixedCellWidth(50);

rowHeader.setFixedCellHeight(table.getRowHeight()
+ table.getRowMargin());
// + table.getIntercellSpacing().height);
rowHeader.setCellRenderer(new RowHeaderRenderer(table));

JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
getContentPane().add(scroll, BorderLayout.CENTER);
}

public static void main(String[] args) {
RowHeaderTable frame = new RowHeaderTable();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}

最佳答案

正如 @MadProgrammer 已经建议的那样,MouseMotionListener 可能是到达这里的唯一方法,即使不是最简单的方法。这是修改后的代码:

package main;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

class RowHeaderRenderer extends JLabel implements ListCellRenderer {

RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;
}
}

public class RowHeaderTable extends JFrame {

public RowHeaderTable() {
super("Row Header Example");
setSize(300, 150);

ListModel lm = new AbstractListModel() {
String headers[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };

@Override
public int getSize() {
return headers.length;
}

@Override
public Object getElementAt(int index) {
return headers[index];
}
};

DefaultTableModel dm = new DefaultTableModel(lm.getSize(), 15);
JTable table = new JTable(dm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JList rowHeader = new JList(lm);
rowHeader.setFixedCellWidth(50);

// *************** CODE ADDED *******************
// Add a Mouse Motion Listener for the rowHeader JList.
rowHeader.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
// Set the list cell width as mouse is dragged.
rowHeader.setFixedCellWidth(e.getX());
}

@Override
public void mouseMoved(MouseEvent e) {
// If the mouse pointer is near the end region of the
// list cell then change the mouse cursor to a resize cursor.
if ((e.getX()>= (rowHeader.getWidth() - 5)) && (e.getX()<= rowHeader.getWidth())) {
rowHeader.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
}
// If the mouse pointer is not near the end region of a cell
// then change the pointer back to its default.
else {
rowHeader.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
});
// ***********************************************

rowHeader.setFixedCellHeight(table.getRowHeight()
+ table.getRowMargin());
// + table.getIntercellSpacing().height);
rowHeader.setCellRenderer(new RowHeaderRenderer(table));

JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
getContentPane().add(scroll, BorderLayout.CENTER);
}

public static void main(String[] args) {
RowHeaderTable frame = new RowHeaderTable();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}

关于java - 如何调整 JScrollPane rowHeader 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34214388/

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