JScrollPane panel = new JScrollPane();
JTable table = new JTable();
table.setPreferredSize(new Dimension(1000, 1000));
panel.setPreferredSize(new Dimension(300, 300));
panel.setViewportView(table);
请问要如何设置才能出现横向滚动条
如果加了
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
则又不能手动拖动改变列宽了。求救啊。搞了好久都没有试出来。
非常感谢!
11 个解决方案
#1
上面列出来的代码没问题,效果应该可以出现。
不行的话,贴全部代码。
不行的话,贴全部代码。
#2
table放到JScrollPane里,应该没什么问题,只要table里面的数据多了就出现效果了
#3
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
final JScrollPane scroller = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
可以出现
#4
还是不行,只要设置了
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);,就不能用鼠标改变列宽了。
#5
奇怪,我可以,文档里也这么说。
#6
下面是全部的代码,谢谢各位
package gui;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CommonSPPanel extends JDialog {
private static final long serialVersionUID = 1L;
private JTextField codeTxt;
private JButton btn;
private JTable table;
private Object[] columnNames = { "商品编码", "条码", "商品名称" };
private String code;
private Object[][] cells;
private DefaultTableModel model;
public CommonSPPanel(String code) {
this.code = code;
initComponents();
setSize(100, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setModal(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Dialog dialog = (Dialog) evt.getSource();
dialog.setVisible(false);
dialog.dispose();
}
});
}
private void initComponents() {
JPanel topPanel = new JPanel();
JLabel codeLabel = new JLabel();
codeLabel.setText("编码");
codeTxt = new JTextField();
codeTxt.setColumns(5);
btn = new JButton("搜索");
codeTxt.setText(code);
topPanel.add(codeLabel);
topPanel.add(codeTxt);
topPanel.add(btn);
table = new JTable();
buildTable(code);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
// table.setPreferredScrollableViewportSize(new Dimension(600, 600));
table.getColumnModel().getColumn(0).setMaxWidth(200);
table.getColumnModel().getColumn(1).setMaxWidth(300);
table.getColumnModel().getColumn(2).setMaxWidth(500);
table.setPreferredSize(new Dimension(1000, 1000));
JScrollPane panel = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
panel.setPreferredSize(new Dimension(500, 300));
add(panel, BorderLayout.CENTER);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String code = codeTxt.getText();
if (code != null && !code.trim().equals("")) {
buildTable(code);
}
}
});
}
public void buildTable(String code) {
model = new DefaultTableModel();
cells = new Object[20][columnNames.length];
for (int i = 0; i < cells.length; i++) {
cells[i][0] = "pluCode";
cells[i][1] = "barCode";
cells[i][2] = "pluName";
}
model.setDataVector(cells, columnNames);
table.setModel(model);
}
public static void main(String[] args) {
new CommonSPPanel("");
}
}
#7
上面代码显示出来的结果如上图。
希望是能够有横向滚动条可以左右滑动来看整行结果。同时可以用鼠标拖动改变列宽
#8
table.getColumnModel().getColumn(0).setMaxWidth(200);
table.getColumnModel().getColumn(1).setMaxWidth(300);
table.getColumnModel().getColumn(2).setMaxWidth(500);
去掉后,应该可以了
#9
把那个setSize(100,500)改成 pack()
#10
to:huntor还是不行
最后用了个本办法来处理
在JScrollPane外边再套一个JScrollPane
里面的JScrollPane跟JTable的宽高一致, 自动resize还是按照默认的
这样就实现了上面的要求。
谢谢上面各位。
最后用了个本办法来处理
在JScrollPane外边再套一个JScrollPane
里面的JScrollPane跟JTable的宽高一致, 自动resize还是按照默认的
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
这样就实现了上面的要求。
谢谢上面各位。
package gui;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CommonSPPanel extends JDialog {
private static final long serialVersionUID = 1L;
private JTextField codeTxt;
private JButton btn;
private JTable table;
private Object[] columnNames = { "商品编码", "条码", "商品名称" };
private String code;
private Object[][] cells;
private DefaultTableModel model;
public CommonSPPanel(String code) {
this.code = code;
initComponents();
setSize(500, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setModal(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Dialog dialog = (Dialog) evt.getSource();
dialog.setVisible(false);
dialog.dispose();
}
});
}
private void initComponents() {
JPanel topPanel = new JPanel();
JLabel codeLabel = new JLabel();
codeLabel.setText("编码");
codeTxt = new JTextField();
codeTxt.setColumns(5);
btn = new JButton("搜索");
codeTxt.setText(code);
topPanel.add(codeLabel);
topPanel.add(codeTxt);
topPanel.add(btn);
table = new JTable();
buildTable(code);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
// table.setPreferredScrollableViewportSize(new Dimension(600, 600));
table.setPreferredSize(new Dimension(1000, 1000));
JScrollPane panel = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
panel.setWheelScrollingEnabled(true);
panel.setAutoscrolls(true);
panel.setPreferredSize(new Dimension(1000, 1000));
JScrollPane p = new JScrollPane(panel);
p.setPreferredSize(new Dimension(500,300));
add(p, BorderLayout.CENTER);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String code = codeTxt.getText();
if (code != null && !code.trim().equals("")) {
buildTable(code);
}
}
});
}
public void buildTable(String code) {
model = new DefaultTableModel();
cells = new Object[20][columnNames.length];
for (int i = 0; i < cells.length; i++) {
cells[i][0] = "pluCode";
cells[i][1] = "barCode";
cells[i][2] = "pluName";
}
model.setDataVector(cells, columnNames);
table.setModel(model);
}
public static void main(String[] args) {
new CommonSPPanel("");
}
}
#11
改成pack后界面变得好大。
#1
上面列出来的代码没问题,效果应该可以出现。
不行的话,贴全部代码。
不行的话,贴全部代码。
#2
table放到JScrollPane里,应该没什么问题,只要table里面的数据多了就出现效果了
#3
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
final JScrollPane scroller = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
可以出现
#4
还是不行,只要设置了
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);,就不能用鼠标改变列宽了。
#5
奇怪,我可以,文档里也这么说。
#6
下面是全部的代码,谢谢各位
package gui;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CommonSPPanel extends JDialog {
private static final long serialVersionUID = 1L;
private JTextField codeTxt;
private JButton btn;
private JTable table;
private Object[] columnNames = { "商品编码", "条码", "商品名称" };
private String code;
private Object[][] cells;
private DefaultTableModel model;
public CommonSPPanel(String code) {
this.code = code;
initComponents();
setSize(100, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setModal(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Dialog dialog = (Dialog) evt.getSource();
dialog.setVisible(false);
dialog.dispose();
}
});
}
private void initComponents() {
JPanel topPanel = new JPanel();
JLabel codeLabel = new JLabel();
codeLabel.setText("编码");
codeTxt = new JTextField();
codeTxt.setColumns(5);
btn = new JButton("搜索");
codeTxt.setText(code);
topPanel.add(codeLabel);
topPanel.add(codeTxt);
topPanel.add(btn);
table = new JTable();
buildTable(code);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
// table.setPreferredScrollableViewportSize(new Dimension(600, 600));
table.getColumnModel().getColumn(0).setMaxWidth(200);
table.getColumnModel().getColumn(1).setMaxWidth(300);
table.getColumnModel().getColumn(2).setMaxWidth(500);
table.setPreferredSize(new Dimension(1000, 1000));
JScrollPane panel = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
panel.setPreferredSize(new Dimension(500, 300));
add(panel, BorderLayout.CENTER);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String code = codeTxt.getText();
if (code != null && !code.trim().equals("")) {
buildTable(code);
}
}
});
}
public void buildTable(String code) {
model = new DefaultTableModel();
cells = new Object[20][columnNames.length];
for (int i = 0; i < cells.length; i++) {
cells[i][0] = "pluCode";
cells[i][1] = "barCode";
cells[i][2] = "pluName";
}
model.setDataVector(cells, columnNames);
table.setModel(model);
}
public static void main(String[] args) {
new CommonSPPanel("");
}
}
#7
上面代码显示出来的结果如上图。
希望是能够有横向滚动条可以左右滑动来看整行结果。同时可以用鼠标拖动改变列宽
#8
table.getColumnModel().getColumn(0).setMaxWidth(200);
table.getColumnModel().getColumn(1).setMaxWidth(300);
table.getColumnModel().getColumn(2).setMaxWidth(500);
去掉后,应该可以了
#9
把那个setSize(100,500)改成 pack()
#10
to:huntor还是不行
最后用了个本办法来处理
在JScrollPane外边再套一个JScrollPane
里面的JScrollPane跟JTable的宽高一致, 自动resize还是按照默认的
这样就实现了上面的要求。
谢谢上面各位。
最后用了个本办法来处理
在JScrollPane外边再套一个JScrollPane
里面的JScrollPane跟JTable的宽高一致, 自动resize还是按照默认的
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
这样就实现了上面的要求。
谢谢上面各位。
package gui;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CommonSPPanel extends JDialog {
private static final long serialVersionUID = 1L;
private JTextField codeTxt;
private JButton btn;
private JTable table;
private Object[] columnNames = { "商品编码", "条码", "商品名称" };
private String code;
private Object[][] cells;
private DefaultTableModel model;
public CommonSPPanel(String code) {
this.code = code;
initComponents();
setSize(500, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setModal(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Dialog dialog = (Dialog) evt.getSource();
dialog.setVisible(false);
dialog.dispose();
}
});
}
private void initComponents() {
JPanel topPanel = new JPanel();
JLabel codeLabel = new JLabel();
codeLabel.setText("编码");
codeTxt = new JTextField();
codeTxt.setColumns(5);
btn = new JButton("搜索");
codeTxt.setText(code);
topPanel.add(codeLabel);
topPanel.add(codeTxt);
topPanel.add(btn);
table = new JTable();
buildTable(code);
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
// table.setPreferredScrollableViewportSize(new Dimension(600, 600));
table.setPreferredSize(new Dimension(1000, 1000));
JScrollPane panel = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
panel.setWheelScrollingEnabled(true);
panel.setAutoscrolls(true);
panel.setPreferredSize(new Dimension(1000, 1000));
JScrollPane p = new JScrollPane(panel);
p.setPreferredSize(new Dimension(500,300));
add(p, BorderLayout.CENTER);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String code = codeTxt.getText();
if (code != null && !code.trim().equals("")) {
buildTable(code);
}
}
});
}
public void buildTable(String code) {
model = new DefaultTableModel();
cells = new Object[20][columnNames.length];
for (int i = 0; i < cells.length; i++) {
cells[i][0] = "pluCode";
cells[i][1] = "barCode";
cells[i][2] = "pluName";
}
model.setDataVector(cells, columnNames);
table.setModel(model);
}
public static void main(String[] args) {
new CommonSPPanel("");
}
}
#11
改成pack后界面变得好大。