public class SplitPaneExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JMenu menu;
JMenuItem menuOpen, menuSave, menuQuit;
JMenuBar menuBar;
JFileChooser fileChoose;
File file;
FileReader fileReader;
BufferedReader bufferReader;
StringBuffer stringBuffer;
JTextArea bottomArea, topLeft, topRight;
JScrollPane scrollPane;
JSplitPane hSplit, vSplit;
JPanel mainPanel, leftPanel, rightPanel;
JTable myLeftTable;
JScrollPane myLeftTableScrollPane;
@SuppressWarnings("rawtypes")
Vector<Vector> leftTableContainer, rightTableContainer, bottomTableContainer;
Vector<String> leftTableValues, rightTableValues, leftTableColumnName;
public SplitPaneExample(){
createUserInterface();
}
private void createUserInterface() {
setFrame();
setPanels();
}
public void setFrame(){
//set frame/window title, background color, dimensions...
setTitle("test - top left and table listeners...");
setBackground(Color.white);
setPreferredSize(new Dimension(600, 500));
setVisible(true);
}
public void setPanels(){
//set text areas
topLeft = new JTextArea();
topLeft.setText("topLeft...");
topLeft.setAlignmentX(CENTER_ALIGNMENT);
topRight = new JTextArea();
topRight.setText("topRight...");
topRight.setAlignmentX(CENTER_ALIGNMENT);
bottomArea = new JTextArea();
bottomArea.setText("bottomArea...");
bottomArea.setAlignmentX(CENTER_ALIGNMENT);
//split panes
vSplit = new JSplitPane();
vSplit.setOrientation(JSplitPane.VERTICAL_SPLIT);
vSplit.setDividerSize(3);
vSplit.setDividerLocation(200);
hSplit = new JSplitPane();
hSplit.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
hSplit.setDividerSize(3);
hSplit.setDividerLocation(130);
// add table to left component
leftTableValues = new Vector<String>();
leftTableValues.add("value 0");
leftTableValues.add("value 1");
leftTableValues.add("value 2");
leftTableValues.add("value 3");
hSplit.setLeftComponent(getLeftTable());
hSplit.setRightComponent(topRight);
vSplit.setLeftComponent(hSplit);
vSplit.setRightComponent(bottomArea);
//add vertical split: vSplit to content_pane
Container content_pane = getContentPane();
content_pane.add(vSplit, BorderLayout.CENTER);
}
public JScrollPane getLeftTable(){
myLeftTable = new JTable(getLeftTableValues(), getColumnName());
myLeftTable.setCellSelectionEnabled(true);
ListSelectionModel cellSelectionModel = myLeftTable.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel
.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
String valueSelected = null;
int selectedRow = myLeftTable.getSelectedRow();
valueSelected = leftTableValues.get(selectedRow);
System.out.println("value selected: " + valueSelected);
}
});
myLeftTableScrollPane = new JScrollPane(myLeftTable);
return myLeftTableScrollPane;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Vector<Vector> getLeftTableValues(){
if(leftTableContainer == null){
leftTableContainer = new Vector<Vector>();
//left table values
Vector[] v = new Vector[leftTableValues.size()];
for(int x=0; x < leftTableValues.size(); x++){
v[x] = new Vector<String>();
v[x].add(leftTableValues.get(x));
leftTableContainer.add(v[x]);
}
return leftTableContainer;
} else {
return leftTableContainer;
}
}
public Vector<String> getColumnName(){
if(leftTableColumnName == null){
leftTableColumnName = new Vector<String>();
leftTableColumnName.add("values:");
return leftTableColumnName;
} else {
return leftTableColumnName;
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String args[]) {
// Create an instance of the test application
SplitPaneExample frame = new SplitPaneExample();
frame.pack();
frame.setVisible(true);
}
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
From novice to tech pro — start learning today.
Open in new window