如何将JTable插入面板?

时间:2016-03-30 12:26:59

标签: java swing jtable

我正在尝试将新创建的public class resultTable extends JPanel { public void mainTable() { GridLayout mainLayout = (new GridLayout(1, 0)); String[] columnNames = { "NAME", "GRADE" }; Object[][] data = { { nameField.getText(), gradeField.getText() } }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); } } public class myBottomPanel extends JPanel { public myBottomPanel() { setBorder(BorderFactory.createTitledBorder("Students/Results")); setOpaque(false); setPreferredSize(new Dimension(0, 100)); add(resultField); resultField.setAlignmentX(LEFT_ALIGNMENT); add(resultTable); } } public class buttonListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { String studentName; int studentMark; if (event.getSource() == addEntry) { studentName = nameField.getText(); String intMark = gradeField.getText(); studentMark = Integer.parseInt(intMark); System.out.println(studentName); System.out.println(studentMark); } } } } 插入到我创建的但没有运气的面板中。除此之外,我还尝试创建一个基于按下按钮进行编辑的表,其中按钮读取并存储用户输入,然后填写正确的列,并且不确定我是否以正确的方式进行了此操作

代码如下:

ws.Columns("D:D").Select
Selection.replace What:="In", Replacement:="Scheduled to work", LookAt:=xlPart _
    , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=Falseenter code here

1 个答案:

答案 0 :(得分:2)

如果您希望表格可滚动且可见,请确保将表格添加为JScrollPane的视图

// Create the Scroll panel and add the table (view)
JScrollPane jsp = new JScrollPane(table)
....
// Add the Scroll Panel to the main panel
myBottomPanel.add(jsp)

然后,将JScrollpane添加到面板中,该表应该是可见的和可滚动的。